ByteScout Barcode Reader SDK - C# - Split PDF Document By Found Barcode - ByteScout

ByteScout Barcode Reader SDK – C# – Split PDF Document By Found Barcode

  • Home
  • /
  • Articles
  • /
  • ByteScout Barcode Reader SDK – C# – Split PDF Document By Found Barcode

How to split PDF document by found barcode in C# with ByteScout BarCode Reader SDK

This code in C# shows how to split PDF document by found barcode with this how to tutorial

The coding tutorials are designed to help you test the features without need to write your own code. ByteScout BarCode Reader SDK can split PDF document by found barcode. It can be used from C#. ByteScout BarCode Reader SDK is the SDK for reading of barcodes from PDF, images and live camera or video. Almost every common type like Code 39, Code 128, GS1, UPC, QR Code, Datamatrix, PDF417 and many others are supported. Supports noisy and defective images and docs. Includes optional documents splitter and merger for pdf and tiff based on found barcodess. Batch mode is supported for superior performance using multiple threads. Decoded values are easily exported to JSON, CSV, XML and to custom format.

The SDK samples like this one below explain how to quickly make your application do split PDF document by found barcode in C# with the help of ByteScout BarCode Reader SDK. In order to implement the functionality, you should copy and paste this code for C# below into your code editor with your app, compile and run your application. You can use these C# sample examples in one or many applications.

You can download free trial version of ByteScout BarCode Reader SDK from our website to see and try many others source code samples for C#.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using System; using System.IO; using System.Text; using Bytescout.BarCodeReader; namespace SplitDocumentByFoundBarcode { class Program { const string InputFIle = "Barcodes.pdf"; static void Main() { Console.WriteLine("Reading barcode(s) from PDF document {0}", Path.GetFullPath(InputFIle)); // Create Bytescout.BarCodeReader.Reader instance Reader reader = new Reader(); reader.RegistrationName = "demo"; reader.RegistrationKey = "demo"; // Set barcode type to find reader.BarcodeTypesToFind.Code39 = true; /* ----------------------------------------------------------------------- NOTE: We can read barcodes from specific page to increase performance. For sample please refer to "Decoding barcodes from PDF by pages" program. ----------------------------------------------------------------------- */ // Find barcode in PDF document reader.ReadFrom(InputFIle); // Method 1: Split PDF document in two parts by found barcode // NOTE: In Full version of the SDK this method is unlocked in "PRO" license type only. reader.SplitDocument(@"barcodes.pdf", @"part1.pdf", @"part2.pdf", reader.FoundBarcodes[0].Page + 1); // Method 2: Extract page containing the barcode from PDF document // NOTE: In Full version of the SDK this method is unlocked in "PRO" license type only. reader.ExtractPageFromDocument(@"barcodes.pdf", @"extracted_page.pdf", reader.FoundBarcodes[0].Page + 1); // Method 3: Split PDF document into parts in one pass. // NOTE: In Full version of the SDK this method is unlocked in "PRO" license type only. StringBuilder pageRanges = new StringBuilder(); // Create string containing page ranges to extract in the form "1-4,6-8,10-11,12-". Page numbers are 1-based! for (int i = 0; i < reader.FoundBarcodes.Length; i++) { FoundBarcode barcode = reader.FoundBarcodes[i]; // Add pages before the first barcode found if (i == 0 && barcode.Page > 0) { pageRanges.Append("1"); if (barcode.Page > 1) { pageRanges.Append("-"); pageRanges.Append(barcode.Page); } pageRanges.Append(","); } // Add page with barcode pageRanges.Append(barcode.Page + 1); // +1 because we skip the page with barcode and another +1 because need 1-based page numbers // Add range untill the next barcode if (i < reader.FoundBarcodes.Length - 1) { if (reader.FoundBarcodes[i + 1].Page - barcode.Page > 1) { pageRanges.Append("-"); pageRanges.Append(reader.FoundBarcodes[i + 1].Page); } pageRanges.Append(","); } else // for the last found barcode add ending "-" meaning "to the last page" { pageRanges.Append("-"); } } // Split document string[] splittedParts = reader.SplitDocument(@"barcodes.pdf", pageRanges.ToString()); // The method returns array of file names. Rename files as desired. foreach (var fileName in splittedParts) { Console.WriteLine(fileName); } // Cleanup reader.Dispose(); Console.Read(); Console.WriteLine("Press enter key to exit..."); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout BarCode Reader SDK Home Page

Explore ByteScout BarCode Reader SDK Documentation

Explore Samples

Sign Up for ByteScout BarCode Reader SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout BarCode Reader SDK Home Page

Explore ByteScout BarCode Reader SDK Documentation

Explore Samples

Sign Up for ByteScout BarCode Reader SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next