Skip to Content
NexPDF

Barcodes

Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and images, and draw real vector barcodes, with NexPDF for .NET.

English

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.

using System.Text.Json.Nodes;
using Nexdevit.NexPdf;

using var doc = NexPdfDocument.Open("delivery-note.pdf");
foreach (var code in JsonNode.Parse(doc.ReadBarcodesJson(-1))!.AsArray())
    Console.WriteLine($"{code!["page"]} {code["format"]} {code["text"]} {code["rect"]!.ToJsonString()}");

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

NexPdfSdk.ReadBarcodesImageJson(data, optionsJson) reads a PNG or JPEG. rect and points are then in pixels with the origin at the top-left, and page is null.

var codes = JsonNode.Parse(NexPdfSdk.ReadBarcodesImageJson(
    File.ReadAllBytes("label.png"), """{"formats":["qr_code"]}"""))!.AsArray();

Options

OptionDefaultMeaning
formatsevery formatLimit the search, for example ["qr_code","code_128"]
dpi200Resolution pages are rendered at
try_hardertrueSpend more time on hard images
pure_barcodefalseThe image holds only one barcode and nothing else
multipletrueReturn every barcode, not just the first
tile_scantrueAlso 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.

using var doc = NexPdfDocument.Open("label.pdf");
doc.AddBarcode(0, 400, 700, 520, 820, "qr_code", "https://example.com/track/8812");
doc.AddBarcode(0, 72, 60, 300, 110, "code_128", "SHIP-8812-PT");
doc.AddBarcode(0, 320, 60, 460, 110, "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.

AddBarcodeEx(pageIndex, llx, lly, urx, ury, format, text, optionsJson) takes options:

doc.AddBarcodeEx(0, 72, 140, 260, 200, "ean_13", "560123456789",
    """{"human_readable":"auto","quiet_zone":10,"background":false,"color":"#1A1A1A"}""");
OptionDefaultMeaning
human_readableautoauto 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
quiet_zoneper formatQuiet zone around the code, in modules (10 for most linear codes, 20 for ITF, 4 for QR and Aztec)
backgroundtrueDraw the white background
color#000000Colour of the bars

On this page