PM > Install-Package XDoc.PDF

How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Barcode Reader Library
How to scan, read linear, 2d barcodes from PDF file using C#.NET


Using C# Code to Scan 1D and 2D Barcodes on PDF Document Page in C#.NET Application





In this C# tutorial, you learn how to scan, read linear and 2d barcode from PDF file using C#

  • Read Code 39, Code 128, EAN/UPC, GS1 128 linear barcodes from PDF
  • Read Data Matrix, PDF-417 and QR Code 2d barcodes from PDF
  • Scan multiple barcode types from a PDF page

How to read, scan barcode from PDF file using C#

  1. Download XDoc.PDF barcode reader C# library
  2. Install C# library to read barcodes from PDF document
  3. Step by Step Tutorial










C#.NET PDF Barcode Reader & Scanner Controls from RasterEdge DocImage SDK for .NET is a professional barcode detecting and decoding library toolkit designed specifically for linear and 2d barcodes reading, scanning and recognition from PDF document files in C#.NET class applications.

  • Read & scan most 1d and 2d barcodes from PDF document using C# code in .NET
  • Detect & decode barcode from whole or partial PDF document page using C#.NET
  • Recognize barcode from PDF document and return decoded barcode value to data string
  • Speed up barcode reading rate from PDF by limiting detected barcode number using C#.NET code
  • Support multi-threaded barcode recognition from PDF with high performance
  • Recognized barcode value will be returned with 100% accuracy






Scan, read barcode from PDF file using C#.NET


The following steps and sample c# source code will show how to quickly read a barcode from PDF file in C# code.

  1. Create a new PDFDocument with an existing PDF file loaded
  2. Create a new ReaderSettings object with scan options applied
  3. Call BarcodeReader.ReadBarcodes() to read, scan all Code 128 barcodes from PDF document.



            String inputFilePath = @"C:\1.pdf";

            //  Open PDF file
            PDFDocument doc = new PDFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type to Code128
            settings.AddTypesToRead(BarcodeType.Code128);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");




Barcode Read Options


In your C# code, you can use class "ReaderSettings" to apply the barcode read options. The class is used to configure the barcode reader.


  • Direction: Type: ScanDirection. Gets and sets the direction of reading the source image.
    Enum: ScanDirection
    • Undefined: Scan the source in different directions and stop once the 1st barcode is found.
    • LeftToRight: Scan the source from left to right only.
    • TopToBottom: Scan the source from top to bottom only
    • RightToLeft: Scan the source from right to left only.
    • BottomToTop: Scan the source from bottom to top only.
    • Horizontal: Scan the source in horizontal.
      Equivalent to: LeftToRight | RightToLeft
    • Vertical: Scan the source in vertical.
      Equivalent to: TopToBottom | BottomToTop
    • All: Scan the source in all 4 directions.

  • RegionOfInterest: Specifies the region on the source image of which it would scan.
    Unit: in percent
    Remarks: X, Y, (X + Width) and (Y + Height) of the Rectangle must be in the range of 0 to 100; otherwise, the region is the whole image.

  • NumberOfBarcodeToRead: Limit the number of barcode that read from the source.
    MoreThanOne: read all barcodes.







Barcode Result Information


Once you have scanned the PDF file, and you will get a list of Barcode object. Each object contain returned barcode information.


  • Type: Gets type of the found barcode.
  • RawData: Gets barcode message data in byte array
  • DataString: Gets barcode message data in string.
  • BeginLine: Gets the begin line segment of the barcode, which usually is the left edge of the barcode boundary.
  • EndLine: Gets the end line segment of the barcode, which usually is the right edge of the barcode boundary.
  • BoundingRectangle: Gets the minimum bounding rectangle of the barcode.
  • Direction: Gets the direction of the barcode.
    LeftToRight: from left to right
    TopToBottom: from top to bottom
    RightToLeft: from right to left
    BottomToTop: from bottom to top






Read multiple barcode types from PDF file using C#.NET


The following steps and sample c# source code will show how to scan, read more than one barcode type from a PDF file in C# code.

  1. Create a new PDFDocument with an existing PDF file loaded
  2. Create a new ReaderSettings object, and add three barcode types to scan, Code128, DataMatrix, and PDF417.
  3. Call BarcodeReader.ReadBarcodes() to read, scan all these three barcodes from PDF document.



            String inputFilePath = @"C:\1.pdf";

            //  Open PDF file
            PDFDocument doc = new PDFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set multiple target barcode types to Code 128, Data Matrix and PDF-417
            settings.AddTypesToRead(BarcodeType.Code128);
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            settings.AddTypesToRead(BarcodeType.PDF417);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                {
                    Console.WriteLine("Barcode Type: " + barcode.Type.ToString());
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
                }
            }
            else
                Console.WriteLine("[No Barcode Found]");






Read barcode from PDF page using C# code


The following C# examle source code will show how to read barcode from a PDF page using C#.



            String inputFilePath = @"C:\1.pdf";

            //  Open PDF file
            PDFDocument doc = new PDFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type to Code128
            settings.AddTypesToRead(BarcodeType.Code128);

            //  Scan barcodes from the first page.
            int pageIndex = 0;
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc.GetPage(pageIndex));

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");