Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active December 30, 2021 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/a19a5c062ccef69dd9aed92f48b7451a to your computer and use it in GitHub Desktop.
Save conholdate-gists/a19a5c062ccef69dd9aed92f48b7451a to your computer and use it in GitHub Desktop.
Generate Barcode in XML using Java
// Initialize the BarcodeGenerator object
// Pass barcode text and barcode symbology as parameters.
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DATA_MATRIX, "abcdefghijklmnopqrstuvwxyzabcdef");
// Set various different properties/variables of the barcode.
generator.getParameters().getBorder().setVisible(true);
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.ABOVE);
// Specify caption Above settings.
generator.getParameters().getCaptionAbove().setText("Caption ABOVE");
generator.getParameters().getCaptionAbove().setAlignment(TextAlignment.CENTER);
generator.getParameters().getCaptionAbove().setVisible(true);
generator.getParameters().getCaptionAbove().setTextColor(Color.GREEN);
// Specify caption Below settings.
generator.getParameters().getCaptionBelow().setText("Caption BELOW");
generator.getParameters().getCaptionBelow().setAlignment(TextAlignment.CENTER);
generator.getParameters().getCaptionBelow().setVisible(true);
generator.getParameters().getCaptionBelow().setTextColor(Color.YELLOW);
// Specify text font settings.
generator.getParameters().getBarcode().getCodeTextParameters().getFont().setFamilyName("Courier New");
generator.getParameters().getBarcode().getCodeTextParameters().getFont().getSize().setPoint(24);
generator.getParameters().getBarcode().getCodeTextParameters().getFont().setStyle(FontStyle.BOLD);
// Call the export to XML method to export the properties to XML file.
generator.exportToXml("C:\\Files\\barcode\\DataMatrix_out.xml");
// Instantialize barcode generator instance with CodeText & Barcode Symbology
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.PDF_417,
"this is some test code text. \n Second line \n third line.");
// Save in XML
generator.exportToXml("C:\\Files\\barcode\\barcode_xml_out.xml");
// Initialize the BarcodeGenerator object
// Pass barcode symbology as QR and barcode text as parameters.
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode");
// Set resolution
generator.getParameters().setResolution(400);
// Save QR code in XML
generator.exportToXml("C:\\Files\\barcode\\QR_out.xml");
// Read barcode from XML and instantialize BarcodeGenerator object
BarcodeGenerator generator = BarcodeGenerator.importFromXml("C:\\Files\\barcode\\barcode_xml_out.xml");
// Save barcode as Jpeg
generator.save("C:\\Files\\barcode\\barcode_xml_out.jpg", BarCodeImageFormat.JPEG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment