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.

Mon Dec 02, 2019 2:43 pm

I need to combine multiple PDFs so that one page has 4 PDFs in Visual Basic.net. How would I go about doing that?

metrofuse
 
Posts: 4
Joined: Mon Nov 11, 2019 4:58 pm

Tue Dec 03, 2019 2:26 am

Hi,

Thanks for your inquiry.
To help us investigate your issue accurately, please offer us the following information.
1. Your input Pdf files.
2. Your desired result file.

You could upload them here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Dec 05, 2019 2:35 am

Hi,

Thanks for your information via email.
Sorry that there is no direct way to merge several Pdf files into one page. But you could create templates from your Pdf files then draw them on the Pdf page. Below is the complete code for you.

Code: Select all
            String[] filePath = new String[]
            {
                filePath1 + "01.pdf",
                filePath2 + "02.pdf",
                filePath3 + "03.pdf",
                filePath4 + "04.pdf"
            };

            PdfDocument pdf1 = new PdfDocument();
            pdf1.LoadFromFile(filePath[0]);
            PdfPageBase p1 = pdf1.Pages[0];

            PdfDocument newPdf = new PdfDocument();
            //create a new page, we are going to draw the templates on it
            PdfPageBase newPage = newPdf.Pages.Add(new SizeF(p1.Size.Width * 2, p1.Size.Height * 2), new PdfMargins(0));
            //create a template based on the first page of the first Pdf file
            PdfTemplate template1 = p1.CreateTemplate();
            //draw the template on the new page
            newPage.Canvas.DrawTemplate(template1, new PointF(0, 0));

            //create a template based on the first page of the second Pdf file and draw it on specific location
            PdfDocument pdf2 = new PdfDocument();
            pdf2.LoadFromFile(filePath[1]);
            PdfPageBase p2 = pdf2.Pages[0];
            PdfTemplate template2 = p2.CreateTemplate();
            newPage.Canvas.DrawTemplate(template2, new PointF(p1.Size.Width,0));

            //create a template based on the first page of the third Pdf file and draw it on specific location
            PdfDocument pdf3 = new PdfDocument();
            pdf3.LoadFromFile(filePath[2]);
            PdfPageBase p3 = pdf3.Pages[0];
            PdfTemplate template3 = p3.CreateTemplate();
            newPage.Canvas.DrawTemplate(template3, new PointF(0, p1.Size.Height));

            //create a template based on the first page of the fourth Pdf file and draw it on specific location
            PdfDocument pdf4 = new PdfDocument();
            pdf4.LoadFromFile(filePath[3]);
            PdfPageBase p4 = pdf4.Pages[0];
            PdfTemplate template4 = p4.CreateTemplate();
            newPage.Canvas.DrawTemplate(template4, new PointF(p1.Size.Width, p1.Size.Height));

            //save the new file
            newPdf.SaveToFile(@"result.pdf", FileFormat.PDF);


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Wed Dec 11, 2019 12:00 am

Please assist with the code in VB.NET

metrofuse
 
Posts: 4
Joined: Mon Nov 11, 2019 4:58 pm

Wed Dec 11, 2019 1:55 am

Hi,

Thanks for your reply.
Below is the sample code in VB.Net for you.
Code: Select all
        Dim filePath As [String]() = New [String](){
            "..\01.pdf",
            "..\02.pdf",
            "..\03.pdf",
            "..\04.pdf"
        }
        Dim pdf1 As PdfDocument = New PdfDocument
        pdf1.LoadFromFile(filePath(0))
        Dim p1 As PdfPageBase = pdf1.Pages(0)
        Dim newPdf As PdfDocument = New PdfDocument
        'Create a new page, we are going to draw the template on it
        Dim newPage As PdfPageBase = newPdf.Pages.Add(New SizeF((p1.Size.Width * 2), (p1.Size.Height * 2)), New PdfMargins(0))
        'Create a template based on the first page of the first Pdf file
        Dim template1 As PdfTemplate = p1.CreateTemplate
        'Draw the template on the new page
        newPage.Canvas.DrawTemplate(template1, New PointF(0, 0))

        'Create a template based on the first page of the second Pdf file and draw it on specific location
        Dim pdf2 As PdfDocument = New PdfDocument
        pdf2.LoadFromFile(filePath(1))
        Dim p2 As PdfPageBase = pdf2.Pages(0)
        Dim template2 As PdfTemplate = p2.CreateTemplate
        newPage.Canvas.DrawTemplate(template2, New PointF(p1.Size.Width, 0))

        'Create a template based on the first page of the third Pdf file and draw it on specific location
        Dim pdf3 As PdfDocument = New PdfDocument
        pdf3.LoadFromFile(filePath(2))
        Dim p3 As PdfPageBase = pdf3.Pages(0)
        Dim template3 As PdfTemplate = p3.CreateTemplate
        newPage.Canvas.DrawTemplate(template3, New PointF(0, p1.Size.Height))

        'Create a template based on the first page of the fourth Pdf file and draw it on specific location
        Dim pdf4 As PdfDocument = New PdfDocument
        pdf4.LoadFromFile(filePath(3))
        Dim p4 As PdfPageBase = pdf4.Pages(0)
        Dim template4 As PdfTemplate = p4.CreateTemplate
        newPage.Canvas.DrawTemplate(template4, New PointF(p1.Size.Width, p1.Size.Height))

        'Save the new file
        newPdf.SaveToFile("result.pdf", FileFormat.PDF)

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF