Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save aspose-com-gists/f35622b4c08c86abf07eed0997570b69 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 with Aspose.PDF for Java. The tutorial covers SDK installation, creating and customizing a Barcode object, placing it on a page, and saving the file. Includes a complete code sample and best-practice tips for integration.

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

import com.aspose.pdf.*;
public class AddBarcodeToPdf {
public static void main(String[] args) {
// Load the existing PDF document
Document pdfDocument = new Document("input.pdf");
// Get the first page of the PDF
Page page = pdfDocument.getPages().get_Item(1);
// Create a barcode object
Barcode barcode = new Barcode();
barcode.setCodeText("9876543210");
barcode.setType(BarcodeType.Code128);
// Optional: adjust size and appearance
barcode.setBarHeight(50);
barcode.setXDimension(0.8);
barcode.setBarColor(Color.getBlue());
// Add the barcode to the page
page.getParagraphs().add(barcode);
// Save the updated PDF document
pdfDocument.save("output.pdf");
// Release resources
pdfDocument.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment