Barcodes
Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and images, and draw real vector barcodes, with NexPDF for Rust.
License module
Reading barcodes needs the conversion module and drawing them the editor module (both
Startup tier and above).
Read barcodes from pages
read_barcodes_json(page_index, options_json) renders a page (every page with -1) and reads
the barcodes on it. Positions are in PDF points.
use nexpdf_sdk::Document;
fn main() -> nexpdf_sdk::Result<()> {
let doc = Document::open("delivery-note.pdf")?;
for code in doc.read_barcodes_json_value(-1, None)?.as_array().into_iter().flatten() {
println!("{} {} {} {}", code["page"], code["format"], code["text"], code["rect"]);
}
Ok(())
}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. The example uses the
serde feature; read_barcodes_json returns the same JSON as a String.
Read barcodes from an image
nexpdf_sdk::read_barcodes_image_json(data, options_json) reads a PNG or JPEG. rect and points
are then in pixels with the origin at the top-left, and page is null.
let png = std::fs::read("label.png")?;
let codes = nexpdf_sdk::read_barcodes_image_json(&png, r#"{"formats":["qr_code"]}"#)?;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
add_barcode(page_index, 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 mut doc = Document::open("label.pdf")?;
doc.add_barcode(0, 400.0, 700.0, 520.0, 820.0, "qr_code", "https://example.com/track/8812")?;
doc.add_barcode(0, 72.0, 60.0, 300.0, 110.0, "code_128", "SHIP-8812-PT")?;
doc.add_barcode(0, 320.0, 60.0, 460.0, 110.0, "ean_13", "560123456789")?; // check digit computed
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.
add_barcode_ex(page_index, llx, lly, urx, ury, format, text, options_json) draws the same
barcodes with options (None = defaults):
doc.add_barcode_ex(
0, 72.0, 60.0, 260.0, 130.0, "ean_13", "560123456789",
r#"{"human_readable":"auto","quiet_zone":10,"background":true,"color":"#000000"}"#,
)?;| 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 | Quiet zone around the code |
background | true | Draw a white background |
color | #000000 | Colour of the bars |