Barcodes
Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and images, and draw real vector barcodes, with NexPDF for iOS (Swift).
License module
Reading barcodes needs the conversion module and drawing them the editor module (both
Startup tier and above).
Read barcodes from pages
readBarcodesJson(pageIndex:optionsJson:) renders a page (every page with -1) and reads the
barcodes on it. Positions are in PDF points.
import NexPdf
let doc = try NexPdfDocument.open(url: deliveryNoteURL)
let codes = NexPdf.json(try doc.readBarcodesJson(pageIndex: -1)) as? [[String: Any]] ?? []
for code in codes {
print(code["page"] ?? 0, code["format"] ?? "", code["text"] ?? "", code["rect"] ?? [])
}Each result has format, text, bytes_base64 (the raw payload of 2D codes, NSNull for linear
codes), page, rect ([llx, lly, urx, ury]) and the corner points.
Read barcodes from an image
NexPdf.readBarcodesImageJson(data:optionsJson:) reads a PNG or JPEG, for example a photo from the
camera. rect and points are then in pixels with the origin at the top-left, and page is
NSNull.
let label = try Data(contentsOf: labelURL)
let found = NexPdf.json(try NexPdf.readBarcodesImageJson(data: label, optionsJson: #"{"formats":["qr_code"]}"#))Reading renders the page at the chosen resolution: run it off the main thread.
Options
| Option | Default | Meaning |
|---|---|---|
formats | every format | Limit the search, for example ["qr_code","code_128"] |
dpi | 200 | Resolution pages are rendered at |
try_harder | true | Spend more time on hard images |
pure_barcode | false | The image holds only one barcode and nothing else |
multiple | true | Return every barcode, not just the first |
tile_scan | true | Also scan tiles of the page for small codes |
Formats: qr_code, data_matrix, aztec, pdf_417, code_128, code_39, code_93, ean_13,
ean_8, upc_a, upc_e, itf, codabar and more (MaxiCode and DataBar are also read).
Draw a barcode
addBarcode(pageIndex:llx:lly:urx:ury:format:text:) draws the text as a vector barcode, with a
white background and a quiet zone, into the rectangle (PDF points). It is real page content, not an
image, so it prints sharply at any size.
let label = try NexPdfDocument.open(url: labelURL)
try label.addBarcode(pageIndex: 0, llx: 400, lly: 700, urx: 520, ury: 820, format: "qr_code",
text: "https://example.com/track/8812")
try label.addBarcode(pageIndex: 0, llx: 72, lly: 60, urx: 300, ury: 110, format: "code_128", text: "SHIP-8812-PT")
try label.addBarcode(pageIndex: 0, llx: 320, lly: 60, urx: 460, ury: 110, format: "ean_13",
text: "560123456789") // check digit computed
try label.write(to: codedURL)Formats you can draw: qr_code, data_matrix, aztec, pdf_417, code_128, code_39,
code_93, ean_13 (12 digits plus a computed check digit, or 13), ean_8, upc_a, upc_e,
itf, codabar and telepen.
Options for drawing
addBarcodeEx(pageIndex:llx:lly:urx:ury:format:text:optionsJson:) draws like addBarcode with
options:
try label.addBarcodeEx(pageIndex: 0, llx: 72, lly: 60, urx: 300, ury: 110, format: "code_128", text: "SHIP-8812-PT",
optionsJson: #"{"human_readable":"always","background":false,"color":"#1F2937"}"#)| Option | Default | Meaning |
|---|---|---|
human_readable | auto | auto prints the digits under EAN-13, EAN-8, UPC-A and UPC-E in the standard layout; always also prints the text under other linear codes; never prints none |
quiet_zone | 10 | The quiet zone around the code |
background | true | Fill the rectangle white behind the code |
color | #000000 | Colour of the bars and text |