I am trying to convert an HTML string to PDF. It works fine, but I want it to be landscape orientation. I’ve followed the other posts and it makes the page landscape mode, but does not resize the content.
Here is one version of code I have used thus far:
htmlLoadOptions.PageInfo.Margin.Bottom = 10;
htmlLoadOptions.PageInfo.Margin.Top = 10;
htmlLoadOptions.PageInfo.Margin.Left = 10;
htmlLoadOptions.PageInfo.Margin.Right = 10;
var pdfDocument = new Document(inputFileStream, htmlLoadOptions);
for (var pageNumber = 1; pageNumber <= pdfDocument.Pages.Count; pageNumber++)
pdfDocument.Pages[pageNumber].SetPageSize(Aspose.Pdf.PageSize.PageLetter.Height, Aspose.Pdf.PageSize.PageLetter.Width); // A5 size
var pdfSaveOptions = new PdfSaveOptions();
pdfDocument.Save(pdfMemoryStream, pdfSaveOptions);
Hi Jeremy,
Thank you for the reply. All I want to do is have my HTML to PDF be in landscape orientation without being cut off and filling the entire width of the page. Isn’t there a way to set the document to landscape orientation BEFORE importing the HTML? Your “Trim White-space Around a Page” is way more than I want to deal with for something that seems so simple. Is it possible that the issue is that you are using the constructor to load the HTML instead of first instantiating the document and then calling a method to Load HTML? Maybe there are technical issues with this approach but I have code from a really old version of ABC Pdf that had no problem adding html to a pdf in landscape mode. I am trying to eliminate ABC Pdf from my application and make it use Aspose only, and this is my last hurdle. Is there a way to load the html after the document has been instantiated with landscape orientation / size? Why does it default to portrait 8.5 x 11? Can I override it temporarily in code before creating the document? Thank you again, Jeremy.
Hi Jeremy,
Thanks for sharing the details.
Once the HTML file is loaded, we can set dimensions for resultant PDF but the contents inside PDF file are rendered in Left to Right layout, so you need to adjust left margin information accordingly. Please take a look over following code snippet to fulfill your requirements.
In case you encounter any issue, please share your input HTML file, so that we can test the scenario in our environment.
[C#]
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.PageInfo.Margin.Bottom = 10;
htmlLoadOptions.PageInfo.Margin.Top = 10;
htmlLoadOptions.PageInfo.Margin.Left = 10;
htmlLoadOptions.PageInfo.Margin.Right = 10;
var pdfDocument = new Document("c:/pdftest/Resume.html", htmlLoadOptions);
Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();
ppe.BindPdf(pdfDocument);
for (var pageNumber = 1; pageNumber <= pdfDocument.Pages.Count; pageNumber++)
{
Aspose.Pdf.Rectangle r = pdfDocument.Pages[pageNumber].Rect;
ppe.Zoom = (float)(r.Width / r.Height);
ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);
}
ppe.Save("c:/pdftest/Resume_source.pdf");
That prevents the content from being cut off, but since you are still loading the HTML before changing the size/orientation, the rendered content in the pdf still only covers the 8.5" width of the original portrait style. I have to be able to set the page size/orientation BEFORE importing the HTML. I’ve attached both the input file html (as .txt) and the output pdf. The whole point of making it landscape is so we have more room for the content. Is there any way to load the html AFTER I change the page size/orientation?
Hi Jeremy,