翻譯|使用教程|編輯:李顯亮|2020-07-10 10:18:04.877|閱讀 798 次
概述:許多公司需要在分發(fā)在線訂單進(jìn)行交付時(shí)打印PDF。他們大規(guī)模處理項(xiàng)目,因此手動打印每個(gè)文檔可能是一項(xiàng)艱巨的任務(wù)。在本文中將探討與使用C#在.NET應(yīng)用程序中打印PDF文件有關(guān)的用例。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
許多公司在很大程度上減少了紙張的使用。但是,在某些情況下打印很重要。例如,系統(tǒng)可能包含PDF格式的在線訂單的詳細(xì)信息。他們需要在分發(fā)在線訂單進(jìn)行交付時(shí)打印PDF。他們大規(guī)模處理項(xiàng)目,因此手動打印每個(gè)文檔可能是一項(xiàng)艱巨的任務(wù)。
Aspose.PDF除了許多其他文件操縱和轉(zhuǎn)換功能外,還支持PDF文件的高效打印。讓我們探討以下與使用C#在.NET應(yīng)用程序中打印PDF文件有關(guān)的用例:
目前,.NET版Aspose.PDF升級到v20.7版,添加PDF到CSV轉(zhuǎn)換支持,更改XImage.Name結(jié)果損壞的PDF文檔,修復(fù)提取頁面后,缺少內(nèi)容的問題,感興趣的朋友可點(diǎn)擊下方按鈕下載最新版。
點(diǎn)擊下載最新版Aspose.PDF for .NET
可以使用C#或VB.net在.NET應(yīng)用程序中自動打印PDF文件。您可以按照以下簡單步驟打印PDF文件:
下面的代碼段顯示了如何使用C#打印PDF文件:
//Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(dataDir + "Test.pdf"); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close();
如果由于業(yè)務(wù)性質(zhì)需要打印多個(gè)文件,則Aspose.PDF for .NET API已支持該功能。使用上面的代碼片段一個(gè)接一個(gè)地打印多個(gè)PDF文件可能會有點(diǎn)慢。因此,讓我們將PDF打印再進(jìn)一步邁出一步,以簡化流程。在這里,我們將使用列表,同時(shí)將每個(gè)PDF文件的名稱添加到該列表中。以下步驟說明了我們將如何打印多個(gè)PDF文件:
該代碼段顯示了如何使用C#打印多個(gè)PDF文件:
var files = new List(); files.Add(dataDir + "First.pdf"); files.Add(dataDir + "Second.pdf"); foreach (String file in files) { //Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(file); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close(); }
API中提供了打印PDF文檔特定頁面的功能。我們將考慮一個(gè)示例,其中包括打印多個(gè)頁面范圍。您需要按照以下步驟中的說明指定起始和結(jié)束頁碼:
下面的代碼段顯示了如何使用C#打印文檔的特定頁面:
string inPdf = dataDir + "Test.pdf"; string output = dataDir; IListprintingJobs = new List(); PrintingJobSettings printingJob1 = new PrintingJobSettings(); printingJob1.FromPage = 2; printingJob1.ToPage = 3; printingJobs.Add(printingJob1); PrintingJobSettings printingJob2 = new PrintingJobSettings(); printingJob2.FromPage = 5; printingJob2.ToPage = 7; printingJobs.Add(printingJob2); { for (var printingJobIndex = 0; printingJobIndex < printingJobs.Count; printingJobIndex++) { System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); ps.PrinterName = "Microsoft Print to PDF"; ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; ps.FromPage = printingJobs[printingJobIndex].FromPage; ps.ToPage = printingJobs[printingJobIndex].ToPage; System.Console.WriteLine(ps.FromPage); System.Console.WriteLine(ps.ToPage); System.Console.WriteLine(printingJobIndex); using (var theViewer = new Aspose.Pdf.Facades.PdfViewer()) { // Document printing code goes here // Print document using printer and page settings theViewer.BindPdf(inPdf); theViewer.AutoResize = true; theViewer.AutoRotate = true; theViewer.PrintPageDialog = false; theViewer.PrintDocumentWithSettings(pgs, ps); theViewer.Close(); } } }
PDF文件可以用密碼保護(hù)和保護(hù)。但是,密碼可以有兩種類型,即用戶密碼和所有者密碼。使用用戶密碼保護(hù)的PDF文件需要密碼才能打開和查看加密的PDF文件。另一方面,需要所有者密碼來修改受保護(hù)和受密碼保護(hù)的PDF文件的內(nèi)容。以下步驟說明了安全PDF的打印:
以下代碼段顯示了如何使用C#打印受保護(hù)的PDF文件:
//Load secure PDF document while specifying User or Owner password Document document = new Document(dataDir + "Password.pdf" , "userORowner"); //Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(document); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close();
使用Aspose.PDF for .NET API將PDF打印到特定的紙盒。例如,您可能希望將包含大量照片的PDF打印到另一個(gè)紙盒,將文本PDF文件打印到另一個(gè)紙盒。請按照以下步驟設(shè)置用于打印PDF文件的出紙盤或紙槽:
這里值得注意的是,您可以更改打印機(jī)的名稱。在這里,我們將使用Microsoft Print to PDF作為示例。以下代碼段遵循以下步驟,并說明如何使用C#將文檔打印到特定的紙盒或打印機(jī)的紙槽中:
Document doc = new Document("Test.pdf"); PdfViewer viewer = new PdfViewer(); viewer.BindPdf(doc); viewer.PrinterJobName = System.IO.Path.GetFileName(doc.FileName); viewer.Resolution = 110; // Set attributes for printing viewer.AutoResize = true; // Print the file with adjusted size viewer.AutoRotate = false; // Print the file with adjusted rotation viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing viewer.RenderingOptions.UseNewImagingEngine = true; // Create objects for printer and page settings and PrintDocument System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); // Set printer name ps.PrinterName = "Microsoft Print to PDF"; pgs.PaperSize = new System.Drawing.Printing.PaperSize(paperTypeName, paperWidth, paperHeight); pgs.Margins = new System.Drawing.Printing.Margins(margins.Left, margins.Right, margins.Top, margins.Bottom); pgs.PaperSource = GetPaperSource(printerName, trayName); // Print document using printer and page settings viewer.PrintDocumentWithSettings(pgs, ps); ////// Return the PaperSource object for the provided printer and tray name. ////////////public static System.Drawing.Printing.PaperSource GetPaperSource(string printerName, string trayName) { System.Drawing.Printing.PaperSource ps = null; System.Drawing.Printing.PrintDocument prtDoc = new System.Drawing.Printing.PrintDocument(); prtDoc.PrinterSettings.PrinterName = printerName; for (int i = 0; i < prtDoc.PrinterSettings.PaperSources.Count; i++) { if (prtDoc.PrinterSettings.PaperSources[i].SourceName.ToLower().Equals(trayName.ToLower())) { ps = prtDoc.PrinterSettings.PaperSources[i]; break; } } return ps; }
在某些情況下,您可能需要將一個(gè)PDF文檔的不同頁面打印到不同的紙盒或紙槽中。例如,封面的紙張來源不同,其他頁面的紙張來源不同。您當(dāng)然可以按照以下步驟在同一打印作業(yè)中將頁面范圍打印到不同的紙張來源:
以下代碼段顯示了如何執(zhí)行以下步驟,以及如何使用C#將不同的頁面范圍打印到不同的紙張來源或紙盤:
Aspose.Pdf.Facades.PdfViewer pdfv = new Aspose.Pdf.Facades.PdfViewer(); pdfv.PdfQueryPageSettings += PdfvOnPdfQueryPageSettings; System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrinterSettings prin = new System.Drawing.Printing.PrinterSettings(); pdfv.BindPdf(dataDir + "Print-PageRange.pdf"); prin.PrinterName = "HP LaserJet M9050 MFP PCL6"; prin.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); Aspose.Pdf.Facades.PdfPageEditor pageEditor = new Aspose.Pdf.Facades.PdfPageEditor(); pageEditor.BindPdf(dataDir + "input.pdf"); pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); pgs.PaperSize = prin.DefaultPageSettings.PaperSize; pdfv.PrintDocumentWithSettings(pgs, prin); pdfv.Close(); private static void PdfvOnPdfQueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs queryPageSettingsEventArgs, PdfPrintPageInfo currentPageInfo) { bool isOdd = currentPageInfo.PageNumber % 2 != 0; System.Drawing.Printing.PrinterSettings.PaperSourceCollection paperSources = queryPageSettingsEventArgs.PageSettings.PrinterSettings.PaperSources; if (isOdd) queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[0]; else queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[1]; }
可以將PDF文件打印到其他打印機(jī)。例如,Microsoft Print to PDF,Microsoft XPS Document Writer或任何物理打印機(jī)。但是,大型PDF文檔的打印可能會花費(fèi)很長時(shí)間,或者由于某種原因打印可能會失敗。因此,API提供了一項(xiàng)功能,可讓您通過以下步驟檢查打印作業(yè)的狀態(tài):
下面的代碼片段顯示了如何使用C#檢查打印作業(yè)狀態(tài)或PDF的打印進(jìn)度:
// Instantiate PdfViewer object PdfViewer viewer = new PdfViewer(); // Bind source PDF file viewer.BindPdf(dataDir + "Sample Document with Bookmark.pdf"); viewer.AutoResize = true; // Hide printing dialog viewer.PrintPageDialog = false; // Create Printer Settings object System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument(); // Specify the printer anme //ps.PrinterName = "Microsoft XPS Document Writer"; ps.PrinterName = "Microsoft Print to PDF"; // Resultant Printout name //ps.PrintFileName = "ResultantPrintout.xps"; ps.PrintFileName = "ResultantPrintout.pdf"; // Print the output to file ps.PrintToFile = true; ps.FromPage = 1; ps.ToPage = 2; ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; // Specify the page size of printout pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); ps.DefaultPageSettings.PaperSize = pgs.PaperSize; pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); // Print the document with settings specified above viewer.PrintDocumentWithSettings(pgs, ps); // Check the print status if (viewer.PrintStatus != null) { // An exception was thrown Exception ex = viewer.PrintStatus as Exception; if (ex != null) { // Get exception message } } else { // No errors were found. Printing job has completed successfully Console.WriteLine("printing completed without any issue.."); }
結(jié)果,此代碼段在.NET應(yīng)用程序的控制臺上打印有關(guān)PDF文件的打印狀態(tài)的代碼:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn