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

  • Posted 6 September 2023, 1:49 am EST

    Sorry to resurrect this post, but I’ve come back to trying to solve this problem. Maybe these images will describe what I’m trying to do.

    Here is my original page: https://postimg.cc/1nHM8yH1

    With the code I posted above, I am able to rotate the graphics of the page. Now I want to draw text in the upper left corner of the page using the DrawString method, like this:

    graphics.DrawString("NEW TEXT", new TextFormat() { Font = StandardFonts.Helvetica, FontSize = 20, ForeColor = Color.Red }, new PointF(0, 0));

    I want it to look like this: https://postimg.cc/S2qDQncG

    But what ends up happening is that the coordinate 0,0 is rotated along with everything else, so it looks like this: https://postimg.cc/Wq18rxWF

    Is there a way to rotate the page visually, while maintaining 0,0 in the upper left corner of the page?

  • Posted 6 September 2023, 6:20 pm EST

    Hi,

    As per your code, the page graphic is transforming and hence its content.

    If you want to add a string and don’t want to rotate it with the transformable graphics.

    Then you can follow the below approach:

    1. Take a copy of current graphics.
    2. DrawString on the particular location.
    3. Transform the copied graphics. So that the string will not considered under the Rotate transformation.
    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.DrawString("NEW TEXT", new TextFormat() { Font = StandardFonts.Helvetica, FontSize = 20, ForeColor = Color.Red }, new PointF(0, 0));
        newGraphics.Transform = rotateTransformation;
        newGraphics.DrawForm(formXObject, doc.Pages[i].Bounds, null, ImageAlign.CenterImage);    
    }

    Please refer the attached modified sample: PdfRotate_Mod.zip

    Best Regards,

    Nitin

  • Posted 7 September 2023, 5:10 am EST

    Ok, I see that copying the graphics before drawing the string seems to solve that problem. The other problem is that the rotated form is off-center. This is apparent when rotating the attached TestOrientation.pdf file.

    The original doc looks like this: https://postimg.cc/3406wzz3

    The rotated doc is off-center: https://postimg.cc/p964RPG0

    Also, the border doesn’t show in the rotated doc. Are either of those issues expected?

  • Posted 7 September 2023, 5:15 pm EST

    Hi,

    As per the previous response. The Rotation is performed at the center point of the page and the graphics rotated 90 degrees. Due to the change in size of Page it results with this offset. You can try below code to translate graphics to a corner point and then perform rotation on that point to achieve your requirement.(see code snippet)

    GcPdfDocument newDoc = new GcPdfDocument();
    var adjustmentOffsetHeight = doc.PageSize.Height+50;
    for (var i = 0; i < doc.Pages.Count; i++)
    {
        var translateTransformation = Matrix3x2.CreateTranslation(adjustmentOffsetHeight,0);
        var rotateTransformation = Matrix3x2.CreateRotation((float)(90 * Math.PI) / 180f, new Vector2(adjustmentOffsetHeight, 0)); // rotate 90 degrees
        var newPage = newDoc.NewPage();
        newPage.Size = new SizeF(adjustmentOffsetHeight, newPage.Size.Width);  //Swap height and width dimensions
        var newGraphics = newPage.Graphics;
        var formXObject = new FormXObject(newDoc, doc.Pages[i]);
        newGraphics.DrawString("NEW TEXT", new TextFormat() { Font = StandardFonts.Helvetica, FontSize = 20, ForeColor = Color.Red }, new PointF(0, 0));
        var finalMatrix = Matrix3x2.Multiply(translateTransformation, rotateTransformation);
        newGraphics.Transform = finalMatrix;
        newGraphics.DrawForm(formXObject, doc.Pages[i].Bounds, null, ImageAlign.CenterImage);    
    }

    The border is also considered in this example. Please refer the attached sample for the same: PdfRotate_Mod2.zip

    Best Regards,

    Nitin

  • Posted 12 September 2023, 2:36 am EST

    Is 50 arbitrary? Also, this resizes the page. And when the FormXObject is drawn, it’s drawn over top of the text, making it invisible.

    Here’s the situation: we have faxes come in that are saved as PDF files. Sometimes, those faxes are sent upside down or sideways, so they need to be rotated. We also need to draw text (and other annotations) on that PDF at user-specified coordinates that assume the origin is in the upper left corner. It seems like this should be relatively simple, but maybe this is a limitation of the PDF format.

    I just tried an experiment with exporting each page to a PNG file, rotating the PNG, and drawing it back into a new document. Not ideal, but it seems to be working so far.

  • Posted 12 September 2023, 5:14 pm EST

    Hi,

    We apologize for the inconvenience.

    But this is a limitation, The Text drawn before it gets rotated. That’s why it is behind the FormXObject. If we draw string after rendering FormXObject, it will be rotated as well.

    However, we are happy that you found a workaround to make it possible by rotating it as a Png file.

    You can get back to us once you face any issue with this.

    And we apologize again for this limitation.

    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