PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Text on landscape pages is sideways, how can it be rotated?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4007
Page 1 of 1

Author:  ZFunhouse [ Fri Aug 23, 2019 2:32 pm ]
Post subject:  Text on landscape pages is sideways, how can it be rotated?

My code takes pages, one by one, from an existing PDF, and adds them to a new PDF, drawing text on each page as it goes. (There's more, but that the part that's having an issue.)

When the original PDF has a Landscape format page, the text comes up sideways, and positioned incorrectly. To illustrate, I would get the same result if I added text to a Portrait page and rotated the lot 90 degrees anti-clockwise.

I've tried rotating the XGraphics object with which I'm drawing the text, but that only fixes orientation, not positioning. Some text is thrown off the page due to the XGraphics taking a Portrait shape on a Landscape page.

Code:
//This code puts the text onto Landscape pages sideways (vertical)
//And in the wrong position
//Exactly as if the page was turned portrait, then text added correctly, then the whole lot returned to portrait

XGraphics gfx;
XRect box;

XStringFormat formatTitle = new XStringFormat();
XStringFormat formatTopLeft = new XStringFormat();
XStringFormat formatTopCenter = new XStringFormat();
XStringFormat formatTopRight = new XStringFormat();
XStringFormat formatBottomLeft = new XStringFormat();
XStringFormat formatBottomCenter = new XStringFormat();
XStringFormat formatBottomRight = new XStringFormat();
formatTitle.Alignment = XStringAlignment.Center;
formatTitle.LineAlignment = XLineAlignment.Center;
formatTopLeft.Alignment = XStringAlignment.Near;
formatTopLeft.LineAlignment = XLineAlignment.Near;
formatTopCenter.Alignment = XStringAlignment.Center;
formatTopCenter.LineAlignment = XLineAlignment.Near;
formatTopRight.Alignment = XStringAlignment.Far;
formatTopRight.LineAlignment = XLineAlignment.Near;
formatBottomLeft.Alignment = XStringAlignment.Near;
formatBottomLeft.LineAlignment = XLineAlignment.Far;
formatBottomCenter.Alignment = XStringAlignment.Center;
formatBottomCenter.LineAlignment = XLineAlignment.Far;
formatBottomRight.Alignment = XStringAlignment.Far;
formatBottomRight.LineAlignment = XLineAlignment.Far;

for (int idx = 0; idx < count; idx++)
{
    PdfPage nextPage = inputDocument.PageCount > idx ? inputDocument.Pages[idx] : new PdfPage();
    nextPage = outputDocument.AddPage(nextPage);
    gfx = XGraphics.FromPdfPage(nextPage);
    box = nextPage.MediaBox.ToXRect();
    box.Inflate(-10, -10);
    gfx.DrawString(text1, font, XBrushes.Black, box, formatTopLeft);
    gfx.DrawString(text2, font, XBrushes.Black, box, formatTopCenter);
    gfx.DrawString(text3, font, XBrushes.Black, box, formatTopRight);
    gfx.DrawString(text4, font, XBrushes.Black, box, formatBottomLeft);
    gfx.DrawString(text5, font, XBrushes.Black, box, formatBottomCenter);
    gfx.DrawString(text6, font, XBrushes.Black, box, formatBottomRight);
}

Tested on PdfSharp v1.50, and on v1.51

Author:  Thomas Hoevel [ Mon Aug 26, 2019 8:20 am ]
Post subject:  Re: Text on landscape pages is sideways, how can it be rotat

Hi!

A single DrawString would be enough to illustrate the problem. Your code snippet does much more, but is still just a code snippet.
Does this happen with all Landscape PDF files or just with some? You don't show the code where you use rotation.

Have you tried the fix shown here:
https://stackoverflow.com/questions/363 ... 6#41557736

Author:  ZFunhouse [ Mon Sep 02, 2019 1:52 pm ]
Post subject:  Re: Text on landscape pages is sideways, how can it be rotat

Hi, sorry for the long snippet. I've found that it's only affecting pages that have a Rotate value other than 0. i.e. 90 or 270. When I draw a string it is also rotated the same number of degrees, but I'd like it to be upright.
I have tried the solution you linked to and changing the PageOrientation didn't have an effect.
What I think I'm looking for is a way for the XGraphics object to have a Rotate value of 0 while still having the same size/shape/orientation as the page.

Rotating and transforming it produced skewed text, but here's what I tried anyway:
Code:
           XPoint matrixPoint = new XPoint(nextPage.Width / 2, nextPage.Height / 2);
           gfx.RotateAtTransform(360-nextPage.Rotate, matrixPoint);
           gfx.ScaleTransform((nextPage.Height/nextPage.Width),(nextPage.Width/nextPage.Height));

Author:  Thomas Hoevel [ Tue Sep 03, 2019 8:55 am ]
Post subject:  Re: Text on landscape pages is sideways, how can it be rotat

In the first post you wrote: "When the original PDF has a Landscape format page, the text comes up sideways, and positioned incorrectly."
That sounds like there is an error.

Now it sounds as if PDFsharp is working correctly and as expected.

Transformations in PDF are a bit difficult.
Maybe this snippet from the Watermark sample can help:
Code:
// Define a rotation transformation at the center of the page.
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

I think these are all the transformations you need. You still have to figure out the coordinates for your text or your textbox respectively.

I'm not sure what the ScaleTransform in your code snippet is supposed to do. It changes the aspect ratio of the text - so do not call ScaleTransform if you want the normal aspect ratio for text.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/