Scan Barcode From PDF Using ITextSharp

Introduction

There is a very popular free .NET PDF library called iTextSharp. I used it to process a PDF document sometimes. So I tried to complete the job using iTextSharp and the library introduced in the first paragraph. And it worked. So I want to share the solution with you guys here.

What iTextSharp support?

  • PDF generation
  • PDF manipulation (stamping watermarks, merging/splitting PDFs and so on)
  • PDF form filling
  • XML functionality
  • Digital signatures

Simple code showing how to use iTextSharp.

// create a document object  
Document document = new Document(PageSize.A4, 50, 50, 25, 25);  
  
// create a new PdfWriter object, specifying the output stream  
FileStream output = new FileStream("firstPdf.pdf", FileMode.Create);  
  
var writer = PdfWriter.GetInstance(document, output);  
  
// open the document for writing  
document.Open();  
  
// create a new paragraph object with the text, "Hello, World!"  
Paragraph welcomeParagraph = new Paragraph("Hello, World!");  
  
// add the paragraph object to the document  
document.Add(welcomeParagraph);  
  
// close the document - this saves the document contents to the output stream  
document.Close(); 

Output

How to scan barcode from PDF?

In this part, I present you complete code to fulfill the job. If you don't know how to use the barcode library, please check Sourav Kayal's article. The “Hello, World!” code will make you understand how to use iTextSharp.

The method GetImages uses iTextSharp to extract images from a PDF document.

By using the method GeImages

private static void GetImages(string filename)  
{
    int pageNum = 1;  

    PdfReader pdf = new PdfReader(filename);  // PdfReader keyword
    PdfDictionary pg = pdf.GetPageN(pageNum);  // PdfDictionary keyword
    PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));  // PdfDictionary keyword
    PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));  // PdfDictionary keyword

    if (xobj == null) { return; }  // if keyword

    foreach (PdfName name in xobj.Keys)  // foreach keyword
    {
        PdfObject obj = xobj.Get(name);  // PdfObject keyword

        if (!obj.IsIndirect()) { continue; }  // if keyword

        PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);  // PdfDictionary keyword
        PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));  // PdfName keyword

        if (!type.Equals(PdfName.IMAGE)) { continue; }  // if keyword

        int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));  // Convert keyword
        PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);  // PdfObject keyword
        PdfStream pdfStrem = (PdfStream)pdfObj;  // PdfStream keyword

        byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);  // byte[] keyword

        if (bytes == null) { continue; }  // if keyword

        using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))  // using keyword
        {
            memStream.Position = 0;  // memStream keyword
            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);  // System.Drawing.Image keyword

            string path = Path.Combine(String.Format(@"result-{0}.jpg", pageNum));  // Path keyword
            System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);  // System.Drawing.Imaging.EncoderParameters keyword
            parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);  // System.Drawing.Imaging.EncoderParameter keyword
            var jpegEncoder = ImageCodecInfo.GetImageEncoders().ToList().Find(x => x.FormatID == ImageFormat.Jpeg.Guid);  // var keyword
            img.Save(path, jpegEncoder, parms);  // Save method
        }
    }
}

The following shows how to use the barcode library to scan the extracted image.

// Get images from source2.pdf
GetImages("source2.pdf");

// Scan the images for barcode
bool imageExist = File.Exists("result-1.jpg");
if (imageExist)
{
    string scanningResult = Spire.Barcode.BarcodeScanner.ScanOne("result-1.jpg");
    Console.WriteLine(scanningResult);
}

Console.WriteLine("Done!");
Console.ReadLine();

Output

Conclusion

You are welcome to test the code to scan barcodes from a PDF document. I hope this article may provide you some help in programming.


Similar Articles