Read and Create Barcodes with the Webservice

Minimum Requirements​

  • Java version: 7
  • webPDF version: 7
  • wsclient version: 1

This article shows a concrete coding example for the Barcode webservice and its usage with the webPDF wsclient library.

Important note: The following coding example is based on the webPDF wsclient library.

Preparations for the Webservice Call​

Before calling the webservice, you should already have created a REST or SOAP session. Use the WebServiceFactory to create either a BarcodeWebService object for SOAP or a BarcodeRestWebService object for REST.

SOAP:

BarcodeWebService barcodeWebService =
WebServiceFactory.createInstance(
session, WebServiceType.BARCODE
);

REST:

BarcodeRestWebService barcodeWebService =
WebServiceFactory.createInstance(
session, WebServiceType.BARCODE
);

Then pass a RestDocument or SoapDocument via setDocument().

Webservice Parameters​

For write access, the current open and/or permission password may be required:

barcodeWebService.getPassword().setOpen("password");
barcodeWebService.getPassword().setPermission("password");

If the document is not password protected, this step can be skipped.

Description of the Barcode Webservice​

The Barcode webservice is an endpoint of the webPDF server that can create barcodes in PDF documents and also detect and extract them.

You can retrieve the operation like this:

BarcodeType barcode = barcodeWebService.getOperation();

The add Object​

To add new barcodes:

BarcodeType.Add add = new BarcodeType.Add();
barcode.setAdd(add);

Supported barcode types:

  • Aztec (AztecBarcodeType)
  • Codabar (CodabarBarcodeType)
  • Code128 (Code128BarcodeType)
  • Code39 (Code39BarcodeType)
  • DataMatrix (DataMatrixBarcodeType)
  • EAN13 (EAN13BarcodeType)
  • EAN8 (EAN8BarcodeType)
  • ITF (ItfBarcodeType)
  • PDF417 (Pdf417BarcodeType)
  • QR code (QrBarcodeType)
  • UPCA (UpcaBarcodeType)

Example for Aztec:

AztecBarcodeType aztec = new AztecBarcodeType();
add.getAztec().add(aztec);

General Barcode Parameters​

These parameters apply to all barcode types:

value (default: "")​

Encoded barcode content:

typedBarcode.setValue("http://www.softvision.de");

pages (default: "")​

Page range (1, 1,3,5, 1-5, 1,3-5,6, *):

typedBarcode.setPages("1,3-5,6");

charset (default: "utf-8")​

Character set:

typedBarcode.setCharset("utf-8");

rotation (default: 0)​

Rotation in 90-degree steps:

typedBarcode.setRotation(90);

margin (default: 0)​

Blank area around the barcode:

typedBarcode.setMargin(5);

The position Object​

Position and dimensions are defined with RectangleType:

RectangleType position = new RectangleType();
typedBarcode.setPosition(position);

Parameters:

  • x (default: 0): position.setX(15);
  • y (default: 0): position.setY(15);
  • width (default: 0): position.setWidth(200);
  • height (default: 0): position.setHeight(16);
  • coordinates (default: user): position.setCoordinates(CoordinatesType.USER);
  • metrics (default: mm): position.setMetrics(MetricsType.PX);

Coordinate systems:

  • user = origin at the top left
  • pdf = origin at the bottom left

Units:

  • mm = millimeter
  • px = pixel

Type-Specific Parameters​

Aztec​

  • errorCorrection (0-100): aztec.setErrorCorrection(50);
  • layers: aztec.setLayers(10);

DataMatrix​

  • errorCorrection (1-8): dataMatrix.setErrorCorrection(7);
  • shape: dm.setShape(DataMatrixShapeType.RECTANGLE);

QR Code​

  • errorCorrection (l, m, q, h):
qrBarcodeType.setErrorCorrection(QrCodeErrorCorrectionType.M);

PDF417​

  • errorCorrection (1-8): pdf417.setErrorCorrection(7);
  • compact: pdf417.setCompact(true);
  • compactionMode: pdf417.setCompactionMode(Pdf417CompactionModeType.BYTE);
  • shape: pdf417.setShape(DataMatrixShapeType.SQUARE);
  • dataCodewordsMax: pdf417.setDataCodewordsMax(7);
  • dataCodewordsMin: pdf417.setDataCodewordsMin(4);
  • symbolsPerCodewordMax: pdf417.setSymbolsPerCodewordMax(8);
  • symbolsPerCodewordMin: pdf417.setSymbolsPerCodewordMin(4);

The detect Object​

To detect and extract barcodes:

BarcodeType.Detect detect = new BarcodeType.Detect();
barcode.setDetect(detect);

inputFormat (default: "pdf")​

detect.setInputFormat(BarcodeDetectInputFormatType.PDF);

outputFormat (default: "json")​

detect.setOutputFormat(BarcodeDetectOutputFormatType.XML);

The selection Object​

To configure barcode detection:

BarcodeSelectionType selection = new BarcodeSelectionType();
detect.getSelection().add(selection);

Important parameters:

  • formats: selection.setFormats("qrcode,aztec,code39");
  • charset: selection.setCharset("utf-8");
  • pages: selection.setPages("1,3-5,6");
  • allowedLengths: selection.setAllowedLengths("14");
  • barcode39CheckDigit: selection.setBarcode39CheckDigit(true);
  • codabarStartEndDigits: selection.setCodabarStartEndDigits(true);
  • gs1: selection.setGs1(true);
  • pureBarcode: selection.setPureBarcode(true);
  • resolution: selection.setResolution(300);
  • tryHarder: selection.setTryHarder(false);
  • upcEanExtensions: selection.setUpcEanExtensions("8");

The scanArea Object​

To limit the scan area:

RectangleType scanArea = new RectangleType();
selection.setScanArea(scanArea);

For the best results, the area should be restricted as closely as possible to the actual barcode.

SOAP Example​

try (
// Set up a session with the webPDF server (SOAP in this example):
SoapSession session = SessionFactory.createInstance(
WebServiceProtocol.SOAP,
new URL("https://localhost:8080/webPDF/")
);

// Provide the source and target documents:
SoapDocument soapDocument = new SoapDocument(
new File("Path to the source document").toURI(),
new File("Path to the target document")
)
) {
// Webservice call and processing
} catch (ResultException | MalformedURLException ex) {
// Error handling
}

Background Information​

More coding examples can be found here.