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.

Wed Jul 08, 2020 8:52 pm

Hi

Currently i am able to insert pages into another PDF file but only after the last page. This Insert page feature seems more like behaving like appending. Is there a way for me to say from page 2 of File A insert into after page 3 of File B (page 3 is not the last page of File B).

Thanks

wonglau7479
 
Posts: 4
Joined: Sat Jul 04, 2020 8:49 am

Thu Jul 09, 2020 3:13 am

Hello,

Thanks for your inquiry.
Please refer to the code below. If this does not meet your needs well, please provide your input file as well as your desired output for our better reference.
Code: Select all
            PdfDocument pdfA = new PdfDocument();
            pdfA.LoadFromFile("FileA.pdf");

            PdfDocument pdfB = new PdfDocument();
            pdfB.LoadFromFile("FileB.pdf");

            //Get the page 2 of File A
            PdfPageBase pageA = pdfA.Pages[1];

            //Insert a new page after page 3 of File B
            PdfPageBase pageB = pdfB.Pages.Insert(3, pdfB.Pages[0].Size);

            //Draw template
            pageB.Canvas.DrawTemplate(pageA.CreateTemplate(), PointF.Empty);

            pdfB.SaveToFile("Result.pdf", FileFormat.PDF);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Jul 09, 2020 7:46 am

Cool. Thanks Rachel. I managed to get it work for my VB.NET with your coding.

wonglau7479
 
Posts: 4
Joined: Sat Jul 04, 2020 8:49 am

Thu Jul 09, 2020 9:25 am

Hello,

Thanks for your response.
The following is the code in VB for your reference. If there is any question, please feel free to write back.
Code: Select all
        Dim pdfA As PdfDocument = New PdfDocument
        pdfA.LoadFromFile("FileA.pdf")
        Dim pdfB As PdfDocument = New PdfDocument
        pdfB.LoadFromFile("FileB.pdf")
        Dim pageA As PdfPageBase = pdfA.Pages(1)
        Dim pageB As PdfPageBase = pdfB.Pages.Insert(3, pdfB.Pages(0).Size)
        'Draw template
        pageB.Canvas.DrawTemplate(pageA.CreateTemplate, PointF.Empty)
        pdfB.SaveToFile("Result.pdf", FileFormat.PDF)


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF