Reputation: 9959
How can i get the orientation of a page within a pdf document in .NET? A pdf document may contain portrait and landscape pages... Rigth?
Any help would be gratefully appreciated.
Upvotes: 1
Views: 5534
Reputation: 49179
With straight forward approaches, you'll get about 95% of the way there. You'll need the page dimensions, which you can get from the MediaBox, but really you want the CropBox if it exists because it can crop a portrait page into a landscape page (or vice versa). In addition, you need to look at the Rotation entry in the page dictionary because a page could be rotated in any of the compass points. And just to make life particularly interesting, the content of the page could be rendered in any orientation. You could have an "upright" portrait page with the text drawn upside down.
Upvotes: 1
Reputation: 55417
Using iTextSharp you can do this pretty easily:
''//File to test
Dim TestFileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
''//Create an object to read the PDF
Dim Reader As New iTextSharp.text.pdf.PdfReader(TestFileName)
''//Get the page size of the first page (iTextSharp page numbers start at 1)
Dim Rect = Reader.GetPageSize(1)
''//Compare the dimensions of the rectangle returned. For simplicity I'm saying that a square object is portraint, too
Dim IsPortrait = Rect.Height >= Rect.Width
Upvotes: 4