通過Python列印 PDF

如何使用 Python 庫列印 PDF

如何使用 Python 庫列印 PDF

為了列印PDF檔,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Python Package Manager Console

pip install aspose-pdf

通過Python列印 PDF 文件


您需要 Aspose.PDF for .NET 在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。
  2. 使用 Document.Info 屬性獲取文件資訊。 1.訪問和顯示不同的 Document.Info 屬性。

列印 PDF - Python

<% print.code-block.subtitle %>

    import aspose.pdf as ap
    import aspose.pydrawing as drawing

    # Create PdfViewer object
    viewer = ap.facades.PdfViewer()

    # Open input PDF file
    viewer.bind_pdf("input.pdf")

    # Set attributes for printing
    # Print the file with adjusted size
    viewer.auto_resize = True
    # Print the file with adjusted rotation
    viewer.auto_rotate = True
    # Do not produce the page number dialog when printing
    viewer.print_page_dialog = False

    # Create objects for printer and page settings
    ps = drawing.printing.PrinterSettings()
    pgs = drawing.printing.PageSettings()

    # Set XPS/PDF printer name
    ps.printer_name = "Microsoft XPS Document Writer"
    # Or set the PDF printer
    # ps.printer_name = "Adobe PDF"

    # Set PageSize(if required)
    pgs.paper_size = drawing.printing.PaperSize("A4", 827, 1169)

    # Set PageMargins(if required)
    pgs.margins = drawing.printing.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()