Some test text!


Python cos/SDF low-level API to edit PDF files

Sample Python code for editing an existing PDF document at the object level by using the PDFTron SDK Cos/SDF low-level API. Learn more about our PDF Editing & Manipulation Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

# Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.

import site
import sys
from PDFNetPython import *

# This sample illustrates how to use basic SDF API (also known as Cos) to edit an 
# existing document.

def main():
    # Relative path to the folder containing the test files.
    input_path = "../../TestFiles/"
    output_path = "../../TestFiles/Output/"
    print("Opening the test file...")
    # Here we create a SDF/Cos document directly from PDF file. In case you have 
    # PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
    doc = SDFDoc(input_path + "fish.pdf")
    print("Modifying info dictionary, adding custom properties, embedding a stream...")
    trailer = doc.GetTrailer()  # Get the trailer
    # Now we will change PDF document information properties using SDF API
    # Get the Info dictionary
    itr = trailer.Find("Info")
    info = Obj()
    if itr.HasNext():
        info = itr.Value()
        # Modify 'Producer' entry
        info.PutString("Producer", "PDFTron PDFNet")
        # Read title entry (if it is present)
        itr = info.Find("Author")
        if itr.HasNext():
            oldstr = itr.Value().GetAsPDFTest()
            info.PutText("Author", oldstr + "- Modified")
            info.PutString("Author", "Me, myself, and I")
        # Info dict is missing.
        info = trailer.PutDict("Info")
        info.PutString("Producer", "PDFTron PDFNet")
        info.PutString("Title", "My document")
    # Create a custom inline dictionary within Info dictionary
    custom_dict = info.PutDict("My Direct Dict")
    custom_dict.PutNumber("My Number", 100)     # Add some key/value pairs
    custom_dict.PutArray("My Array")
    # Create a custom indirect array within Info dictionary
    custom_array = doc.CreateIndirectArray()
    info.Put("My Indirect Array", custom_array)    # Add some entries
    # Create indirect link to root
    # Embed a custom stream (file mystream.txt).
    embed_file = MappedFile(input_path + "my_stream.txt")
    mystm = FilterReader(embed_file)
    custom_array.PushBack( doc.CreateIndirectStream(mystm) )
    # Save the changes.
    print("Saving modified test file...")
    doc.Save(output_path + "sdftest_out.pdf", 0, "%PDF-1.4")
    print("Test Completed")
if __name__ == '__main__':

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