Some test text!

menu
search

Python

Welcome to PDFTron. This guide will help you run PDFTron samples and integrate PDFTron SDK into Python projects on Windows.

There are two ways to use PDFTron with Python:

  • Use the precompiled library for Python 2.7.
  • Use PDFNet Wrappers to generate your own bindings.

This guide will focus on the precompiled version of Python. You can find more information about PDFNetWrappers and generating your own bindings here.

linkPrerequisites

  1. Python 2.7.

    If you have multiple Python versions installed see the Troubleshooting.

  2. PDFTron SDK for Windows (64-bit or 32-bit).

linkSetup

  1. Make sure that Python 2.7 has been added to your path environment variable and that the executable name is python.exe.

  2. Extract the folder from the .zip file.

    This article uses PDFNET_BASE as the path into the PDFNetDotNet4 folder that you extracted.

    PDFNET_BASE = path/to/extraction/folder/PDFNetDotNet4/

linkRun Samples

Find and enter the Samples folder (PDFNetC(64)/Samples). Here you can find sample code for a large number of features supported by the PDFTron SDK.

Running all samples

Find runall_python.bat in the samples folder and double click on it to run it. The results should appear on a cmd window.

AddImageTest output

Running a specific sample

  1. Find the sample you want to run and navigate into the PYTHON folder inside.

  2. Find RunTest.bat and run it. The results should appear on a cmd window.

AddImageOutput

linkIntegrate PDFTron into your Python project

This is called the "PDFTron Hello World" application. It is easy to integrate the rest of PDFTron SDK if you are able to open, save and close a PDFDoc.

  1. Create a new folder in Samples by the name HelloWorld.

  2. In the HelloWorld folder, create a new file called HelloWorld.py, open and edit it using your favorite text editor.

  3. Insert the following to your file:

    # You can use the following two lines to use PDFNetPython
    # in your solution from anywhere on your system
    # so long as you have relative path
    import site
    site.addsitedir("../../Lib")
    import sys
    
    # This is the most important packages to import
    # for basic document manipulation.
    from PDFNetPython import *
    
    def main():
    
        # You need to initialize the PDFNet library 
        # Before calling any PDF related methods
        PDFNet.Initialize()
    
        # This example creates a new document
        # and a new page, then adds the page
        # in the page sequence of the document
        doc = PDFDoc()
    
        page1 = doc.PageCreate()
        doc.pagePushBack(page1)
    
        # We save the document in a linearized
        # format which is the most popular and 
        # effective way to speed up viewing PDFs
        doc.Save(("linearized_output.pdf"), SDFDoc.e_linearized)
    
        doc.close()
    
    if __name__ == "__main__":
        main()

    To test that your code works, run the code using a shell in the HelloWorld folder using:

    python.exe -u HelloWorld.py

    Once you have successfully run this, you should see an output file in the working directory of this program.

You can learn about and add more features by reading our guides and samples.

linkTroubleshooting

Multiple versions of Python
More information for conflict resolution between multiple Python installations.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close