使用 Python 打印 PDF

打印 PDF 文档。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文件

Aspose.PDF for Python for .NET Logo

如何使用 Python 库打印 PDF

要打印 PDF 文件,请使用 Aspose.PDF for Python via .NET,这是一个功能强大且易于使用的 API。打开 PyPI,搜索 aspose-pdf 并安装。或者,运行以下命令:

Console

pip install aspose-pdf

通过 Python 打印 PDF 文档


要在您的环境中尝试代码,您需要Aspose.PDF for Python via .NET

  1. 使用 “文档” 实例加载 PDF。
  2. 使用 Document.Info 属性获取文档信息。
  3. 访问并显示不同的 Document.Info 属性。

打印 PDF-Python

此示例代码显示如何打印 PDF 文件

import aspose.pdf as apdf

import aspose.pdf.facades as apfacades
import aspose.pydrawing as drawing
import aspose.pdf.printing as aprinting

from os import path

path_infile = path.join(self.data_dir, infile)

# Create PdfViewer object
viewer = apfacades.PdfViewer()
viewer.bind_pdf(path_infile)

viewer.auto_resize = True
viewer.auto_rotate = True
viewer.print_page_dialog = False

ps =  aprinting.PrinterSettings()
pgs = aprinting.PageSettings()

ps.printer_name = "Microsoft Print to PDF"

# Set PageSize (if required)
pgs.paper_size = aprinting.PaperSizes.A4

# Set PageMargins (if required)
pgs.margins = apdf.devices.Margins(0, 0, 0, 0)

# Print document using printer and page settings
viewer.print_document_with_settings(pgs, ps)

# Close the PDF file after printing
viewer.close()