通过 Python 打印 PDF

列印 PDF 文件。使用 Aspose.PDF for Python for .NET 以程式設計方式修改 PDF 文件

如何使用 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()