DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

Read and Write Barcodes with PDF

Iron Barcode is proficient in reading barcodes from one or all pages of a PDF document, and those pages can be easily specified. It can also correct for badly scanned PDFs with perhaps skew or digital noise within their image. This can be set specifically by the developer, or automated using the QuicklyReadAllBarcodes method with the TryHarder flag set to true.

We may also add barcodes to a PDF document using the StampToExistingPdfPage method, or simply save our barcode as a standalone PDF file.

You can download the software product from this link.

C#:

using IronBarCode;


/*** READ BARCODES FROM A PDF DOCUMENT ***/

// A PDF document may be easily scanned for 1 or multiple Barcodes
BarcodeResult[] PDFResults = BarcodeReader.QuicklyReadAllBarcodes("MultipleBarcodes.pdf", BarcodeEncoding.AllOneDimensional, true);

// Read only a specific barcode type
BarcodeResult[] InvoiceResults = BarcodeReader.ReadBarcodesFromPdf("PileofInvoices.pdf", BarcodeEncoding.Code128);

// Read only specific pages
PagedBarcodeResult[] InvoiceResultsPage123 = BarcodeReader.ReadBarcodesFromPdfPages("PileofInvoices.pdf", new[] { 1, 2, 3 }, BarcodeEncoding.Code128);

// Read even from badly scanned or noisy PDFs
PagedBarcodeResult[] InvoiceResultsCorrected = BarcodeReader.ReadBarcodesFromPdfPages("PileofInvoices.BadScan.pdf", new[] { 1, 2, 3 }, BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.CleanNonBlackPixels);


/*** WRITE BARCODES TO A NEW OR EXISTING PDF DOCUMENT ***/

GeneratedBarcode MyBarCode = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128);

// Save to a new PDF
MyBarCode.SaveAsPdf("MyBarCode.Pdf");

/// Add the barcode to any page or pages of an existing PDF
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 200, 50, 1);  // position x=200 y=50 on page 1
MyBarCode.StampToExistingPdfPages("ExistingPDF.pdf", 200, 50, new[] { 1, 2, 3 }, "Password123");  // multiple pages of an encrypted PDF
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronBarCode


'''* READ BARCODES FROM A PDF DOCUMENT **

' A PDF document may be easily scanned for 1 or multiple Barcodes
Private PDFResults() As BarcodeResult = BarcodeReader.QuicklyReadAllBarcodes("MultipleBarcodes.pdf", BarcodeEncoding.AllOneDimensional, True)

' Read only a specific barcode type
Private InvoiceResults() As BarcodeResult = BarcodeReader.ReadBarcodesFromPdf("PileofInvoices.pdf", BarcodeEncoding.Code128)

' Read only specific pages
Private InvoiceResultsPage123() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdfPages("PileofInvoices.pdf", { 1, 2, 3 }, BarcodeEncoding.Code128)

' Read even from badly scanned or noisy PDFs
Private InvoiceResultsCorrected() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdfPages("PileofInvoices.BadScan.pdf", { 1, 2, 3 }, BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.CleanNonBlackPixels)


'''* WRITE BARCODES TO A NEW OR EXISTING PDF DOCUMENT **

Private MyBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128)

' Save to a new PDF
MyBarCode.SaveAsPdf("MyBarCode.Pdf")

''' Add the barcode to any page or pages of an existing PDF
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 200, 50, 1) ' position x=200 y=50 on page 1
MyBarCode.StampToExistingPdfPages("ExistingPDF.pdf", 200, 50, { 1, 2, 3 }, "Password123") ' multiple pages of an encrypted PDF
Enter fullscreen mode Exit fullscreen mode

Top comments (0)