Rotating a page while maintaining the original coordinate orientation

Posted by: jerrad.elmore on 21 November 2022, 5:36 am EST

    • Post Options:
    • Link

    Posted 21 November 2022, 5:36 am EST

    I need to rotate pages in a document (for example, a fax that came through sideways). I can use the Rotate method of the Page, but that also rotates the origin of the page as well as the overall orientation of the page. If I rotate the page 90 degrees and then try to place a form field at 0,0, it will actually appear in the upper right corner and appear sideways (because everything has been rotated 90 degrees). Is there a way to rotate a page and then reset the origin to be the “new” upper left corner?

    I’ve seen sample code that gets the Graphics object of the page and rotates that, but the rotated page appears off-center. What am I doing wrong here?

    Here is the code I’m using:

    [code]GcPdfDocument doc = new GcPdfDocument();

    FileStream fs = new FileStream(“test.pdf”, FileMode.Open);

    doc.Load(fs);

            GcPdfDocument newDoc = new GcPdfDocument();
                        
            for(var i=0; i < doc.Pages.Count; i++)
            {
                var rotateTransformation = Matrix3x2.CreateRotation((float)(90 * Math.PI) / 180f, new Vector2(doc.Pages[i].Bounds.Width / 2, doc.Pages[i].Bounds.Height / 2)); // rotate 90 degrees
                var newPage = newDoc.NewPage();
                newPage.Size = new SizeF(newPage.Size.Height, newPage.Size.Width);  //Swap height and width dimensions
                var newGraphics = newPage.Graphics;
                var formXObject = new FormXObject(newDoc, doc.Pages[i]);
                newGraphics.Transform = rotateTransformation;
                newGraphics.DrawForm(formXObject, doc.Pages[i].Bounds, null, ImageAlign.CenterImage);
            }
    
            newDoc.Save("rotated.pdf");[/code][img]https://gccontent.blob.core.windows.net/discourse-forum-uploads/file-f70240cb-9a95-4a34-858a-1ec5f8b4d2dd.png[/img]
    
  • Posted 21 November 2022, 7:40 pm EST

    Hi,

    As per our observation, you want to rotate the page but want to keep the content as it is. To achieve this requirement. You can swap the Page Height/Width and render the graphics without any transformation. (see code snippet)

    GcPdfDocument doc = new GcPdfDocument();
    FileStream fs = new FileStream("test.pdf", FileMode.Open);
    doc.Load(fs);
    
    GcPdfDocument newDoc = new GcPdfDocument();
    for (var i = 0; i < doc.Pages.Count; i++)
    {
        var newPage = newDoc.NewPage();
        newPage.Size = new SizeF(newPage.Size.Height, newPage.Size.Width);  //Swap height and width dimensions
        var newGraphics = newPage.Graphics;
        var formXObject = new FormXObject(newDoc, doc.Pages[i]);
        newGraphics.DrawForm(formXObject, doc.Pages[i].Bounds, null, ImageAlign.CenterImage);
    }

    Please refer to the attached sample for the same: PdfRotate.zip

    If your requirement is something different. Then please provide a Pdf and the resulting output that you want to achieve.

    Best Regards,

    Nitin

  • Posted 22 November 2022, 12:54 am EST

    No, I want the page rotated, along with the content, but I want the origin of the page to remain in the upper left corner (and not be rotated along with the visual aspect of the page). The code that I posted works, except that the graphics are not centered on the page. I have attached the original file along with the rotated output. You can see the problem in the rotated file.

  • Posted 22 November 2022, 4:13 pm EST

    Hi,

    It seems like you missed to attach the file.

    Could you please try to attach the Pdf file along with the rotated Pdf output that you want? So, that we can assist you accordingly.

    Best Regards,

    Nitin

  • Posted 22 November 2022, 10:27 pm EST

    I’ve tried attaching again.

  • Posted 22 November 2022, 10:28 pm EST

    Doesn’t look like attachments are working.

  • Posted 22 November 2022, 11:37 pm EST

    Hi,

    If File size is greater than 5MB o you are unable to share that file. Then you can share it with DropBox.

    Regards,

    Nitin

  • Posted 23 November 2022, 12:43 am EST

    Trying again.

  • Posted 24 November 2022, 4:56 am EST

    Hi,

    We are getting the exact output after rotating the pages to 90 degrees just like you wanted.

    If you want to adjust the origin of rotation. Then you can pass the center point parameter to the CreateRotation() method.(see code snippet)

    //here (100,100) is the origin for rotation.
     var rotateTransformation = Matrix3x2.CreateRotation((float)(90 * Math.PI) / 180f, new Vector2(100,100)); 

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels