Get started with Python
This guide walks you through the steps to use the Toolbox add-on in a sample project with Python.
The Python interface is implemented as a wrapper around the Toolbox add-on C API. For more details about specific functions and properties, review the C API reference documentation.
Request trial or full license
Unlike the Pdftools SDK or the Pdftools SDK Shell Tool, the Toolbox add-on requires a license key for both evaluation and full use. To request a license key, follow these steps:
- Reach out to the sales team through the Contact page and mark the Toolbox add-on as the product of your interest for a trial license.
If you already have a license key and you need to copy it, review Find the license key.
Prerequisites
The Toolbox add-on for Python requires Python 3.6 or higher.
Getting started with a sample project
Learn how to use the Toolbox add-on using a Python sample project and convert a PDF file to an image.
Download and run the sample
-
Download a sample project, and then unzip the file.
-
Navigate to the root directory of the unzipped sample project, where
add_text.py
resides. -
To add the text string "Hello World!" to the first page of the PDF file
BlankNoneNoTP.pdf
, run:python ./add_text.py BlankNoneNoTP.pdf "Hello World!" output.pdf
noteRun
python3
instead on systems where only Python 3 is installed andpython
is not aliased.
The code sample takes:
- The input and output files represented as a file name, a file path with the file name, or the output directory.
- Both file paths (input and output) can be relative or absolute.
Review the following snippet with a placeholder and compare it to the last step of the previous procedure:
python ./add_text.py INPUT_FILE OUTPUT_FILE
Integrate the Toolbox add-on into your application
Integrate and initialize the Toolbox add-on into your application by following the instructions in the next sections.
Add the Toolbox add-on to your project
-
Install the Python package:
pip install https://pdftools-public-downloads-production.s3.eu-west-1.amazonaws.com/productkits/PDFSDKXT/latest/pdftools_toolbox-latest.tar.gz
-
From the Toolbox add-on code samples page, download any Python code sample.
-
Unzip a code sample and review the
README.md
file with usage details. Every code sample includes aREADME.md
with different usage instructions. -
Import the following packages in the header of your Python code:
from ctypes import *
from pdftools_toolbox.py_ctypes import *
from pdftools_toolbox.streams import StreamDescriptor
Initialize the Toolbox add-on
To initialize the Toolbox add-on with your license key, follow these steps:
-
Copy your license key. For more information, review Find the license key.
-
Replace the
<PDFSDK,V1,include-your-key-here>
in the following function with the value of your license key:# Initialize library
initialize()
# Optional: Without a license key the Toolbox add-on returns watermarked results.
# Initialize the Toolbox add-on with a license key to remove the watermark.
# The Toolbox add-on returns an error if you use an invalid license key.
if sdk_initialize("<PDFSDK,1,insert-license-key-here>", None) == 0:
print_error_message("Failed to set the license key.")
exit(1)Use the license key in the same format as you copied it. Include the less-than (
<
) and greater-than (>
) signs.
Without a valid license key the Toolbox add-on returns an error. Get in touch with the Pdftools sales team through the Contact page to get a full license.
Uninitialize the Toolbox add-on
After processing files with the Toolbox add-on, call the Uninitialize
method to unload the library correctly:
# Uninitialize library
uninitialize();
Download other sample projects that show you how to implement specific use cases with the Toolbox add-on using Python. Review the Toolbox add-on code samples page.