SetPageSize doesn't work

Hi, Support:

There is a issue like this:

Dim InputPdf as new Aspose.pdf.Document(InputPdfFile)
Dim W as integer=InputPdf.Pages(1).PageInfo.width
Dim H as integer=InputPdf.Pages(1).PageInfo.Height
Dim PagesCount as integer=InputPdf.Pages.Count
’ PagesCount , here PagesCount is 1
InputPdf .Pages.add()
PagesCount =InputPdf.Pages.Count
’ PagesCount , here PagesCount is 2
Dim NewPage as page=InputPdf .Pages(InputPdf .Pages.count)
NewPage.setPageSize(W,H)
Dim NeWW as integer=InputPdf.Pages(2).PageInfo.width
Dim NewH as integer=InputPdf.Pages(2).PageInfo.Height

'Here is a bug that the NewW is not equal to H and NewH is not equal to H, and NewW is bigger than H and NewH is bigger than H, which means the method NewPage.setPageSize(W,H) doesn’t work.

@ducaisoft

It seems you are experiencing an issue with the setPageSize method not functioning as expected when adding a new page to your PDF document. Based on your code, you are trying to set the size of the newly added page to match the dimensions of the first page, but it appears that the dimensions are not being applied correctly.

Here are a few suggestions to troubleshoot and resolve the issue:

  1. Check Page Size Units: Ensure that the dimensions you are using (width and height) are in the correct units. In Aspose.PDF, the dimensions are typically in points, where 1 inch equals 72 points. If you are using different units, you may need to convert them accordingly.

  2. Use the Correct Method: Instead of using NewPage.setPageSize(W, H), you might want to ensure that you are using the correct method to set the page size. For example, you can set the page size using the PageSize property directly on the page object.

  3. Example Code: Here’s a simple example of how to set the page size correctly:

    Dim InputPdf as new Aspose.pdf.Document(InputPdfFile)
    Dim W as integer = InputPdf.Pages(1).PageInfo.Width
    Dim H as integer = InputPdf.Pages(1).PageInfo.Height
    
    ' Add a new page
    InputPdf.Pages.Add()
    Dim NewPage as Page = InputPdf.Pages(InputPdf.Pages.Count)
    
    ' Set the page size
    NewPage.PageInfo.Width = W
    NewPage.PageInfo.Height = H
    
    ' Verify the new dimensions
    Dim NewW as integer = NewPage.PageInfo.Width
    Dim NewH as integer = NewPage.PageInfo.Height
    
  4. Check for Updates: Ensure that you are using the latest version of the Aspose.PDF library, as bugs and issues are often resolved in newer releases.

If the problem persists after trying these suggestions, consider providing additional details about the version of the library you are using and any error messages you may be encountering. This information can help in diagnosing the issue further.

Feel free to reach out if you need more assistance!

Sources:
[1]: SetOutputFileDimensions.cs
[2]: UpdatePageDimensions.java

Do as your suggestion, however, the pagesize method doesn’t work.

@ducaisoft

Can you please share the sample PDF document for our reference so that we can test the scenario in our environment and address it accordingly?

Also, please try to use below properties to see if page dimensions are changed:

Aspose.Pdf.Rectangle r = page.GetPageRect(true);
double newHeight = r.Width;
double newWidth = r.Height;

Please see the details based on VB.net and pdf.dll(23.4).
-----------------------code begin--------------------------------------
Private Function AddBlankPdfPageAndResize(byval OutFile as string=“OutSample.pdf”,Optional byval SrcPdfFile as string=“Sample.pdf”, Optional byval AddImageOrStamp as string=“”)

        Dim InputSrcPdf As New Aspose.Pdf.Document(SrcPdfFile )
        Dim OutPutPdf As New Aspose.Pdf.Document()
        Dim iW As Integer = InputSrcPdf.Pages(1).PageInfo.Width
        Dim iH As Integer = InputSrcPdf.Pages(1).PageInfo.Height
        OutPutPdf.Pages.Add(InputSrcPdf.Pages(1))
        OutPutPdf.Pages.Add()   'this is to add or insert a blank pdf page
        Dim oW As Integer = OutPutPdf.Pages(2).PageInfo.Width
        Dim oH As Integer = OutPutPdf.Pages(2).PageInfo.Height
        'Note 1: here the iW is equal oW  and iH is equal to oH, whereas the Page size of OutPutPdf.Pages(2) is really bigger than iWxiH
        Dim NewPage As Page = OutPutPdf.Pages(2)
        NewPage.PageInfo.Width = iW
        NewPage.PageInfo.Height = iH
        NewPage.SetPageSize(iW, iH) 'Note 2: this is to resize page size of OutPutPdf.Pages(2), but fail.
     ' TODO: Herer is to add imagestamp or image to the page
        OutPutPdf.Save(OutFile )

End Function
--------------------end code-----------------------------------------
This function is to insert or add a new blank pdf page,and then resize its page size,as well as add imagestamp or image on it. I found there may be two issues(Please see note 1 and note 2).

Please tell me how to reach this purpose, or fix the dll.
Sample.pdf (55.2 KB)

@ducaisoft

Please check the output generated by below code where we have used same options that we suggested you above.

Document inputSrcPdf = new Document(dataDir + "Sample.pdf");
Document outputPdf = new Document();

int iW = (int)inputSrcPdf.Pages[1].GetPageRect(true).Width;
int iH = (int)inputSrcPdf.Pages[1].GetPageRect(true).Height;

outputPdf.Pages.Add(inputSrcPdf.Pages[1]);
outputPdf.Pages.Add();  // Adds or inserts a blank PDF page

int oW = (int)outputPdf.Pages[2].PageInfo.Width;
int oH = (int)outputPdf.Pages[2].PageInfo.Height;

Page newPage = outputPdf.Pages[2];
newPage.PageInfo.Width = iW;
newPage.PageInfo.Height = iH;
newPage.SetPageSize(iW, iH);

outputPdf.Save(dataDir + "out.pdf");

out.pdf (8.7 KB)

That’s ok!
The real w and h of the page must be gotten by the method of inputSrcPdf.Pages[1].GetPageRect(true).Width, not by outputPdf.Pages[2].PageInfo.Width.

1 Like