Barcodes
Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and photos, and draw vector barcodes, in a .NET MAUI app.
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;
var doc = view.Document!;
var codes = JsonNode.Parse(await Task.Run(() => doc.ReadBarcodesJson(view.CurrentPage)))!.AsArray();
foreach (var code in codes)
System.Diagnostics.Debug.WriteLine($"{code!["format"]} {code["text"]}");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, such as a photo the user
picks. rect and points are then in pixels with the origin at the top-left, and page is null.
using Nexdevit.NexPdf;
var photo = await MediaPicker.Default.PickPhotoAsync();
if (photo is not null)
{
await using var s = await photo.OpenReadAsync();
using var ms = new MemoryStream();
await s.CopyToAsync(ms);
var json = await Task.Run(() => NexPdfSdk.ReadBarcodesImageJson(ms.ToArray(), """{"formats":["qr_code"]}"""));
}Options
formats (every format by default), dpi (200), try_harder (true), pure_barcode
(false), multiple (true) and tile_scan (true). 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. See the .NET barcodes guide.
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):
view.Document!.AddBarcode(0, 400, 700, 520, 820, "qr_code", "https://example.com/track/8812");
await view.ReloadAsync();Formats you can draw: qr_code, data_matrix, aztec, pdf_417, code_128, code_39,
code_93, ean_13, ean_8, upc_a, upc_e, itf, codabar and telepen.
AddBarcodeEx(pageIndex, llx, lly, urx, ury, format, text, optionsJson) takes options:
human_readable (auto prints the digits under EAN and UPC codes, always also under other
linear codes, never), quiet_zone (in modules), background (default true) and color
(#RRGGBB, default black):
view.Document!.AddBarcodeEx(0, 72, 60, 260, 120, "ean_13", "560123456789",
"""{"human_readable":"auto","background":false}""");
await view.ReloadAsync();