I am trying to download a PDF with landscape orientation and I cannot get it working.
First I create a word document by importing HTML, and then save the Word document as PDF and return as a download.
Here is a minimized version of the code:
// Create source document.
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert HTML
builder.MoveToDocumentStart();
builder.InsertHtml(document.Html, false);
MemoryStream stream = new MemoryStream();
doc.Save(stream, Aspose.Words.SaveFormat.Pdf);
//open the stream as a PDF
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(stream);
// Create return stream
MemoryStream returnStream = new MemoryStream();
// Save in PDF format
pdf.Save(returnStream, Aspose.Pdf.SaveFormat.Pdf);
// Return Pdf
return Convert.ToBase64String(returnStream.ToArray());
Note, if I return the Word Document stream, the Word Document will correctly have landscape orientation. It’s just the conversion\saving as pdf that fails to maintain the orientation.
Here is what I have tried:
Setting landscape in page setup:
Aspose.Words.PageSetup ps = builder.PageSetup;
// NOTE: Changing Orientation swaps PageWidth and PageHeight
ps.Orientation = Aspose.Words.Orientation.Landscape;
Keep source formatting:
// import options
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
importFormatOptions.IgnoreHeaderFooter = true;
importFormatOptions.KeepSourceNumbering = false;
Setting all Word sections to Landscape:
foreach (Aspose.Words.Section section in doc.Sections)
{
section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
section.PageSetup.LeftMargin = document.LeftMargin != null ? ConvertUtil.InchToPoint(document.LeftMargin.Value) : ConvertUtil.InchToPoint(1.0); ;
section.PageSetup.RightMargin = document.RightMargin != null ? ConvertUtil.InchToPoint(document.RightMargin.Value) : ConvertUtil.InchToPoint(1.0); ;
}
Setting Landscape on the PDF side:
pdf.PageInfo.IsLandscape = true;
None of these options worked.
Any ideas?
I am using Version 23.4.0
Thank You,
Coty