Working with rotated pages/PDFs

Hi,

Are there any examples available of working with rotated PDFs? I'm trying to consistently find the top of a page to add a header but the coordinates seem to rotate and shift from page to page.

See attached for an example of rotated PDFs. I used PdfPage.Graphics to draw all text.

Attachment: out_sync_31504223.zip

3 Replies 1 reply marked as answer

PH Praveenkumar H Syncfusion Team April 24, 2021 03:24 PM UTC

Hi Cheech 

The Syncfusin PDF library Graphics always draw the element from top left corner (0,0) for every rotation, this is the actual behavior of our library, you can rotate the coordinates based on the page rotation, please refer the below code snippet. 

PdfLoadedDocument documet = new PdfLoadedDocument(File.Open("../../../rotated.pdf", FileMode.Open)); 

            //Creating header template 
            PdfTemplate template = new PdfTemplate(new RectangleF(0, 0, 200, 200)); 
            template.Graphics.DrawRectangle(PdfBrushes.Red, new RectangleF(0, 0, 200, 200)); 
            template.Graphics.DrawString("Hello world", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.White, 0, 0); 
             
            foreach (PdfLoadedPage lPage in documet.Pages) 
           

                //page size is same for all the page 595(Width) X 842(width) 
                PdfPageRotateAngle angle = lPage.Rotation; 
                PdfGraphics graphics = lPage.Graphics; 
                float x = 0, y = 0; 
               
                graphics.Save(); 

                //Change the coordination of the page based on the rotation angle 
                if (angle == PdfPageRotateAngle.RotateAngle90) 
               
                    graphics.TranslateTransform(lPage.Size.Height, 0); 
                    graphics.RotateTransform(90); 
               
                if(angle==PdfPageRotateAngle.RotateAngle180) 
               
                    graphics.TranslateTransform(lPage.Size.Width, lPage.Size.Height); 
                    graphics.RotateTransform(180); 

               
                if(angle==PdfPageRotateAngle.RotateAngle270) 
               
                    graphics.TranslateTransform(0,lPage.Size.Width); 
                    graphics.RotateTransform(-90); 
               
              
                //Drawing the template 
                graphics.DrawPdfTemplate(template, new PointF(x,y)); 
                graphics.Restore(); 
           

            using (var memoryStream = File.Open("sample2.pdf", FileMode.OpenOrCreate)) 
           
                documet.Save(memoryStream); 
                documet.Close(true); 
           


Please try this sample and let us you know your result. 

Regards, 
Praveen 


Marked as answer

DA David March 13, 2022 05:16 PM UTC

How do u used 

PdfDocument to roate left or right I am using 


 doc.PageSettings.Rotate = angle; to rotate it but dont see an option for l




GK Gowthamraj Kumar Syncfusion Team March 14, 2022 12:57 PM UTC

Hi David, 
 
We have create a sample to rotate a pdf document using rotate property in our pdf document instance. We have attached the sample and output document for your reference, please try the below sample on your end and let us know the result.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/1647701544681977
 
 
Please refer to the below link for more information, 
 
Please let us know if you need any further assistance in this. 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon