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.

Tue Feb 23, 2021 4:38 pm

Hi,
I'm trying to merge multiple pdf files, each of those has a different page size. First file has A4 size, the othes are different (in general).
When I use PdfDocument.MergeFiles all pages of the result pdf are A4 size.

I have also tried the following way, with no success. Pages remain A4 size.
Code: Select all
        // doc = new Spire.Pdf.PdfDocument(files.First().Content); file where all pages are A4 size
        byte[] content = File.ReadAllBytes(filename); // file with different size than A4
        Spire.Pdf.PdfDocument tempDoc = new Spire.Pdf.PdfDocument(content);
   foreach (Spire.Pdf.PdfPageBase page in tempDoc.Pages)
   {
      Spire.Pdf.PdfPageBase newPage = doc.Pages.Add(page.Size, new Spire.Pdf.Graphics.PdfMargins(0));

      Spire.Pdf.Graphics.PdfMetafileLayoutFormat loLayout = new Spire.Pdf.Graphics.PdfMetafileLayoutFormat();
      loLayout.Layout = Spire.Pdf.Graphics.PdfLayoutType.OnePage;
      loLayout.Break = PdfLayoutBreakType.FitPage;
      page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
   }


For every pdf I'd like to keep the original size. How can I do that?


Thanks in advance,
Elia

eliamuntoni
 
Posts: 10
Joined: Tue Feb 16, 2021 9:40 am

Wed Feb 24, 2021 2:17 am

Hello,

Thanks for your inquiry!

I created an A4, an A3, and a custom-sized PDF documents, and merged them using the PdfDocument.MergeFiles() method with the latest Spire.PDF V7.2.9. But each page in the result PDF keeps its original size. If you were not using the latest version, I first recommend you have a try.

Besides, you can also refer to the following code to merge the PDF files with different page size. If the issue still exists, to help us reproduce your issue that the size of all pages of the result PDF generated by the method is A4, please provide us with your input files for reference. Thanks in advance.
Code: Select all
            String[] files = new String[] { "E:\\testdoc\\sample.pdf", "E:\\testdoc\\sample1.pdf", "E:\\testdoc\\SampleA3.pdf" };
            PdfDocument[] docs = new PdfDocument[files.Length];
            for( int i = 0; i < files.Length; i++)
            {
                docs[i] = new PdfDocument();
                docs[i].LoadFromFile(files[i]);
            }
            for (int i = 1; i < files.Length; i++)
            {
                docs[0].AppendPage(docs[i]);
            }
            docs[0].SaveToFile("MergeDiffSizeByMethod.pdf", FileFormat.PDF)


Sincerely,
Marcia
E-iceblue support team
User avatar

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

Wed Feb 24, 2021 11:27 am

Hi,
I have tried your code and it works!

Thank you so much,
Elia

eliamuntoni
 
Posts: 10
Joined: Tue Feb 16, 2021 9:40 am

Thu Feb 25, 2021 1:05 am

Hello,

Glad to hear that your issue has been solved.

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