Barcodes
Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and pictures, and draw real vector barcodes, with NexPDF for Node.js.
License module
Reading barcodes needs the conversion module and drawing them the editor module (both
Startup tier and above).
Read barcodes from pages
doc.raw.readBarcodesJson(pageIndex, optionsJson) renders a page (every page with -1) and reads
the barcodes on it. Positions are in PDF points.
import { PdfDocument } from '@nexdevit/nexpdf-node';
const doc = await PdfDocument.open('delivery-note.pdf');
for (const code of JSON.parse(doc.raw.readBarcodesJson(-1))) {
console.log(code.page, code.format, code.text, code.rect);
}Each result has format, text, bytes_base64 (the raw payload of 2D codes, null for linear
codes), page, rect ([llx, lly, urx, ury]) and the corner points.
Read barcodes from an image
readBarcodesImageJson(bytes, optionsJson) reads a PNG or JPEG picture. rect and points are
then in pixels (origin at the top-left) and page is null.
import { readFile } from 'node:fs/promises';
import { readBarcodesImageJson } from '@nexdevit/nexpdf-node';
const codes = JSON.parse(readBarcodesImageJson(await readFile('label.png'), '{"formats":["qr_code"]}'));To read a TIFF, or to get positions in PDF points, open the picture as a document
(PdfDocument.fromImage takes JPEG, PNG and TIFF, one page per picture) and use
readBarcodesJson on its page.
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
doc.raw.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.
const doc = await PdfDocument.open('label.pdf');
doc.raw.addBarcode(0, 400, 700, 520, 820, 'qr_code', 'https://example.com/track/8812');
doc.raw.addBarcode(0, 72, 60, 300, 110, 'code_128', 'SHIP-8812-PT');
doc.raw.addBarcode(0, 320, 60, 460, 110, 'ean_13', '560123456789'); // check digit computed
await doc.save('label-coded.pdf');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.
doc.raw.addBarcodeEx(pageIndex, llx, lly, urx, ury, format, text, optionsJson) draws the same
barcodes with options:
doc.raw.addBarcodeEx(0, 72, 60, 300, 110, 'code_128', 'SHIP-8812-PT',
JSON.stringify({ human_readable: 'always', quiet_zone: 10, background: false, color: '#1F2937' }));| Option | Default | Meaning |
|---|---|---|
human_readable | auto | auto prints the digits under EAN-13, EAN-8, UPC-A and UPC-E; always also prints the text under other linear codes; never prints none |
quiet_zone | by format | Quiet zone in modules on every side (10 for most linear codes, 20 for ITF, 4 for QR and Aztec, 2 for PDF417, 1 for Data Matrix) |
background | true | Paint a white background over the whole rectangle first |
color | #000000 | Colour of the bars and modules |