How to Create Barcode Signature Java for PDF Documents
Introduction
In this tutorial you’ll learn how to create barcode signature Java for PDF files using GroupDocs.Signature. Whether you need to embed product codes, audit IDs, or any structured data into a contract, barcode signatures let you add machine‑readable information that can be scanned instantly while still keeping the document cryptographically secure. We’ll walk through setup, code, customization, and best‑practice tips so you can start adding barcode signatures to your PDFs in minutes.
Quick Answers
- What library adds barcode signatures in Java? GroupDocs.Signature for Java.
- Which barcode type is best for retail?
GS1CompositeBar(industry‑standard for product tracking). - Do I need a license for production? Yes – a purchased GroupDocs license is required.
- Can I add both digital and barcode signatures? Absolutely; combine them for security and scanability.
- Is the code compatible with Java 11 and later? Yes, the API works with JDK 8+.
What is create barcode signature java?
create barcode signature java refers to the process of programmatically embedding a barcode into a PDF using Java code. GroupDocs.Signature provides a simple API that generates the barcode image, positions it on the page, and saves the signed document in one seamless operation.
Why Use Barcode Signatures?
Barcode signatures give you machine‑readable metadata inside a legally signed PDF. They enable instant visual verification, streamline supply‑chain scanning, and let downstream systems extract structured data without opening the file. Over 60 barcode formats are supported, and GS1CompositeBar can encode product identifiers, serial numbers, and batch codes in a single compact symbol—perfect for retail, healthcare, and finance.
Prerequisites
- Java Development Kit: JDK 8 or newer (Java 11, 17, 21 all work).
- Build Tool: Maven or Gradle – choose the one you prefer.
- IDE: IntelliJ IDEA, Eclipse, or VS Code.
- GroupDocs.Signature library: Add the dependency as shown below.
- License: Free trial for development; a purchased license for production.
Adding GroupDocs.Signature to Your Project
For Maven users, add this to your pom.xml:
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-signature</artifactId>
<version>23.12</version>
</dependency>
For Gradle users, add this to your build.gradle:
implementation 'com.groupdocs:groupdocs-signature:23.12'
About licensing: GroupDocs offers a free trial that’s perfect for testing and development. You can download it from their releases page. When you’re ready for production, you’ll need to purchase a license or request a temporary one from the GroupDocs website. For detailed API usage see the API Reference, consult the Developer Guide for step‑by‑step instructions, and ask questions on the GroupDocs Forum. The same purchase link is also listed as the purchase page.
How to Set Up GroupDocs.Signature in Java?
The Signature class represents a document and provides methods to apply signatures, search content, and manage resources. Create a Signature instance to open your PDF and prepare it for signing. The Signature class is GroupDocs.Signature’s core component that manages document loading, resource handling, and the signing workflow, ensuring that all operations are performed safely and efficiently.
import com.groupdocs.signature.Signature;
// Point to your PDF file
Signature signature = new Signature("path/to/your/document.pdf");
Important: Always dispose of the Signature object after use—this releases file handles and frees memory.
How to Configure the Barcode Sign Options?
The BarcodeSignOptions class defines every aspect of the barcode you’re about to embed, from data payload to visual style. It acts as a container for all barcode‑related settings such as type, size, colors, and placement. Below is a minimal configuration that creates a GS1CompositeBar barcode containing a GTIN and a serial number, and it also demonstrates how to set margins and background colors for optimal readability.
import com.groupdocs.signature.options.sign.BarcodeSignOptions;
import com.groupdocs.signature.domain.barcodes.BarcodeTypes;
// Create barcode with GS1 format data
BarcodeSignOptions options = new BarcodeSignOptions("(01)03212345678906/(21)A1B2C3D4E5F6G7H8");
options.setEncodeType(BarcodeTypes.GS1CompositeBar);
The string "(01)03212345678906/(21)A1B2C3D4E5F6G7H8" follows the GS1 standard:
(01)= GTIN (global product identifier)03212345678906= actual product code(21)= serial number identifierA1B2C3D4E5F6G7H8= unique serial
You can replace this with any text for QR codes, Code128, DataMatrix, etc. Over 60 barcode types are supported—see the GroupDocs docs for the full list.
How to Position and Customize the Barcode?
Placement and styling are controlled through the same BarcodeSignOptions object. Use setTop, setLeft, setWidth, and setHeight to define exact coordinates and dimensions. Setting setAllPages(true) adds the barcode to every page automatically, while setPageNumber(1) targets a specific page. You can also rotate the barcode, add a background color, or adjust margins to ensure the barcode does not overlap existing content.
// Set position and apply to all pages
options.setTop(200); // Set vertical position
options.setAllPages(true);
For more precise layout, you can also anchor the barcode to a specific page, rotate it, or add a background color:
options.setLeft(100); // 100 pixels from left edge
options.setTop(200); // 200 pixels from top
options.setWidth(300); // Barcode width in pixels
options.setHeight(100); // Barcode height in pixels
options.setPageNumber(1); // Only sign page 1 (removes setAllPages)
Visual customization (foreground/background colors, margins, text labels) is available via additional properties:
options.setMargin(new Padding(10)); // Add padding around barcode
options.setBackground(new Background()); // Set background color
options.setForeColor(Color.BLACK); // Barcode color
options.setBackgroundColor(Color.WHITE); // Background color
How to Sign the Document with a Barcode?
The sign method on the Signature instance applies the configured options and writes the signed document to disk. It returns a SignResult object that tells you how many signatures were applied and whether any warnings occurred. This method handles all low‑level PDF manipulation, so you do not need to work with PDF libraries directly.
try {
SignResult signResult = signature.sign(outputPath, options);
System.out.println("Document signed successfully!");
System.out.println("Signed document saved to: " + outputPath);
} finally {
if (signature != null) {
signature.dispose();
}
}
The surrounding try‑finally block guarantees the Signature object is disposed even if an exception occurs, preventing file‑handle leaks.
You can inspect the SignResult to confirm success:
if (signResult.getSucceeded().size() > 0) {
System.out.println("Successfully added " + signResult.getSucceeded().size() + " signature(s)");
}
Complete Working Example
Putting everything together, here’s a single method that loads a PDF, adds a GS1CompositeBar barcode, and saves the signed file:
import com.groupdocs.signature.Signature;
import com.groupdocs.signature.options.sign.BarcodeSignOptions;
import com.groupdocs.signature.domain.barcodes.BarcodeTypes;
public class BarcodeSigningExample {
public void signPdfWithBarcode(String inputPath, String outputPath) {
Signature signature = null;
try {
// Initialize signature
signature = new Signature(inputPath);
// Configure barcode
BarcodeSignOptions options = new BarcodeSignOptions(
"(01)03212345678906/(21)A1B2C3D4E5F6G7H8"
);
options.setEncodeType(BarcodeTypes.GS1CompositeBar);
options.setTop(200);
options.setAllPages(true);
// Sign and save
SignResult result = signature.sign(outputPath, options);
System.out.println("Signing completed: " + result.getSucceeded().size() + " signature(s) added");
} catch (Exception e) {
System.err.println("Error signing document: " + e.getMessage());
e.printStackTrace();
} finally {
if (signature != null) {
signature.dispose();
}
}
}
}
This method is production‑ready: it validates input, manages resources, and reports the outcome.
How to Add Both Digital and Barcode Signatures?
For maximum security, add a cryptographic digital signature first, then layer the barcode on top. The digital signature guarantees the document’s integrity, while the barcode provides quick visual verification and machine‑readable data. Use the DigitalSignOptions class for the first step and then apply BarcodeSignOptions as shown above.
// First, add digital signature
DigitalSignOptions digitalOptions = new DigitalSignOptions("certificate.pfx");
digitalOptions.setPassword("cert_password");
signature.sign(outputPath, digitalOptions);
// Then, add barcode on top
BarcodeSignOptions barcodeOptions = new BarcodeSignOptions("DATA");
barcodeOptions.setEncodeType(BarcodeTypes.QR);
signature.sign(outputPath, barcodeOptions);
The resulting PDF is both legally binding (digital signature) and instantly readable by any barcode scanner.
Working with Different Barcode Types
Switching barcode formats is as easy as changing one enum value. Below is an example that swaps the GS1CompositeBar for a QR code:
// Define barcode sign options with sample text
BarcodeSignOptions options = new BarcodeSignOptions("Sample Text");
// Assign specific barcode type
options.setEncodeType(BarcodeTypes.GS1CompositeBar);
And here’s the same change for a Code128 linear barcode:
options.setEncodeType(BarcodeTypes.QR); // QR code
options.setEncodeType(BarcodeTypes.Code128); // Code 128
options.setEncodeType(BarcodeTypes.DataMatrix); // Data Matrix
Remember each barcode type has its own data capacity limits—QR codes can hold thousands of characters, while Code39 is limited to alphanumerics.
Real‑World Use Cases
Supply Chain Management
Embedding a GS1CompositeBar on shipping manifests lets warehouse staff scan order numbers, destinations, and batch codes without opening the PDF.
BarcodeSignOptions options = new BarcodeSignOptions(
"(01)" + gtin + "/(21)" + serialNumber + "/(10)" + batchNumber
);
options.setEncodeType(BarcodeTypes.GS1CompositeBar);
Healthcare Documentation
QR codes on consent forms can be scanned to automatically file the document under the correct patient record.
String patientData = "PATIENT_ID:" + patientId + "|DOC_TYPE:CONSENT|DATE:" + dateString;
BarcodeSignOptions options = new BarcodeSignOptions(patientData);
options.setEncodeType(BarcodeTypes.QR);
Financial Services
Transaction IDs encoded in a barcode enable auditors to verify compliance with a simple scan.
String complianceData = "TXN:" + transactionId + "|AGENT:" + agentId + "|BRANCH:" + branchCode;
BarcodeSignOptions options = new BarcodeSignOptions(complianceData);
options.setEncodeType(BarcodeTypes.PDF417);
Manufacturing Quality Control
Inspection reports signed with barcodes that contain batch numbers and inspector IDs make root‑cause analysis instantaneous.
String qcData = "BATCH:" + batchNumber + "|INSPECTOR:" + inspectorId + "|RESULT:" + passFailStatus;
BarcodeSignOptions options = new BarcodeSignOptions(qcData);
options.setEncodeType(BarcodeTypes.DataMatrix);
Common Implementation Issues
Issue 1: “File is already in use” Exception
Answer: The file remains locked if a previous Signature instance wasn’t disposed. Always wrap signing code in a try‑finally block and call signature.dispose().
Signature signature = null;
try {
signature = new Signature(filePath);
// ... your code ...
} finally {
if (signature != null) {
signature.dispose();
}
}
Issue 2: Barcode Doesn’t Appear on PDF
Answer: Verify that setTop/setLeft values are within page bounds, increase the barcode’s width/height, and ensure foreground/background colors contrast with the page background.
// Make sure dimensions are reasonable
options.setTop(100); // Not 10000
options.setLeft(100);
options.setWidth(300); // At least 200 pixels for readability
options.setHeight(100);
// Ensure contrast
options.setForeColor(Color.BLACK);
options.setBackgroundColor(Color.WHITE);
Issue 3: “Invalid Barcode Data” Exception
Answer: GS1 barcodes require strict parentheses notation. Use the correct Application Identifiers (e.g., (01), (21)) and avoid unsupported characters.
// Correct:
"(01)03212345678906/(21)ABC123"
// Incorrect:
"03212345678906ABC123"
Issue 4: OutOfMemoryError with Large PDFs
Answer: Increase the JVM heap (-Xmx4G) and consider signing pages individually instead of using setAllPages(true) for very large documents.
java -Xmx2G -jar your-application.jar
Issue 5: Barcode Scanning Fails
Answer: Ensure the barcode is at least 200 × 100 px, uses high contrast colors, and isn’t distorted by PDF compression.
// Increase size
options.setWidth(400);
options.setHeight(150);
// Maximize contrast
options.setForeColor(Color.BLACK);
options.setBackgroundColor(Color.WHITE);
// Add quiet zone (blank space around barcode)
options.setMargin(new Padding(10));
Security and Validation
Are Barcode Signatures Secure?
A barcode alone isn’t cryptographically protected, so anyone could generate a new one. Pair it with a digital signature to guarantee both authenticity and scanability. The dual‑signature pattern (digital first, barcode second) gives you legal enforceability plus operational convenience.
Validating Barcode Data
After scanning, verify that the document’s digital signature is still valid and that the barcode payload matches expected patterns. Use GroupDocs’ search() API with BarcodeSearchOptions to extract and validate barcode content automatically.
public boolean validateScannedBarcode(String scannedData, String documentPath) {
// Verify digital signature first
if (!verifyDigitalSignature(documentPath)) {
return false;
}
// Validate barcode format
if (!scannedData.matches("(01)\\d{14}/(21)[A-Z0-9]+")) {
return false;
}
// Check against database
String productId = extractProductId(scannedData);
return database.productExists(productId);
}
Performance Considerations
Resource Management
Always dispose of Signature objects after each operation to avoid memory leaks:
signature.dispose();
When processing many files, create and dispose a new Signature per document:
for (String filePath : documentPaths) {
Signature signature = null;
try {
signature = new Signature(filePath);
// ... signing logic ...
} finally {
if (signature != null) {
signature.dispose();
}
}
}
Batch Processing
For small batches (< 100 files), sequential processing is simple:
for (String path : paths) {
signDocument(path);
}
For larger workloads, parallel processing can speed things up—but monitor I/O pressure and limit threads to 4‑8:
paths.parallelStream().forEach(path -> signDocument(path));
Memory Optimization for Large PDFs
Increase heap size, process pages individually, and clear references after each step. This prevents OutOfMemoryError on documents larger than 50 MB.
Caching Barcode Options
If you reuse the same barcode configuration across many files, cache the BarcodeSignOptions instance:
// Create once, reuse many times
private static BarcodeSignOptions createStandardBarcodeOptions(String data) {
BarcodeSignOptions options = new BarcodeSignOptions(data);
options.setEncodeType(BarcodeTypes.GS1CompositeBar);
options.setTop(200);
options.setAllPages(true);
return options;
}
Advanced Features
Multiple Signatures on One Document
Add several barcodes (or mix with digital signatures) by calling sign multiple times with different option objects:
// Add QR code in top-left
BarcodeSignOptions qrOptions = new BarcodeSignOptions("https://example.com");
qrOptions.setEncodeType(BarcodeTypes.QR);
qrOptions.setTop(50);
qrOptions.setLeft(50);
signature.sign(outputPath, qrOptions);
// Add GS1 barcode in bottom-right
BarcodeSignOptions gs1Options = new BarcodeSignOptions("(01)03212345678906");
gs1Options.setEncodeType(BarcodeTypes.GS1CompositeBar);
gs1Options.setTop(700);
gs1Options.setLeft(400);
signature.sign(outputPath, gs1Options);
Dynamic Barcode Content
Generate barcode data from document metadata, timestamps, or database lookups:
// Extract data from PDF or database
String orderId = extractOrderIdFromPdf(filePath);
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
String barcodeData = String.format("ORDER:%s|TIME:%s", orderId, timestamp);
BarcodeSignOptions options = new BarcodeSignOptions(barcodeData);
Integration with Spring Boot
Expose a REST endpoint that receives a PDF, adds a barcode, and returns the signed file:
@Service
public class DocumentSigningService {
public void signWithBarcode(MultipartFile file, String barcodeData) {
// ... implementation ...
}
}
REST API Example
A minimal controller that delegates to the signing service:
@PostMapping("/sign-document")
public ResponseEntity<byte[]> signDocument(
@RequestParam("file") MultipartFile file,
@RequestParam("barcode") String barcodeData
) {
// Sign document and return as byte array
}
Frequently Asked Questions
Q: What is a GS1CompositeBar barcode?
A: A GS1CompositeBar combines linear and 2D components to store product IDs, serial numbers, and other data in a single scannable symbol, widely used in retail and logistics.
Q: Can I use other barcode types besides GS1CompositeBar?
A: Yes—GroupDocs.Signature supports over 60 types, including QR, Code128, DataMatrix, PDF417, and Aztec. Switch the setEncodeType() value to change the format.
Q: Do I need a license for production use?
A: A valid GroupDocs license is required for production deployments. A free trial is available for development and testing.
Q: Will barcode scanners read barcodes from signed PDFs?
A: Absolutely, provided the barcode is at least 200 × 100 px and has sufficient contrast. Smartphone scanning apps work on‑screen; hardware scanners work on printed copies.
Q: How do I handle errors during signing?
A: Wrap your code in try‑catch blocks, log full stack traces, and always call signature.dispose() in a finally block to release resources.
Q: Can I sign other document formats?
A: Yes—GroupDocs.Signature also supports DOCX, XLSX, PPTX, images, and many more. Just change the input file extension; the API remains the same.
Q: Are there file‑size limits?
A: No hard limit, but documents over 50 MB may require a larger JVM heap and page‑by‑page processing to stay memory‑efficient.
Q: How do I sign PDFs stored in cloud storage?
A: Download the file to a temporary local path, apply the signature, then upload it back to your cloud bucket. Clean up temporary files afterward.
Conclusion
You now have a complete, production‑ready guide to create barcode signature java for PDF documents. By combining cryptographic digital signatures with machine‑readable barcodes, you achieve both legal enforceability and operational efficiency across supply‑chain, healthcare, finance, and manufacturing use cases. Experiment with different barcode types, integrate the code into your existing services, and explore batch processing for large‑scale deployments.
Last Updated: 2026-05-21
Tested With: GroupDocs.Signature 23.10 for Java
Author: GroupDocs
String filePath = "YOUR_DOCUMENT_DIRECTORY" + "/sample.pdf";
String fileName = new java.io.File(filePath).getName();
String outputPath = "YOUR_OUTPUT_DIRECTORY" + "/SignedWithBarcodeGS1CompositeBar/" + fileName;
String documentDir = System.getenv("DOCUMENT_DIR");
String outputDir = System.getenv("OUTPUT_DIR");
Signature signature = new Signature(filePath);