Create Documents with Document Writers - Python

This tutorial shows how to create a new blank PDF document and add pages to it from existing PDF files in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to create a new PDF document and add pages to it in a Python Console application using LEADTOOLS Document Writers.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
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 Create Documents with Document Writers - 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 - Python topic.

If you do not have that project, follow the steps in the relevant tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following .NET 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:

Create a New PDF Document and Add Pages Code

With the project created, the references added, 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.Codecs") 
from Leadtools.Codecs import * 
LibraryLoader.add_reference("Leadtools.Document") 
from Leadtools.Document import * 
from Leadtools.Document.Writer import * 
from System.IO import * 

Add a new method named create_pdf_document(). Call this new method inside the main() method under the set license code. Add the below code to create a new PDF file and add to it the first page of each PDF in a given directory.

def create_pdf_document(): 
    codecs = RasterCodecs() 
    dir = r"C:\LEADTOOLS22\Resources\Images" 
    page_number = 1 
    pdf_files = Directory.GetFiles(dir , "*.pdf") 
 
    doc_format = DocumentFormat.Pdf 
    out_file = Path.Combine(dir, "DocumentWriters." + DocumentWriter.GetFormatFileExtension(doc_format)) 
 
    codecs.Options.RasterizeDocument.Load.Resolution = 300 
 
    doc_writer = DocumentWriter() 
 
    pdf_options = doc_writer.GetOptions(doc_format)  
    pdf_options.DocumentType = PdfDocumentType.PdfA 
    pdf_options.ImageOverText = True 
    doc_writer.SetOptions(doc_format, pdf_options) 
 
    # Create a new PDF document 
    doc_writer.BeginDocument(out_file, doc_format) 
 
    # Add the pages 
    for file in pdf_files: 
        page = DocumentWriterSvgPage() 
        page.SvgDocument = codecs.LoadSvg(file, page_number, None) 
        if (pdf_options.ImageOverText): 
            # If we are using image/text, then load the overlay raster image 
            page.Image = codecs.Load(file, page_number) 
 
        # Add the page to the created PDF document 
        doc_writer.AddPage(page) 
        print(f"Added page {page_number} from {Path.GetFileNameWithoutExtension(file)}\n") 
 
        # Dispose resources 
        if (page.SvgDocument != None): 
            page.SvgDocument.Dispose() 
        if (page.Image != None): 
            page.Image.Dispose() 
 
    # Finalized document to disk 
    doc_writer.EndDocument() 
    print("PDF document saved successfully!") 

Handling Streams

To load the images from memory stream instead of file locations, first create the array of input memory streams after pdf_files is created:

pdf_streams = [] 
for i in range(pdf_files.Length): 
    pdf_streams.append(i) 
for i in range(pdf_files.Length): 
    pdf_data = File.ReadAllBytes(pdf_files[i]) 
    pdf_stream = MemoryStream(pdf_data) 
    pdf_streams[i] = pdf_stream 

Then use the code below to add pages from the streams.

for stream in pdf_streams: 
        page = DocumentWriterSvgPage() 
        page.SvgDocument = codecs.LoadSvg(stream, page_number, None) 
        if (pdf_options.ImageOverText): 
            # If we are using image/text, then load the overlay raster image 
            page.Image = codecs.Load(stream, page_number) 
 
        # Add the page to the created PDF document 
        doc_writer.AddPage(page) 
        print(f"Added page {page_number} from Stream {pdf_streams.index(stream)}\n") 
 
        # Dispose resources 
        if (page.SvgDocument != None): 
            page.SvgDocument.Dispose() 
        if (page.Image != None): 
            page.Image.Dispose() 

To create the document in a memory stream instead of a file location, use the code below:

out_stream = MemoryStream() 
doc_writer.BeginDocument(out_stream, doc_format) 

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 new PDF file and adds the first page of each PDF file in a given directory using SVG and Document Writers.

Wrap-up

This tutorial showed how to create documents using the Document Writers. It also covered how to use the DocumentWriter, PdfDocumentOptions, and DocumentWriterSvgPage 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.