Posted 21 November 2022, 2:36 pm 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]