Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 5, 2026 19:52
Show Gist options
  • Select an option

  • Save aspose-com-gists/cde11fdbca708788e3211f3c0e413b37 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/cde11fdbca708788e3211f3c0e413b37 to your computer and use it in GitHub Desktop.
Add Barcode to PDF in Java

Add Barcode to PDF in Java

Learn how to add a barcode to a PDF in Java with Aspose.PDF for Java. This guide walks you through the required environment, Maven setup, code integration, and provides a working example, enabling you to embed Code128 barcodes quickly and efficiently.

Read the full guide here: https://blog.aspose.com/pdf/add-barcode-to-pdf-in-java/

import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.Barcode;
import com.aspose.pdf.BarcodeType;
import com.aspose.pdf.PlacementType;
public class AddBarcodeToPdf {
public static void main(String[] args) {
try {
// Load the existing PDF document
Document pdfDocument = new Document("input.pdf");
// Get the page where the barcode will be placed (first page)
Page page = pdfDocument.getPages().get_Item(1);
// Create a Code128 barcode with the desired text
Barcode barcode = new Barcode(page, BarcodeType.Code128, "1234567890");
// Configure barcode appearance
barcode.setXDimension(0.8f); // Width of the smallest bar
barcode.setBarHeight(20); // Height of the barcode
barcode.setPlacement(PlacementType.Floating);
barcode.setLeft(100); // X position on the page
barcode.setTop(500); // Y position on the page
// Add the barcode to the page's content
page.getParagraphs().add(barcode);
// Save the updated PDF document
pdfDocument.save("output.pdf");
// Clean up resources
pdfDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment