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 |
|
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.
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:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Document.dll
Leadtools.Document.Writer.dll
For a complete list of which DLL files are required for your application, refer toFiles to be Included With Your Application.
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:
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 clr
import sys
# Set the search path for the LEADTOOLS DLLs
sys.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 = 1
pdfFiles = Directory.GetFiles(dir ,"*.pdf")
docFormat = DocumentFormat.Pdf
outFile = Path.Combine(dir,"DocumentWriters."+ DocumentWriter.GetFormatFileExtension(docFormat))
codecs.Options.RasterizeDocument.Load.Resolution = 300
docWriter = DocumentWriter()
pdfOptions = docWriter.GetOptions(docFormat)
pdfOptions.DocumentType = PdfDocumentType.PdfA
pdfOptions.ImageOverText = True
docWriter.SetOptions(docFormat, pdfOptions)
# Create a new PDF document
docWriter.BeginDocument(outFile, docFormat)
# Add the pages
forfilein pdfFiles:
page = DocumentWriterSvgPage()
page.SvgDocument = codecs.LoadSvg(file, pageNumber, None)
if(pdfOptions.ImageOverText):
# If we are using image/text, then load the overlay raster image
page.Image = codecs.Load(file, pageNumber)
# Add the page to the created PDF document
docWriter.AddPage(page)
print(f"Added page {pageNumber} 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
docWriter.EndDocument()
print("PDF document saved successfully!")
To load the images from memory stream instead of file locations, first create the array of input memory streams afterpdfFiles
is 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 image
page.Image = codecs.Load(stream, pageNumber)
# Add the page to the created PDF document
docWriter.AddPage(page)
print(f"Added page {pageNumber} from Stream {pdfStreams.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:
outStream = MemoryStream()
docWriter.BeginDocument(outStream, docFormat)
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.
This tutorial showed how to create documents using the Document Writers. It also covered how to use theDocumentWriter
,PdfDocumentOptions
, andDocumentWriterSvgPage
classes.