Embedding barcodes directly into PDF files is a common requirement for inventory tracking, ticketing, and secure document workflows. Aspose.PDF for Python via .NET provides a robust SDK that simplifies add Barcode to PDF in Python. In this tutorial you will learn how to generate barcode images, place them on existing PDFs, and customize their appearance. By the end you will be able to programmatically add barcodes to single or multiple PDF documents with confidence.
How to Add Barcode to PDF in Python - Step by Step
- Install the required packages: Use pip to install Aspose.PDF and Aspose.BarCode.
pip install aspose-pdf pip install aspose-barcode-for-python-via-net - Generate the barcode image: Create a barcode using Aspose.BarCode. The generated image can be saved to a memory stream.
from aspose.barcode import generation # Generate the barcode image generator = generation.BarcodeGenerator(generation.EncodeTypes.CODE39_STANDARD, "1234567") generator.save(barcode_image, generation.BarCodeImageFormat.PNG) - Create the target PDF document: Open the existing PDF with Aspose.PDF.
import aspose.pdf as ap # Create a new PDF document and add a page document = ap.Document("input.pdf") page = document.pages.add() page.set_page_size(595, 842) - Insert the barcode image onto the desired page: Use an ImageStamp to place the barcode. Adjust position and size as needed.
# Add the barcode image at the specified coordinates page.add_image(barcode_image, ap.Rectangle(100, 650, 300, 750, True)) - Save the updated PDF (or loop for bulk processing): Persist changes or iterate over multiple PDFs for bulk add Barcode to PDF in Python.
document.save(output_pdf)
Embedding Barcode in PDF with Python - Complete Code Example
The following example demonstrates a full workflow that generates a barcode, embeds it into an existing PDF, and saves the result.
import aspose.pdf as ap
from aspose.barcode import generation
def add_barcode_to_new_pdf(output_pdf, barcode_image):
# Generate the barcode image
generator = generation.BarcodeGenerator(generation.EncodeTypes.CODE39_STANDARD, "1234567")
generator.save(barcode_image, generation.BarCodeImageFormat.PNG)
# Create a new PDF document and add a page
document = ap.Document()
page = document.pages.add()
page.set_page_size(595, 842)
# Add the barcode image at the specified coordinates
page.add_image(barcode_image, ap.Rectangle(100, 650, 300, 750, True))
# Save the resulting PDF document
document.save(output_pdf)
if __name__ == "__main__":
add_barcode_to_new_pdf("AddBarcodeToNewPDF.pdf", "barcode.png")
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Aspose.PDF for Python via .NET - Prerequisites and Setup
To start using the SDK, ensure you have Python 3.6+ and the .NET runtime installed. Then install the packages via pip:
pip install aspose-pdf aspose-barcode
You can also download the latest binaries from the download page. No additional licensing code is required for evaluation, but a valid license is needed for production use (see the temporary license page and the pricing page).
Read Barcode from PDF Document using Python
We can recognize any barcode image embedded on any of the pages of the PDF document using the resources.images collection of a page. It allows extracting images from the PDF, and then we will read the barcode information from the extracted image. We can read barcodes from the PDF documents by following the steps given below:
- Firstly, load the input PDF document using the Document class.
- Next, select the target page and get its resources.images collection.
- Then, save each embedded image to a file in a loop.
- Next, create an instance of the BarCodeReader class with the image path and DecodeType as arguments.
- After that, call the read_bar_codes() method to get the recognition results.
- Finally, loop through the results and show the barcode information.
The following code example demonstrates how to read a barcode image from a PDF document using Python.
import aspose.pdf as ap
from aspose.barcode import recognition
from io import FileIO
def read_barcode_from_pdf(input_pdf, extracted_image):
# Load the input PDF document
document = ap.Document(input_pdf)
page = document.pages[1]
# Extract the first image from the page resources
x_image = page.resources.images[1]
with FileIO(extracted_image, "wb") as image_stream:
x_image.save(image_stream)
# Read the barcode from the extracted image
reader = recognition.BarCodeReader(extracted_image, recognition.DecodeType.CODE39_STANDARD)
for result in reader.read_bar_codes():
print(f"Codetext found: {result.code_text}")
print(f"Symbology: {result.code_type_name}")
if __name__ == "__main__":
read_barcode_from_pdf("AddBarcodeToExistingPDF.pdf", "extracted_barcode.png")
Codetext found: 1234567
Symbology: Code39Standard
Conclusion
Adding a barcode to PDF in Python becomes straightforward with Aspose.PDF for Python via .NET. The SDK’s powerful PDF manipulation features combined with Aspose.BarCode’s barcode generation let you implement a complete add Barcode to PDF workflow in just a few lines of code. Whether you are updating a single document or performing bulk add Barcode to PDF in Python for large batches, the library offers the performance and flexibility you need. Remember to acquire a proper license for production use; you can explore the affordable pricing options on the pricing page or obtain a temporary evaluation license from the temporary license page.
FAQs
How do I add a barcode to an existing PDF in Python?
Use Aspose.PDF to load the PDF, generate the barcode with Aspose.BarCode, and embed it using anadd_image(). The full process is illustrated in the code example above.What is the recommended approach to bulk add Barcode to PDF in Python?
Loop through your PDF collection, reuse a singleBarcodeGenerator, and work with streams. Apply the performance tips from the “Performance Considerations” section to keep memory usage low.Is there a way to customize the barcode size and position programmatically?
Absolutely. Adjust thewidth,height,x,y, androtationproperties of thebarcode.
