PageInfo.IsLandScape = true is not working with HTML to PDF

I am trying to convert an HTML file to PDF, it is working fine in portrait mode. But I want some of the pages to be in the landscape mode. I have tried isLandScape property, but its not working. I could make the page in the landscape mode by resizing the page, not sure whether this is proper approach though. But the rendered contents are still in the portrait mode (not taking the full width).



This is the c# code I am using,



string contentHtml = File.ReadAllText(@“Sample.html”);

docs[1] = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(contentHtml)), htmlLoadOptions);



Rectangle r = docs[1].Pages[1].GetPageRect(false);

PdfPageEditor ppe = new PdfPageEditor();

ppe.BindPdf(docs[1]);

ppe.Zoom = ((float)(r.Width / r.Height));

ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);

ppe.Save(“Landscape.pdf”);

Hi Ramees,

Thanks for contacting support.

I have converted the HTML into PDF and set the orientation to Landscape through following code snippet. The code generated correct output. I am also attaching input/output file(s) for your reference.

string contentHtml = File.ReadAllText(dataDir + @“InputHtml.html”);

Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(contentHtml)), new HtmlLoadOptions());

Rectangle r = doc.Pages[1].GetPageRect(false);

Facades.PdfPageEditor ppe = new Facades.PdfPageEditor();

ppe.BindPdf(doc);

ppe.Zoom = ((float)(r.Width / r.Height));

ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);

ppe.Save(dataDir + "Landscape.pdf");

Please check the attached file(s) and above code snippet. Moreover in order to update page dimensions you can check “Manipulate Page in a PDF” section of API documentation. If you still face any issue please share your sample input file so that we can try to replicate the issue in our environment. We are sorry for the inconvenience.

Best Regards,

Hi Asad,



Thanks for the help.



I have tried the above code. It will change the orientation to landscape but not the content. The content is still in the portrait mode, means it is not taking the full width.



The sample html file you tried contains only a p tag with a small content. There will not be any issue if the orientation is changed for that file.



I have attached a sample input html file and the out put pdf I generated using the above code you provided. Please check.



And I don’t think it will be a good idea to change the height and width of the pdf document after loading the html. Also If I try to zoom the contents as mentioned in the link “Manipulate Page in a PDF”, then it will only just stretch the content and it is not looking good.



We should be able to set the orientation before the html file is loaded to the document object such that the content is loaded in landscape mode.



Please help me out in this regard asap, as we have lot of pages with large grids which we want to render in landscape mode.

Hi Ramees,

Thanks for sharing sample file(s) and more details. I have tried to generate PDF file from your sample HTML and noticed that the content in the PDF file did not rendered correctly. I have also tried to resize the content through following code snippet but I did not get much success.

doc.ProcessParagraphs();

// Resize contents of resultant PDF
int[] page_cnt1 = new int[doc.Pages.Count];

for (int i = 0; i < doc.Pages.Count; i++)
{
    page_cnt1[i] = i + 1;
}

Facades.PdfFileEditor pfe = new Facades.PdfFileEditor();
pfe.ResizeContents(doc, page_cnt1, Facades.PdfFileEditor.ContentsResizeParameters.PageResize(Aspose.Pdf.PageSize.A4.Height, Aspose.Pdf.PageSize.A4.Width));

doc.Save(dataDir + "Landscape.pdf");

It seemed that API is not honoring content of the HTML correctly while generating the PDF. However for the sake of detailed investigation I have logged this issue as PDFNET-42358 in our issue tracking system. We will further look into the details of the issue and keep you updated on the status. Please be patient and spare us a little time.

We are sorry for the inconvenience.

Best Regards,

The issues you have found earlier (filed as PDFNET-42358) have been fixed in Aspose.PDF for .NET 23.2.