Add Blank Page To PDF with specific size in C# using Aspose.PDF

I am trying to add a blank page to a PDF and it’s not coming out with the correct page size. I followed instructions that I found on Aspose.com, but the added page comes out smaller than the original document. Here is a screenshot of what it looks like. SecondPageSmaller.png (24.2 KB)

Here is a copy of the PDF I am using: Doc1.pdf (30.6 KB)

Here is the output PDF I am getting: output.pdf (63.6 KB)

Here is the code that I’m using:

using (Document basePDFdocTemp = new Document(@“C:\temp\pdf\Doc1.pdf”))
{
foreach (int pageNumber in Enumerable.Range(2, 1))
{
Page newPage = basePDFdocTemp.Pages.Insert(pageNumber);
newPage.PageInfo.Height = basePDFdocTemp.Pages.First().PageInfo.Height;
newPage.PageInfo.Width = basePDFdocTemp.Pages.First().PageInfo.Width;
}

            basePDFdocTemp.Save(@"C:\temp\pdf\output.pdf");
        }

Please let me know what I am doing wrong! Thanks!

@Robert343

Thank you for contacting support.

We would like to update you that PageInfo class contains default values which can be changed while creating new pages. Whereas, you may retrieve meta information with PdfFileInfo class. Please try using below code snippet to read actual width and height of pages.

Document document = new Document(dataDir + "YOUR_LOGO.pdf");
PdfFileInfo info = new PdfFileInfo(document);
Console.WriteLine("Width: " + info.GetPageWidth(document.Pages[1].Number));
Console.WriteLine("Height: " + info.GetPageHeight(document.Pages[1].Number));
OR
Console.WriteLine("Height: " + info.GetPageHeight(1));

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

How would I use this to set the page width? The problem I have is that when I use the Pages.Insert() method, it creates a page with a different width. See the above attachment SecondPageSmaller.png.

@Robert343

We have updated your code snippet based on above suggestion and the page is not smaller anymore. Please feel free to contact us if you need any further assistance.

using (Document basePDFdocTemp = new Document(dataDir + @"Doc1.pdf"))
{
    PdfFileInfo info = new PdfFileInfo(basePDFdocTemp);
    var Width = info.GetPageWidth(basePDFdocTemp.Pages[1].Number);
    var Height = info.GetPageHeight(basePDFdocTemp.Pages[1].Number);
    Page newPage = basePDFdocTemp.Pages.Insert(2);
    newPage.SetPageSize(Width , Height);
    basePDFdocTemp.Save(dataDir + @"InsertPageOutput_19.5.pdf");
}

We have also attached generated PDF document for your kind reference. InsertPageOutput_19.5.pdf

Okay, now that worked good. Thank you!