Custom Python wrapper for PDF library integration

There are three ways to use Apryse with Python:

  • Use pip - the precompiled library for Python 2.7.x.
  • Use pip - the precompiled library for Python 3.x.
  • (less common) Use PDFNet bindings to build your own wrappers.

This guide will help you get started by building your own wrapper. You can find more information about using the precompiled library

Custom Python wrapper & Windows PDF library integration

This guide will help you run Apryse samples and integrate a free trial of the Apryse SDK into Python applications on Windows. Your free trial includes unlimited trial usage and support from solution engineers.

Prerequisites

sh

1git clone https://github.com/PDFTron/PDFNetWrappers.git
  • Apryse's Python PDF library for Windows:

Make sure Python, cmake and SWIG are all added to your path environment variable.

Run Apryse SDK in production

A commercial license key is required for use in a production environment. Please contact sales to purchase a commercial key or if you need any other license key assistance.

Keep your commercial license key confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

Initial setup

1. Extract the PDFNetWrappersWinXX folder from the .zip file. This guide will assume it has been extracted to the current user's Desktop.

2. From the extracted folder, copy the Lib and Headers folders and paste them into PDFNetWrappers-master/PDFNetC/

3. Create a new folder called Build inside the PDFNetWrappers folder and enter it.This is what the current directory structure should look like:

Apryse Docs Image

4. Open a Command Prompt here and run the following:

sh

1cmake -D BUILD_PDFNetPython=ON ..

This will choose a default Visual Studio version to build the projects for. Wait for the bindings to finish. The output should end with something like this:

sh

1-- Build files have been written to: C:/Users/User_Name/Desktop/PDFNetWrappers-master/Build

If you see error messages during this process, see the Troubleshooting section.You should have the following files in your Build directory:

Apryse Docs Image

5. Open the generated PDFNetLanguageBindings.sln with Visual Studio.

6. Change the build mode from Debug to Release, make sure the appropriate target version (x86 or x64) is selected and build the solution.

Release configuration only

There is no Debug version of the PDFNet library available.

x64 target missing? Otherwise skip to Step 11.

Step 7 to step 10 is only required if the x64 target build option is not available. These steps will create the x64 build target and manually copy the dynamic link library output for installation.

7. When using the PDFNetC64 (64-bit) library files then change the build configuration to x64 in the Build dropdown by choosing Configuration Manager....Under Active Solution Platform, Select <New...> and choose x64 if x64 is not displayed as a platform.Click ok then close the modal to exit.

8. Right-click project PDFNetPython within Solution Explorer and choose Properties.Under Linker, select Command Line and change X86 to X64.Click ok to exit the modal.

9. Right-click and build the PDFNetPython project.This will build the Python library files.

10. Copy the _PDFNetPython.pyd dynamic link library file to the build\lib\Release folder using the command line below or manually from Windows explorer.This will ensure the Python module which communicates through the dynamic link library can be located for installation.

sh

1copy ".\x64\Release\_PDFNetPython.pyd" ".\lib\Release"

11. When this is done, select and build the INSTALL project.This will install PDFNetPython lib files to the PDFNetWrappers\PDFNetC\lib folder where it can be located by the samples.

12. Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.

If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

The bindings should be successfully built. You can now run the samples to test out the Apryse SDK.

Run the samples

Find and enter the Samples folder (PDFNetWrappers-master/Samples). Here you can find sample code for a large number of features supported by the Apryse SDK.

The output files from all tests are stored in Samples/TestFiles/output/

Run 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.
Apryse Docs Image

Run all samples

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

Press any key when a sample ends to start the next one.

Apryse Docs Image

Integrate into your application

This is what we call the "Apryse Hello World" application. It is easy to integrate the rest of Apryse 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:To test that your code works, run the code using a shell in the HelloWorld folder using:Once you have successfully run this, you should see an output file in the working directory of this program.

Python

1# You can use the following two lines to use PDFNetPython
2# in your solution from anywhere on your system
3# so long as you have relative path
4import site
5site.addsitedir("../../Lib")
6import sys
7
8# This is the most important packages to import
9# for basic document manipulation.
10from PDFNetPython import *
11
12def main():
13
14 # You need to initialize the PDFNet library
15 # Before calling any PDF related methods
16 PDFNet.Initialize("YOUR_APRYSE_LICENSE_KEY")
17
18 # This example creates a new document
19 # and a new page, then adds the page
20 # in the page sequence of the document
21 doc = PDFDoc()
22
23 page1 = doc.PageCreate()
24 doc.PagePushBack(page1)
25
26 # We save the document in a linearized
27 # format which is the most popular and
28 # effective way to speed up viewing PDFs
29 doc.Save(("linearized_output.pdf"), SDFDoc.e_linearized)
30
31 doc.Close()
32
33if __name__ == "__main__":
34 main()

sh

1python.exe -u HelloWorld.py

Next step

Troubleshooting

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

CMake Process finding incorrect version
Setting specific versions of Python to use for cmake.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales