Burn Annotations to a LEADDocument - Python

This tutorial shows how to burn the annotations from an external XML file to a PDF document in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to burn annotations to a PDF file using the LEADDocument class in a Python application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python
IDE Visual Studio 2022
Runtime Target Python 3.10 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Burn Annotations to a LEADDocument - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following DLLs:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add LEADDocument and Burn Annotations Code

With the project created, and the license set, coding can begin.

In the Solution Explorer, open Project-Name.pyand place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Document") 
from Leadtools.Document import * 
LibraryLoader.add_reference("Leadtools.Document.Converter") 
from Leadtools.Document.Converter import * 
LibraryLoader.add_reference("Leadtools.Annotations.Rendering") 
from Leadtools.Annotations.Rendering import * 
from System.IO import * 
from System import * 

Add a new method called burn_annotations(document_file, ann_file) and call it inside the main() method under Support.set_license().

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
 
    pdf_file = r"FILE PATH TO PDF FILE" 
    ann_file = r"FILE PATH TO ANNOTATIONS XML FILE" 
 
    burn_annotations(pdf_file, ann_file) 

Add the below code to create a new virtual document, add a PDF document to the virtual document, burn annotations, and export the file using the Document Converter.

def burn_annotations(document_file, ann_file): 
 
    virtual_document = DocumentFactory.Create(CreateDocumentOptions()) 
    ann_uri = Uri(ann_file) 
    load_options = LoadDocumentOptions() 
    load_options.AnnotationsUri = ann_uri 
 
    child_document = DocumentFactory.LoadFromFile(document_file, load_options) 
     
    virtual_document.Pages.Add(child_document.Pages[0]) 
 
    doc_converter = DocumentConverter() 
    doc_converter.SetDocumentWriterInstance(Leadtools.Document.Writer.DocumentWriter()) 
    doc_converter.SetAnnRenderingEngineInstance(AnnDrawRenderingEngine()) 
 
    job_data = DocumentConverterJobData() 
    job_data.AnnotationsMode = DocumentConverterAnnotationsMode.Embed 
    job_data.Document = virtual_document 
    job_data.OutputDocumentFileName = r"C:\LEADTOOLS22\Resources\Images\BurnAnnotationsDoc.pdf" 
    job_data.DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf 
    
    job = doc_converter.Jobs.CreateJob(job_data) 
    doc_converter.Jobs.RunJob(job) 
    for error in job.Errors: 
        print("There was an error", error.Error) 

Handling Streams

If you would like to load a document using memory stream, then add the following code to the top of the BurnAnnotations() method:

    stream = File.OpenRead(document_file) 
    options = LoadDocumentOptions() 
    options.AnnotationsUri = Uri(ann_file) 
    document = DocumentFactory.LoadFromStream(stream, options) 
    print(document.DocumentId) 
    print("Document loaded") 
    child_document = document 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application creates a virtual document, adds a PDF document to the virtual document, burns annotations to it, and exports the document to file.

Wrap-up

This tutorial showed how to create a new virtual document and burn annotations to it. Also, it covered how to use the LEADDocument, LoadDocumentOptions, and DocumentConverter classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.