自动将 PDF 打印到指定打印机
问题:如何在用户启动操作后将在用户桌面上创建的 PDF 文件自动发送到特定的本地打印机队列?
解决方案:利用 PdfiumViewer 库
Google Pdfium 库及其 .NET 包装器 PdfiumViewer 提供了一个简单的解决方案。 以下是演示具有可自定义设置的静默 PDF 打印的示例:
<code class="language-csharp">public bool PrintPDF(string printerName, string paperSizeName, string filePath, int numberOfCopies) { try { // Configure printer settings var printerSettings = new PrinterSettings { PrinterName = printerName, Copies = (short)numberOfCopies }; // Configure page settings var pageSettings = new PageSettings(printerSettings) { Margins = new Margins(0, 0, 0, 0) // Set margins to zero }; // Find the specified paper size foreach (PaperSize paperSize in printerSettings.PaperSizes) { if (paperSize.PaperName == paperSizeName) { pageSettings.PaperSize = paperSize; break; } } // Initiate PDF printing using (var pdfDocument = PdfDocument.Load(filePath)) using (var printDocument = pdfDocument.CreatePrintDocument()) { printDocument.PrinterSettings = printerSettings; printDocument.DefaultPageSettings = pageSettings; printDocument.PrintController = new StandardPrintController(); //Ensures standard printing behavior printDocument.Print(); } return true; } catch (Exception ex) { //Handle exceptions appropriately (log, display error message, etc.) return false; } }</code>
主要考虑因素:
以上是如何自动打印PDF到特定的打印机队列?的详细内容。更多信息请关注PHP中文网其他相关文章!