我正在尝试使用 System.Drawing.Printing 库从 WCF 服务进行打印。问题是我正在尝试选择此库中似乎不可用的信头纸、卡片纸或预印纸的纸张类型(或介质类型)。
System.Drawing.Printing 有一个 PageSettings 类,但我只能设置 PaperSize 并且没有用于 Letterhead、Cardstock、Pre-printed 等的 PaperSize。https: //msdn.microsoft.com/en-us/library/System .Drawing.Printing.PageSettings(v=vs.110).aspx
此外,PrinterSettings.PaperSources 中的 PaperSource 类不包含有关每个纸盘中的纸张类型的任何信息。
有没有人建议如何确保我发送的打印作业具有正确的设置,以便打印机知道从哪个托盘打印?
必须有办法做到这一点。例如,我可以在从 Word 或 Excel 打印时选择信头,但只有在我转到打印机属性时才可以。为什么我不能在 .NET 中以编程方式执行此操作?这是托管代码限制吗?我需要访问打印机驱动程序吗?
甚至 System.Printing 也没有这些选项可用。MSDN 还指出:
警告: System.Printing 命名空间中的类不支持在 Windows 服务或 ASP.NET 应用程序或服务中使用。尝试从这些应用程序类型之一中使用这些类可能会产生意想不到的问题,例如服务性能下降和运行时异常。
我唯一可用的其他选项是让用户手动设置每台打印机,使用数据库中每个托盘中的纸张类型,并带有一些用户界面。然后我只需设置要从中打印的托盘。如果可能的话,我想避免这种情况。
2015 年 12 月 14 日更新
打印机制造商愿意提供付费解决方案,但目前该项目不可行。
粗略的代码解决方案是:
private PrintJobStatusEnum SendToPrinter(PrintDocumentModel printJob, out List<string> errors)
{
errors = new List<string>();
// The print job includes the printer and page settings
var printerSettings = new PrinterSettings
{
PrinterName = "MyPrinterName",
Duplex = printJob.IsDuplex ? Duplex.Vertical : Duplex.Simplex
};
// Set the paper size
var paperKind = PaperKind.Letter;
// Find the paper size in the available sizes on the printer
var paperSizes = printerSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
var paperSize = paperSizes.FirstOrDefault(p => p.Kind == paperKind);
// Set the paper source (tray)
var paperSources = printerSettings.PaperSources.Cast<PaperSource>().AsQueryable();
// The SourceName is different for many printers.
// Double-check yours through PrinterSettings.PaperSources
var paperSource = paperSources.FirstOrDefault(s => s.SourceName == "Cassette 1");
if (paperSource == null)
{
paperSource = paperSources.FirstOrDefault(s => s.Kind == PaperSourceKind.AutomaticFeed);
}
// Set up the page
var pageSettings = new PageSettings
{
Landscape = printJob.PaperOrientationLookUpId == MyConstants.PaperOrientationLandscape,
Margins = new Margins(0, 0, 0, 0), // Not sure if margins are needed
PaperSize = paperSize ?? new PaperSize("Letter", 850, 1100),
Color = printJob.IsColor,
PaperSource = paperSource,
PrinterSettings = printerSettings
};
// Send document, printer settings and page settings to print handler
List<string> printErrors;
var result = _pdfPrintHandler.Print(printerSettings, pageSettings, printJob, out printErrors);
errors.AddRange(printErrors);
return result;
}