We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert html to pdf with landscape orientation

I am trying to convert html to a pdf in landscape orientation, but it is always come out as portrait.

My code is pretty simple:

public static Document GetPdfDocumentFromHtml(string html, HtmlLoadOptions options = null)
{
ApplyLicense();

if (options == null)
{
options = new HtmlLoadOptions();
options.PageInfo.Margin.Bottom = 10;
options.PageInfo.Margin.Top = 20;
options.PageInfo.Margin.Left = 10;
options.PageInfo.Margin.Right = 10;
options.PageInfo.IsLandscape = false;
}
Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), options);

return doc;
}

I am passing in an options object with options.PageInfo.IsLandscape = false and this is staying as expected. I expect I am missing something simple, but everything i found has been old and the objects that they use no longer exist.

Thanks,
Logan

Hi Logan,


Thanks for contacting support.

The PageInfo.IsLandscape does not change size/orientation of page. This property is used only by Generator namespace. However in order to set the size of page, you should set MediaBox of the page. For example:

[C#]

Document doc = new Document(“Sample.pdf”);<o:p></o:p>

double w = doc.Pages[1].Rect.Width;<o:p></o:p>

double h = doc.Pages[1].Rect.Height;<o:p></o:p>

doc.Pages[1].MediaBox = new Rectangle(0, 0, h * h / w, h);<o:p></o:p>

doc.Pages[1].CropBox = new Rectangle(0, 0, h * h / w, h);<o:p></o:p>

doc.Save(“changed.pdf”);

Also please note that this code will expand width of the page to fit contents into new document but make width/height ratio equal to height/width in original document (i.e. change orientation of the page). If you need to zoom document you can use PdfPageEditor facade:
[C#]

Document doc = new Document(“Sample.pdf”);<o:p></o:p>

PdfPageEditor ppe = new PdfPageEditor();<o:p></o:p>

ppe.BindPdf(“PdfWithText.pdf”);<o:p></o:p>

double w = doc.Pages[1].Rect.Width;<o:p></o:p>

double h = doc.Pages[1].Rect.Height;<o:p></o:p>

float k = (float)(w / h);<o:p></o:p>

ppe.Zoom = k;<o:p></o:p>

ppe.PageSize = new PageSize((float)h, (float)w);<o:p></o:p>

ppe.Save(“changed.pdf”);

If you still face any issue or need further assistance, please feel free to contact us.

Best Regards,