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 (2 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 theAdd References and Set a Licensetutorial, before working on theCreate Documents with Document Writers - Pythontutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theAdd References and Set a License - Pythontopic.

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 toFiles 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 toSetting 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 theSolution Explorer, openProject-Name.py. Add the following statements to the top ofProject-Name.py.

from pythonnet import *import clrimport sys# Set the search path for the LEADTOOLS DLLssys.path。append(r"C:\LEADTOOLS22\Bin\net")clr.AddReference("Leadtools")clr.AddReference("Leadtools.Codecs")clr.AddReference("Leadtools.Document")from Leadtools import *from Leadtools.Codecs import *from Leadtools.Document import *from Leadtools.Document.Writer import *from System.IO import *

Add a new method namedCreatePdfDocument(). Call this new method inside themain()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 CreatePdfDocument():codecs = RasterCodecs()dir = r"C:\LEADTOOLS22\Resources\Images"pageNumber = 1pdfFiles = Directory.GetFiles(dir ,"*.pdf")docFormat = DocumentFormat.PdfoutFile = Path.Combine(dir,"DocumentWriters."+ DocumentWriter.GetFormatFileExtension(docFormat))codecs.Options.RasterizeDocument.Load.Resolution = 300docWriter = DocumentWriter()pdfOptions = docWriter.GetOptions(docFormat)pdfOptions.DocumentType = PdfDocumentType.PdfApdfOptions.ImageOverText = TruedocWriter.SetOptions(docFormat, pdfOptions)# Create a new PDF documentdocWriter.BeginDocument(outFile, docFormat)# Add the pagesforfilein pdfFiles:page = DocumentWriterSvgPage()page.SvgDocument = codecs.LoadSvg(file, pageNumber, None)if(pdfOptions.ImageOverText):# If we are using image/text, then load the overlay raster imagepage.Image = codecs.Load(file, pageNumber)# Add the page to the created PDF documentdocWriter.AddPage(page)print(f"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}\n")# Dispose resourcesif(page.SvgDocument != None):page.SvgDocument.Dispose()if(page.Image != None):page.Image.Dispose()# Finalized document to diskdocWriter.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 afterpdfFilesis created:

pdfStreams = []fori in range(pdfFiles.Length):pdfStreams.append(i)fori in range(pdfFiles.Length):pdfData = File.ReadAllBytes (pdfFiles[我])pdfStream = MemoryStream(pdfData)pdfStreams[i] = pdfStream

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

forstream in pdfStreams:page = DocumentWriterSvgPage()page.SvgDocument = codecs.LoadSvg(stream, pageNumber, None)if(pdfOptions.ImageOverText):# If we are using image/text, then load the overlay raster imagepage.Image = codecs.Load(stream, pageNumber)# Add the page to the created PDF documentdocWriter.AddPage(page)print(f"Added page {pageNumber} from Stream {pdfStreams.index(stream)}\n")# Dispose resourcesif(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:

outStream = MemoryStream()docWriter.BeginDocument(outStream, docFormat)

Run the Project

Run the project by pressingF5, or by selectingDebug -> 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 theDocumentWriter,PdfDocumentOptions, andDocumentWriterSvgPageclasses.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2023LEAD Technologies, Inc.All Rights Reserved.