PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Nov 17, 2019 9:34 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Aug 23, 2019 2:32 pm 
Offline

Joined: Fri Aug 23, 2019 2:12 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 26, 2019 8:20 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3004
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 02, 2019 1:52 pm 
Offline

Joined: Fri Aug 23, 2019 2:12 pm
Posts: 2
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));


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 03, 2019 8:55 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3004
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 103 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group