使用 C# 将 PDF 打印为 PDF

本文介绍如何使用 C# 将 PDF 打印为 PDF。您将获得有关设置开发环境的详细信息、步骤列表以及使用Adobe PDF 打印机和 C# 的示例代码。它还将分享设置打印机设置、页面设置和打印属性的属性。

使用 C# 打印到 PDF 虚拟打印机的步骤

  1. 设置环境使用Aspose.PDF for .NET用软打印机打印
  2. 实例化 PDFViewer 类对象
  3. 通过将源 PDF 文件与查看器绑定来加载它
  4. 设置打印属性
  5. 创建 PrinterSettings 和 PageSettings 类对象并设置所需的属性
  6. 将文档打印到 PDF 打印机
  7. 关闭 PDF 文件

这些步骤分享了使用 C# 虚拟 PDF 打印机 的详细信息。实例化 PdfViewer 类对象,绑定源 PDF 文件,并设置打印属性。创建 PrinterSettings 和 PageSettings 类的对象,并在使用 PDFViewer 类中的 PrintDocumentWithSettings() 方法打印文档之前设置打印机名称、页面大小和边距。

使用 C# 打印到 PDF 打印机的代码

using Aspose.Pdf;
using Aspose.Pdf.Facades;
class Program
{
static void Main(string[] args)
{
new License().SetLicense("License.lic");
// Instantiate the PdfViewer object
PdfViewer pdfViewer = new PdfViewer();
// Load the input PDF file
pdfViewer.BindPdf("sample.pdf");
// Set printing attributes
pdfViewer.AutoResize = true;
pdfViewer.AutoRotate = true;
pdfViewer.PrintPageDialog = false;
pdfViewer.PrintAsImage = false;
// Create objects for PrinterSettings and Page settings
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set printer name, paper size and margins
ps.PrinterName = "Adobe PDF";
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
pgs.Margins = new Aspose.Pdf.Devices.Margins(5, 5, 5, 5);
// Print the document
pdfViewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file
pdfViewer.Close();
// Save the document
System.Console.WriteLine("PDF printed successfully");
}
}

此示例代码演示了如何使用 C# 开发 虚拟打印机软件。您可以在 PDFViewer 中设置其他属性,例如坐标类型、水平对齐、灰度打印选项和分辨率(如果需要)。使用默认设置时,您可以省略设置纸张大小和边距。

本教程向我们展示了如何使用 Adobe PDF 虚拟打印机和 C#。如果您想旋转 PDF,请参阅 如何使用 C# 旋转 PDF 上的文章。

 简体中文