Skip to content

Instantly share code, notes, and snippets.

@aspose-barcode-gists
Last active August 18, 2022 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-barcode-gists/f4986b73e3e139a5b105a5a1f2adbe4b to your computer and use it in GitHub Desktop.
Save aspose-barcode-gists/f4986b73e3e139a5b105a5a1f2adbe4b to your computer and use it in GitHub Desktop.
Add Barcode to PDF using C#

Learn how to add a barcode to PDF documents using C#.

The article shall cover the following topics:

  1. C# API to Add Barcode to PDF
  2. Create PDF Document and Add Barcode
  3. Add Barcode to Existing PDF Document
  4. Add QR Code to PDF Documents
  5. Read Barcode from PDF Document
// This code example demonstrates how to add barcode image to a PDF.
// The path to the documents directory.
string dataDir = @"D:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code39Standard, "1234567");
// Create memory stream and Save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Create PDF document and Add a page to the document
Document doc = new Document();
doc.Pages.Add();
// Open document
PdfFileMend mender = new PdfFileMend();
// Bind the PDF to add barcode
mender.BindPdf(doc);
// Add barcode image in the PDF file
mender.AddImage(ms, 1, 100, 600, 200, 700);
// Save changes
mender.Save(dataDir + "AddImage_out.pdf");
// Close PdfFileMend object
mender.Close();
// This code example demonstrates how to add QR code to an existing PDF.
// The path to the documents directory.
string dataDir = @"D:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "1234567");
// Create memory stream and Save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Load a PDF document
Document doc = new Document(dataDir + "sample.pdf");
// Open document
PdfFileMend mender = new PdfFileMend();
// Bind PDF to add barcode image
mender.BindPdf(doc);
// Add QR image in the PDF file
mender.AddImage(ms, 1, 0, 650, 200, 780);
// Save changes
mender.Save(dataDir + "Sample_QR_out.pdf");
// Close PdfFileMend object
mender.Close();
// This code example demonstrates how to add barcode image to an existing PDF.
// The path to the documents directory.
string dataDir = @"D:\Files\BarCode\";
// Instantiate linear barcode object, Set the Code text and symbology type for the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code39Standard, "1234567");
// Create memory stream and Save barcode image to memory stream
Stream ms = new MemoryStream();
generator.Save(ms, BarCodeImageFormat.Bmp);
// Load a PDF document
Document doc = new Document(dataDir + "sample.pdf");
// Open document
PdfFileMend mender = new PdfFileMend();
// Bind PDF to add barcode image
mender.BindPdf(doc);
// Add barcode image in the PDF file
mender.AddImage(ms, 1, 70, 0, 200, 600);
// Save changes
mender.Save(dataDir + "Sample_out.pdf");
// Close PdfFileMend object
mender.Close();
// This code example demonstrates how to read barcode from a PDF
// The path to the documents directory.
string dataDir = @"D:\Files\BarCode\sample_out.pdf";
// Bind the pdf document
Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(dataDir);
// Set page range for image extraction
pdfExtractor.StartPage = 1;
pdfExtractor.EndPage = 1;
// Extract the images
pdfExtractor.ExtractImage();
// Save images to stream in a loop
while (pdfExtractor.HasNextImage())
{
// Save image to stream
MemoryStream imageStream = new MemoryStream();
pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;
// Recognize the barcode from the image stream above
using (BarCodeReader reader = new BarCodeReader(imageStream, DecodeType.Code39Standard))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Codetext found: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment