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 Flutter.

English

License module

Reading barcodes needs the conversion module and drawing them the editor module (both Startup tier and above).

Read barcodes from pages

doc.readBarcodes(pageIndex: ..., options: ...) renders a page (every page when pageIndex is left out) and reads the barcodes on it. Positions are in PDF points.

import 'dart:io';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';

final doc = sdk.open(await File(deliveryNotePath).readAsBytes());
for (final code in doc.readBarcodes().cast<Map<String, Object?>>()) {
  print('${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

sdk.readBarcodesImage(bytes, options: ...) 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 null.

final codes = sdk.readBarcodesImage(await File(labelPath).readAsBytes(), options: {
  'formats': ['qr_code'],
});

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

doc.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.

final label = sdk.open(await File(labelPdfPath).readAsBytes());
label.addBarcode(0, (400.0, 700.0, 520.0, 820.0), format: 'qr_code', text: 'https://example.com/track/8812');
label.addBarcode(0, (72.0, 60.0, 300.0, 110.0), format: 'code_128', text: 'SHIP-8812-PT');
label.addBarcode(0, (320.0, 60.0, 460.0, 110.0), format: 'ean_13', text: '560123456789');   // check digit computed
await File(codedPath).writeAsBytes(label.save());
label.dispose();

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

nexpdf_add_barcode_ex draws like addBarcode with options. It has no typed Dart method; call it through dart:ffi on doc.handle:

import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';

// int32_t nexpdf_add_barcode_ex(NexPdfDocument *doc, size_t page_index, double llx, double lly,
//     double urx, double ury, const char *format, const char *text, const char *options_json);
final addBarcodeEx = nexpdfLib.lookupFunction<
    Int32 Function(Pointer<Void>, Size, Double, Double, Double, Double, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>),
    int Function(Pointer<Void>, int, double, double, double, double, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_add_barcode_ex');

using((a) => nexpdfCall(() => addBarcodeEx(label.handle, 0, 72, 60, 300, 110, 'code_128'.toNativeUtf8(allocator: a),
    'SHIP-8812-PT'.toNativeUtf8(allocator: a), '{"human_readable":"always"}'.toNativeUtf8(allocator: a))));
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 prints none
quiet_zone10The quiet zone around the code
backgroundtrueFill the rectangle white behind the code
color#000000Colour of the bars and text

On this page