Free Support Forum - aspose.com

Split PDF Document as per Barcodes on different pages

How to Split a Multiple page Image PDF into single PDFs via Barcode recognition (using Barcodes as separators) and name the split PDF files as per Barcode Text ?

@bhuwaneshp,

Kindly send a sample of the PDF document and elaborate a bit more about how you need to split the PDF document. For example, if there is a barcode on page X (X is a page number), then do you only need to separate this page X and save into the PDF format with the same name as of the recognized barcode.

Hi I am sending this sample, where we want to split the pdf as per the barcode. In this PDF, The Barcode represents an Order and the Order can be of any number of pages.(Here, it is 2). Each PDF can have multiple number of orders, like (30 pages containing 12 orders i.e. 12 Barcodes)
So I have to split the file into {BarcodeNumber}.pdf Sample.pdf (484.9 KB)

@bhuwaneshp,

With Aspose.PDF API, you can convert each page to an image, and then pass an image stream to Aspose.BarCode API. Aspose.BarCode API will scan this image to find the barcode, and you can apply checks in the source code to split the PDF as follows:
C#

string dataDir = @"C:\Pdf\test831\";
Document pdfDocument = new Document(dataDir + "Sample.pdf");
Document document = new Document();
bool newDoc = false;
string filename = "filename";
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
    using (MemoryStream pageStream = new MemoryStream())
    {
        // Create JPEG device with specified attributes
        // Width, Height, Resolution, Quality
        // Quality [0-100], 100 is Maximum
        // Create Resolution object
        Resolution resolution = new Resolution(300);
        JpegDevice jpegDevice = new JpegDevice(resolution, 100);
        // Convert a particular page and save the image to stream
        jpegDevice.Process(pdfDocument.Pages[pageCount], pageStream);

        // Create an instance of BarCodeReader and set image and symbology type to recognize
        BarCodeReader barCodeReader = new BarCodeReader(pageStream, DecodeType.Code128);

        // if there is a barcode, then Read method will return true
        if (barCodeReader.Read())
        {
            if (newDoc == false)
            {
                newDoc = true;
                filename = barCodeReader.GetCodeText();
                document.Pages.Add(pdfDocument.Pages[pageCount]);
            }
            else
            {
                document.Save(dataDir + filename + ".pdf");
                document = new Document();
                newDoc = true;
                filename = barCodeReader.GetCodeText();
                document.Pages.Add(pdfDocument.Pages[pageCount]);
            }
        }
        else
            // if true, already building a PDF
            if (newDoc)
            {
                document.Pages.Add(pdfDocument.Pages[pageCount]);
                if (pageCount == pdfDocument.Pages.Count)
                {
                    document.Save(dataDir + filename + ".pdf");
                }
            }
      // Close the reader
      barCodeReader.Close();
    }
}

This is the ZIP of output PDF documents: OutputPDFs.zip (361.5 KB)