Pdf ensure A4 Portrait pages from other page sizes and orientations

Hi,


I was trying to load a pdf document, save it again as a pdf, ensuring that I have all pages in A4 Format, portrait mode.

As input I have a pdf, for instance with two A4 pages in portrait mode, and a third A3 page in landscape. So I expect to understand by code, that the third page should be rotated, and scaled in order to fit in a A4 portrait page.

I am not finding the correct code to make this working.
I already used pdffileeditor resizecontents function, I already used page.rotate … but I couldn’t make this work.

Can you help me?

Hi Mario,


Thanks for contacting support.

I will appreciate if you please share your sample code snippet along with your both input files. It will help us to understand your requirement exactly and address it accordingly.

We are sorry for the inconvenience.

Best Regards,

Hi,


The input pdf, which has two a4 pages, and a third a3 landscape page was already attached.

Regarding the code it is not working, but we have a function that will receive a binary pdf, and it will receive a pagesize ‘A4’ tag, if this is used, it should return a pdf where all pages are in A4 format.

So the third page, should be rotated, and scaled to fit on a A4 portrait page.

public void MssPDF_DefinePageSize(byte[] ssSourcePDF, string ssPageSize, out byte[] ssResizedPDF, out string ssErrorMessage)

I attached the code with several attempts to achieve this:

My first guess and easy to explain:

#atempt1:

/create PdfPageEditor object
PdfPageEditor pEdit = new PdfPageEditor();
//bind pdf file
pEdit.BindPdf(inputFileMemStream);
//PageSize property of the PdfPageEditor class
if (ssPageSize.ToUpper().Equals(“A4”))
{
pEdit.PageSize = Aspose.Pdf.PageSize.A4;
ssErrorMessage = string.Empty;
}
else {
ssErrorMessage = “The requested page format is unknown.”;
}
MemoryStream temp = new MemoryStream();
pEdit.Save(temp);
ssResizedPDF = temp.ToArray();
pEdit = null;

This do not scale or rotate, the a3 page, so i got a A4 pdf but the the A3 page cut.

#atemp2 this was not to solve the issue, but to check that the print method with autoresize and autorotate produces a good a4 pdf without cuts… it was something like this that i pretend as a result. (but I don’t want to print, I want a pdf as a result)

#atempt 3, I tried to use isLandscape as a way to detect that i am with a landscape page, that i need to rotate… and scale (the a3 page, is not detected as Landscape), and i do not now very well how to scale.

#atempt 4, by the size of the page, i tried to understand that i am on my landscape a3 page, that o need to rotate… and i tried to use pdfEditor ResizeContents to make the resize for the A4 pages.

Well, What should be the approach and the code to ensure that at the end, all pages at the end are in A4 portrait format.

Hi Mario,


Thanks for sharing further details.

I have tested the scenario with following code and have managed to change the pageSize and rotation of the page without any issue. I have also attached the generated PDF document for your reference. You can scale the PDF content accordingly.

C#

PdfPageEditor pEdit = new PdfPageEditor();

// Bind pdf file
pEdit.BindPdf(dataDir + “A4A3pages.pdf”);

// Change page size of the selected pages
pEdit.ProcessPages = new int[] { 3 };

// Here we select a member named ‘LETTER’ from the list of members of PageSize class and assign it to PageSize property of the PdfPageEditor class
pEdit.PageSize = PageSize.A4;

pEdit.Rotation = 270;

pEdit.MovePosition(-50, -20);

pEdit.HorizontalAlignment = HorizontalAlignment.Left;

// Save the file
pEdit.Save(dataDir + “ChangePageSizes_out12.pdf”);

If you still face any issue, please feel free to contact us.

Best Regards,

Hi,


I will test this code, thanks, but it is not a complete solution.

I will need a more generic way:

- Meaning of (-50,-20) This values are generic?
- How to discovery the pages to Rotate/Resize? I need to fill the array of ProcessPages according to your solution.

Examples:
Example 1: page 1 A4 portrait, page 2 A4 portrait, page 3 A3 Landscape
Example 2: page 1 A4 portrait, page 2 A4 landscape, page 3 A4 portrait etc…

I do not control the pages that I have, but at the end I want All pages in A4 format uncut(with my simple atempt I got a page cut). So the landscape pages (should be rotated, I think, but i do not have sure if this is needed…) but pages on different sizes should be resized to fit A4 pages.

What is the best way?

Hi Mario,


You can use .isLandscape property to determine whether the page is Portrait or Landscape and then you can set the pageSize accordingly. Also, you can use Zoom property to set the page property Zoom also to accomplish your needs. I have modified the above code to make it generic for your reference. I have also attached the generated output PDF document.

.NET

Document doc = new Document(dataDir + “A4A3pages.pdf”);
PdfPageEditor pEdit = new PdfPageEditor();
// Bind pdf file
pEdit.BindPdf(dataDir + “A4A3pages.pdf”);
// Change page size of the selected pages
int total_pages = pEdit.GetPages();
for(int i=1; i <=total_pages; i++)
{
pEdit.ProcessPages = new int[] { i };

// Here we select a member named ‘LETTER’ from the list of members of PageSize class and assign it to PageSize property of the PdfPageEditor class
if (pEdit.GetPageSize(i).IsLandscape)
{
pEdit.PageSize = PageSize.A4;
pEdit.Rotation = 270;
pEdit.Zoom = 0.7f;
}
}

pEdit.HorizontalAlignment = HorizontalAlignment.Left;
// Save the file
pEdit.Save(dataDir + “ChangePageSizes_output.pdf”);
pEdit.Close();