我有3台打印机连接到我的电脑,在我的win表格中,我按代码选择打印,(我不使用PrintDialog)。每台打印机都有不同的纸张尺寸(小,中,大)。我能以任何方式获得当前打印机的纸张尺寸。 我试试
Console.WriteLine(e.PageSettings.Bounds.ToString());
Console.WriteLine(e.PageSettings.PaperSize.ToString());
Console.WriteLine(e.Graphics.VisibleClipBounds.ToString());
Console.WriteLine(e.Graphics.ClipBounds.ToString());
我从委托参数方法中获取变量e:
// When I launch my printer (in class constructor):
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintLabel);
//.... other code
//My delegate method:
private void PrintLabel(object o, PrintPageEventArgs e)
{
//This is my code from above:
Console.WriteLine(e.PageSettings.Bounds.ToString());
// other code
}
但无论我使用哪种打印机,它都会显示相同的尺寸。 提前谢谢。
答案 0 :(得分:6)
为什么不在System.Drawing.Printing
命名空间中使用特定设计的类?
或者我错过了一些问题?
using System.Drawing.Printing;
public static PageSettings GetPrinterPageInfo(String printerName) {
PrinterSettings settings;
// If printer name is not set, look for default printer
if (String.IsNullOrEmpty(printerName)) {
foreach (var printer in PrinterSettings.InstalledPrinters) {
settings = new PrinterSettings();
settings.PrinterName = printer.ToString();
if (settings.IsDefaultPrinter)
return settings.DefaultPageSettings;
}
return null; // <- No default printer
}
// printer by its name
settings = new PrinterSettings();
settings.PrinterName = printerName;
return settings.DefaultPageSettings;
}
// Default printer default page info
public static PageSettings GetPrinterPageInfo() {
return GetPrinterPageInfo(null);
}
...
// Default printer default page
PageSettings page = GetPrinterPageInfo();
// Or default page of some other printer given by its name
// PageSettings page = GetPrinterPageInfo(MyPrinterName);
RectangleF area = page.PrintableArea;
Rectangle bounds = page.Bounds;
Margins margins = page.Margins;