Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sun Aug 13, 2017 9:22 pm

I would like to know if it is possible to determine the orientation of individual pages within a PDF document instead of just the overall document orientation. I have a sample PDF which has one landscape page and two portrait pages and I would need to identify and rotate the landscape page programmatically. I have also tried using actualsize.height and width but this always seems to return the height as the largest value in the sample, regardless of the page orientation. Is there any way for me to do this?

milltownham
 
Posts: 29
Joined: Wed Aug 02, 2017 2:52 pm

Mon Aug 14, 2017 3:52 am

Hello,

Thanks for your inquiry.
After a check, we found the oritation of the first page in your file is potrit rather than landscape. Since it has already rotated 90 degree, it looks like "landscape". As for your requirement of getting the orientation of individual pages and rotate the first page. You could refer to the following code.
Code: Select all
            PdfDocument pdfDoc = new PdfDocument();
            pdfDoc.LoadFromFile(path);

            foreach (PdfPageBase page in pdfDoc.Pages)
            {
             // Determine the orientation of a certain page
             //PdfPageOrientation direction = page.Section.PageSettings.Orientation;
             //Since your first page has rotated 90 degree, if you want to turn it to be potrit- looked-like,
             // just set the rotate to 180 degree, that is, rotate 90 degree again.
                if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
                    page.Rotation = PdfPageRotateAngle.RotateAngle180;
            }
            pdfDoc.SaveToFile("11351.pdf");


Also, you could justify the page size mannually to let the content keep landscape.
Code: Select all
PdfDocument newDoc = new PdfDocument();
            PdfPageBase newPage;
            //Expected width and height
            float width = doc.Pages[1].ActualSize.Width;
            float height = doc.Pages[1].ActualSize.Height;
            foreach (PdfPageBase page in doc.Pages)
            {
                newPage = newDoc.Pages.Add(new SizeF(width, height), new PdfMargins(0));
                if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
                {
                    page.CreateTemplate().Draw(newPage, -360, -80);
                }
                else
                {
                    page.CreateTemplate().Draw(newPage, 0, 0);
                }           
            }

            newDoc.SaveToFile("resut1.pdf");
            System.Diagnostics.Process.Start("resut1.pdf");

Just choose the solution you need.
Let me know if I have made it clear.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Aug 16, 2017 8:21 am

Hello,

How is the issue now?
Your feedback will be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Aug 24, 2017 9:41 pm

Thank you for your help. I had not realised that the first page of my sample PDF was actually a portrait page rotated to appear landscape, so I was able to just set the rotation of all pages to RotateAngle0 so that every page now appears portrait and the height and width correspond with the displayed pages.

milltownham
 
Posts: 29
Joined: Wed Aug 02, 2017 2:52 pm

Fri Aug 25, 2017 1:23 am

Hello,

Thanks for your feedback.
Please feel free to contact us if you need any assistance.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF