Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 22, 2021 2:56 pm

I have a large PDF that contains customer notices. The notice for each customer has an unknown number of pages. I need to split the PDF into the individual customer notices. The first page for each customer contains their name, account number, address, etc. The remaining pages contain account disclosures as well as images of checks written for their account.

Greg_Ledford
 
Posts: 2
Joined: Wed Apr 21, 2021 6:41 pm

Fri Apr 23, 2021 2:21 am

Hello,

Thanks for your inquiry!

You can first mark the page index by some unique information like names which contained in the first page of each of your customer information, and then draw the pages of each customer to a new PDF according to the page index. Here I attached a simple demo.
Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(filepath);

            PdfTextFind[] findResult = null;
            List<int> index = new List<int>();
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Find text
                findResult = page.FindText(findString, TextFindParameter.WholeWord).Finds;
                foreach (PdfTextFind find in findResult)
                {
                    int pageindex = find.SearchPageIndex;
                    index.Add(pageindex);
                }
            }

            for (int i = 0; i < index.Count; i++)
            {
                int currentIndex = index[i];
                int nextIndex = 0;
                if (i.Equals(index.Count - 1))
                {
                    nextIndex = pdf.Pages.Count;
                }
                else
                {
                    nextIndex = index[i + 1];
                }
               
                PdfDocument newpdf = new PdfDocument();

                for (int j = currentIndex; j < nextIndex; j++)
                {
                    PdfPageBase page = pdf.Pages[j];
                    PdfPageBase newPage = newpdf.Pages.Add(page.ActualSize, new PdfMargins(0));
                    page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, 0));
                }

                newpdf.SaveToFile("custom" + i + ".pdf", FileFormat.PDF);
                newpdf.Dispose();
            }


To help us make a demo that meets your needs better, please provide us with your input file for reference. To protect your privacy, you can overwrite or delete the sensitive information in your file, and then attached there or send it to us via email ([email protected]). Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Apr 23, 2021 2:38 pm

This worked perfectly! Thanks!!

Greg_Ledford
 
Posts: 2
Joined: Wed Apr 21, 2021 6:41 pm

Sun Apr 25, 2021 1:18 am

Hello,

Thanks for your feedback, and glad to hear that the code is working.

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF

cron