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.

Fri Feb 17, 2023 1:19 am

Super easy one but I can't seem to get this working
I have a PDF with 100 pages
I want it to break it into two documents
one with pages 1-50 and the other would be pages 51-100
how would I do that?

colinmhart
 
Posts: 15
Joined: Thu Aug 18, 2022 7:19 pm

Fri Feb 17, 2023 10:29 am

Hi,

Thanks for your inquiry.
Please see the following code for reference.
Code: Select all
            PdfDocument oldPdf = new PdfDocument();
            oldPdf.LoadFromFile("old.pdf");
            PdfDocument newPdf1 = new PdfDocument();
            PdfDocument newPdf2 = new PdfDocument();
            PdfPageBase page1;
            PdfPageBase page2;

            for (int i = 0; i < 100; i++)
            {
                if (i < 50)
                {
                    //Add same size page for newPdf
                    page1 = newPdf1.Pages.Add(oldPdf.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));

                    //Create template of the oldPdf page and draw into newPdf page
                    oldPdf.Pages[i].CreateTemplate().Draw(page1, new System.Drawing.PointF(0, 0));
                }
                else
                {
                    //Add same size page for newPdf
                    page2 = newPdf2.Pages.Add(oldPdf.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));

                    //Create template of the oldPdf page and draw into newPdf page
                    oldPdf.Pages[i].CreateTemplate().Draw(page2, new System.Drawing.PointF(0, 0));
                }
               
            }

            String result1 = "new1.pdf";
            String result2 = "new2.pdf";

            //Save the document
            newPdf1.SaveToFile(result1);
            newPdf2.SaveToFile(result2);


If you have any other questions related to our products, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Thu Feb 23, 2023 10:22 pm

when I run this, I get the following error
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=The index can not be less then zero or greater then Count.
Parameter name: index
Source=Spire.Pdf

colinmhart
 
Posts: 15
Joined: Thu Aug 18, 2022 7:19 pm

Thu Feb 23, 2023 10:37 pm

nevermind, I Got it
thanks

colinmhart
 
Posts: 15
Joined: Thu Aug 18, 2022 7:19 pm

Fri Feb 24, 2023 1:22 am

Hi,

Thanks for your feedback.
If there are any questions related to our products, just feel free to contact us, we are will to provide you with the technical supports.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Fri Feb 24, 2023 2:21 am

I actually emailed the project to you, as I had another question
thanks

colinmhart
 
Posts: 15
Joined: Thu Aug 18, 2022 7:19 pm

Fri Feb 24, 2023 9:57 am

Hi,

Thanks for you inquiry.
My colleague Abel will reply to you via email.

Sincerely,
Trite
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF