Print PDF files C#

在今天的数字景观中,许多组织都专注于减少纸张消耗,但是,印刷是必需的场景仍然存在,如发送需要在PDF格式存储的在线订单。 Aspose.PDF 为 .NET API 提供一个有效的解决方案,以编程打印PDF文件。

在此指南中,您将学习如何在 .NET 应用程序中有效地打印 PDF 文件,使用 C#。

在您的 .NET 应用程序中使用 C# 自动打印 PDF 文件是简单的。

  • 创建一个对象的 PdfViewer 班级。
  • 输入 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();
view raw PrintPDF.cs hosted with ❤ by GitHub

如果您的业务需要打印多个PDF文件,Aspose.PDF for .NET API 可以帮助简化过程,而不是单独打印每个文件,您可以使用列表以有效地打印多个文件。

  • 初步 A List<string> 保持文件名。
  • 将PDF文件添加到列表中。
  • 下载每个PDF文件。
  • 打印多个 PDF 文件。

下面的代码剪辑描述了如何使用C#打印多个PDF文件:

var files = new List<string>();
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();
}

Aspose.PDF API 允许您从 PDF 文档中打印特定页面. 要做到这一点,您必须指定您想要打印的页面序列。

  • 设置输入和输出文件路径。
  • 设置您想要打印的页面范围。
  • 指定印刷参数。
  • 执行印刷命令。

下面是一个代码剪辑,表明如何打印PDF文档的特定页面:

string inPdf = dataDir + "Test.pdf";
string output = dataDir;
IList<PrintingJobSettings> printingJobs = new List<PrintingJobSettings>();
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 文件可以用密码安全,其中可能包括用户或所有者密码. 用户密码需要打开和查看 PDF,而所有者密码需要进行修改。

  • 使用密码下载安全的 PDF。
  • 创建 A PdfViewer 对象。
  • 打印安全的 PDF 文件。

下面的代码剪辑描述了如何使用 C# 打印安全的 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();

以 C# 打印 PDF 到 以 C# 打印 PDF 到 以 C# 打印 PDF 到 以 C# 打印 PDF

您可以使用 Aspose.PDF for .NET API 将 PDF 印刷引导到特定纸条,例如,您可能希望从一个字符串中打印图像重的 PDF 和基于文本的 PDF 从另一个字符串中打印。

下面是一个代码剪辑,展示如何打印到一个特定的纸条:

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);
/// <summary>
/// Return the PaperSource object for the provided printer and tray name.
/// </summary>
/// <param name="printerName"></param>
/// <param name="trayName"></param>
/// <returns></returns>
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;
}

打印页面范围到不同纸质来源使用C#

在某些情况下,您可能希望将相同的 PDF 文档的不同页面打印到不同的纸质来源。 例如,您可以从一个字符串打印覆盖页面,然后从另一个字符串打印后面的页面。

下面是一个代码剪辑,显示如何向不同的纸质来源打印不同的页面范围:

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 使用 C#

在打印PDF文件到各种打印机,如微软打印到PDF或任何物理打印机时,监测打印工作状态至关重要,这对于大型文件尤其重要,或者如果有可能打印可能失败。

  • 下载输入 PDF 文件。
  • 指定页面设置。
  • 打印机名称。
  • 使用 PDF 文档打印 印刷文件WithSettings 方法。

下面的代码剪辑显示如何检查印刷工作状态:

// 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..");
}

此代码剪辑将将 PDF 文件的印刷状态输出到您的 .NET 应用程序的控制台:

C# Print PDF

结论

在本文中,我们探讨了使用C#和Aspose插件打印安全和不安全的PDF文件的各种方法,我们讨论了如何打印完整的PDF文件、特定页面序列以及如何将印刷指向特定纸条或字符串的方式。

使用 Aspose.PDF for .NET Plugin,您可以通过高性能的 PDF 打印功能来提高您的应用程序,仅需 99 美元。

如果您正在寻找 ** 创建 PDF 文件在 C#** 中编程,请确保使用 C# Create a PDF File Programmatically 由 Aspose 提供的功能. 此外,如果您需要 Send PDF to Printer你可以利用 C# Print PDF File Programmatically 以确保无缝打印过程的选项,为快速解决方案, C# Print PDF Directly to Printer 方法是无价的,特别是当你需要打印PDF文件使用 Microsoft Print to PDF C# 或任何其他打印机设置. 此外,您可以使用 .NET Core Print PDF 能力或 .NET Print PDF 功能,以进一步简化您的打印任务。

你也可以探索 C# Aspose PDF Print 功能改进您的文档管理系统. 如果您对具体例子感兴趣,则 C# Aspose PDF Print Example 提供有用的了解如何有效地实施这些功能。

最后,考虑到 C# Print PDF Programmatically 以更直接的实施方式,以及 C# Print PDF File to Printer 以最大限度地提高您的打印效率的选项. 您是否正在使用 C# PDF Writer 或者看着 C# PDF Print Libraries机会是无限的。

除此之外,The .NET Core Print PDF.NET Print PDF 功能可为各种打印任务提供坚实的解决方案,确保您可以在任何 .NET 应用程序中轻松印刷 PDF 文档。

你也可以使用 C# Print PDF 命令编程管理您的印刷工作,以及 C# Print PDF Document 提供高品质输出的选项,包括 Aspose PDF C# Example 在您的应用程序中,您将能够更高效率和准确地处理 PDF 文件,最终提高您的生产力。

如果您正在考虑与 .NET 直接打印,则 ASP.NET Print PDFASP.NET Core Print PDF 方法提供有效的方式来管理您的打印需求。 C# Print PDF File Programmatically 该方法可以帮助简化您的工作流,确保您可以在各种应用程序中轻松处理PDF。

More in this category