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”);
double w = doc.Pages[1].Rect.Width;
double h = doc.Pages[1].Rect.Height;
doc.Pages[1].MediaBox = new Rectangle(0, 0, h * h / w, h);
doc.Pages[1].CropBox = new Rectangle(0, 0, h * h / w, h);
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”);
PdfPageEditor ppe = new PdfPageEditor();
ppe.BindPdf(“PdfWithText.pdf”);
double w = doc.Pages[1].Rect.Width;
double h = doc.Pages[1].Rect.Height;
float k = (float)(w / h);
ppe.Zoom = k;
ppe.PageSize = new PageSize((float)h, (float)w);
ppe.Save(“changed.pdf”);

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

Best Regards,