Free Support Forum - aspose.com

Changing Page Orientation to Landscape does not change Page Size

var doc = new Document();
this.pdfDocument.PageInfo.IsLandscape = true;

After setting the orientation to landscape I would expect the document.PageInfo.Width to be Greater than document.PageInfo.Height but it is still same as its default. They should have swapped.

What is an easy way to Update the Width and Height of the whole document.

I don't want to do it in Page by Page basis because my pages are created by importing data from another html so I don't have control on the pages as shown below

Document doc1 = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), htmlLoadOptions);

doc.Pages.Add(doc1.Pages);

Hi,


Thanks for using our API’s.

In order to change the page orientation, you need to iterate through all the pages in PDF file. Once you have imported HTML contents inside Document object, you may consider using either of the following approaches.

  • To change page size customer may use Page.Rect property:
Document doc = new Document(“input.pdf”); //original page is A4
doc.Pages[1].Rect = new Rectangle(0, 0, 421, 595); // A5 size
doc.Save(“output.pdf”);

But, page cropping will occurs if new page size is smaller then original page size.
  • To change page orientation, customer may use Page.Rotation:

Document doc = new Document(“input.pdf”); //original page is A4

doc.Pages[1].Rotation = Rotation.on270;
doc.Save(“output.pdf”);

(page contents will be rotated, and page orientation became landscape if it was portrait)

  • Example of PdfPageEditor usage (but page may be cropped in this case)

PdfPageEditor ppe = new PdfPageEditor();

ppe.BindPdf(“input.pdf”);
PageSize ps = new PageSize(421, 595);
ps.IsLandscape = true;
ppe.PageSize = ps;
ppe.Save(“output.pdf”);

  • As mentioned in forum link, customer has some pages A3 which need to be converted as A4.
To solve this task, PdfFileEditor.ResizeContents may be used.
ResizeContents(String source, String destination, int[] pages, ContentsResizeParameters parameters)
source = source file name;
destination = destination file name;
pages = array of page number which should be converted;
parameters = instance of ContentsResizeParameters. This object describes parameters of page/contents re-size.
It contains size elements: new page width and height and left/right/top/bootom margins.
Each of these element is ContentsResizeValue instance. ContentsResizeValue describes required resize algorithm:
ContentsResizeValue.Units(value) - creates resize value specified as absolute value in PDF document default units (pixels);
ContentsResizeValue.Percents(value) - creates resize value specified in percents of initial page size;
ContentsResizeValue.Auto() - creates resize value which will be automatically calculated (as initial page size - explicitly defined elements).
Thus, customer task may be solved in the following way:

[C#]

PdfFileEditor pfe = new PdfFileEditor();
pfe.ResizeContents(“input.pdf”, “output.pdf”,
new int[] { 1 },
new PdfFileEditor.ContentsResizeParameters(
PdfFileEditor.ContentsResizeValue.Units(0), //left margin
PdfFileEditor.ContentsResizeValue.Units(421), //new page width - for A5
PdfFileEditor.ContentsResizeValue.Units(0), //right margin
PdfFileEditor.ContentsResizeValue.Units(0), //top margin
PdfFileEditor.ContentsResizeValue.Units(595), //new page height - for A5
PdfFileEditor.ContentsResizeValue.Units(0) //bottom margin

));

To make the code more easy, method PdfFileEditor.ContentsResizeParameters.PageResize() was added. This method requires two parameters (new page width and height)

[C#]
PdfFileEditor pfe = new PdfFileEditor();
pfe.ResizeContents(“input.pdf”, "output.pdf"
new int[] { 1 },
PdfFileEditor.ContentsResizeParameters.PageResize(421, 595));

How about to create a new pdf Document in LandScape to begin with?

I am not necessarily converting a portrait document. But creating a new pdf document all in landscape.

Do I have to create a Portrait and conver it to Landscape? I should be able to create a Landscape pdf document with appropriate Pages sizes.


Hi Helen,


Thanks for your inquiry. If you want to set page orientation while creating a PDF document from scratch, you need to set PageInfo of Page object as following. As contents will flow to next page automatically so page setting will implement on all pages automatically until unless you create a new Page object. Hopefully it will help you to accomplish the task.

Document doc = new
Document();<o:p></o:p>

Aspose.Pdf.Page page = doc.Pages.Add();

page.PageInfo = new Aspose.Pdf.PageInfo

{

Width = Aspose.Pdf.PageSize.A4.Height,

Height = Aspose.Pdf.PageSize.A4.Width,

};

....

....

Please feel free to contact us for any further assistance.


Best Regards,