I have a docx document already in Landscape orientation, but when I convert to pdf, it doesnt keep the orientation.
var basea = ConfigurationHelper.BaseDirectory + @"\PT\Templates\teste.docx";
MemoryStream output = new MemoryStream();
var document = new Document(basea);
if (document.GetPageInfo(0).Landscape)
{
//true here
}
document.Save(output, SaveFormat.Pdf);
var docpdf = new Aspose.Pdf.Document(output);
//false when should be true
if (docpdf.PageInfo.IsLandscape)
{
}
@xblader According to Aspose.PDF documentation, Aspose.Pdf.Document.PageInfo gets or sets the page info and is for generator only, not filled in when reading document.
So it is expected that the page orientation is not read from PDF document. Actually you can determine page orientation your self by comparing page height and width. If height is greater that width - portrait orientation otherwise landscape.
How can I check if a pdf is a landscape? My docx is a landscape and pdf generated belongs height greater than width (despite it doesnt make sense to me). I m joining many diferent pdfs in one single file. The first page is a portrait and second is landscape. I need to know it to put watermark in correct position in each page.
pdfDocument.Save(output, SaveFormat.Pdf);
var pdf = new Aspose.Pdf.Document(output);
//Is there a way to check here if pdf is landscape? height is greater then width here, or maybe create a new file with this information
@xblader
The PageInfo properties Width, Height, IsLandscape used in the attached code snippet are intended for using a pdf file generator and are not filled in when reading file.
To reliably determine the orientation of the page, use the MediaBox property of the page.
If the width there is less (or equal) than the height, the page has a portrait orientation, otherwise landscape.
Page sizes can be determined using UserUnit (if specified) and MediaBox.Width, MediaBox.Height.
using (Document document = new Document(fileName))
{
foreach (Page page in document.Pages)
{
var pageMediaBox = page.MediaBox;
var isLandscape = pageMediaBox.Width > pageMediaBox.Height; // reliably determined.
double userUnit = 72.0; // pixels per inch
if (page.EnginePage.PageInformation.UserUnit != null)
{
userUnit = 72.0 * page.EnginePage.PageInformation.UserUnit.ToDouble();
}
double k = 25.4 / userUnit;
var pageWidth = k * pageMediaBox.Width;
var pageHeight = k * pageMediaBox.Height;
Console.WriteLine("Height: " + pageHeight + ", Width: " + pageWidth + ", Landscape: " + isLandscape);
}
}
I apologize for not understanding enough and remove the question to you. Thank you for looking at the documentation and providing clarification to the user.