PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 9:38 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Transform issue
PostPosted: Tue Nov 29, 2016 12:25 pm 
Offline

Joined: Mon Nov 28, 2016 10:37 am
Posts: 6
Discovered a problem when drawing in different scales using scale/translate transform.

In this example we draw two rectangles in different scales, generate a XPS from FixedDocumentSequence and finally convert the XPS to PDF using PDFsharps XPS converter.

Code:
var visual = new DrawingVisual();
DrawingContext dc = visual.RenderOpen();

// Setup transformations.
dc.PushTransform(new TranslateTransform(0, 1122.0));
dc.PushTransform(new ScaleTransform(3.77857136726379, -3.77857136726379));
dc.PushTransform(new TranslateTransform(-1719.41186523438, -1410.32360839844));
dc.PushTransform(new ScaleTransform(0.0117647061124444, 0.0117647061124444));

// Draw red rectangle.
var redPen = new Pen(Brushes.Red, 1);
var rectGeo1 = new RectangleGeometry(new Rect(160000, 130000, 8000, 5000));
dc.DrawGeometry(Brushes.Transparent, redPen, rectGeo1);

// Pop two transformations.
dc.Pop();
dc.Pop();

// Draw blue rectangle.
var bluePen = new Pen(Brushes.Blue, 0.5);
var rectGeo2 = new RectangleGeometry(new Rect(12, 12, 150.9408, 107.088539));
dc.DrawGeometry(Brushes.Transparent, bluePen, rectGeo2);
dc.Close();

// Generate XPS from DocumentSequence.
var a3Size = new Size(1587, 1123);
var docSeq = CreateDocumentSequence(a3Size, visual);
string dirPath = @"C:\Temp\";
string xpsFilePath = dirPath + "test.xps";
string pdfFilePath = dirPath + "test.pdf";
WriteXpsFile(docSeq, xpsFilePath);

// Generate PDF from XPS (PdfSharp)
XpsConverter.Convert(xpsFilePath, pdfFilePath, 0);

Complete program can be found here and VS solution here.

The red rectangle is expected to be aligned against the upper right corner of the blue rectangle. That is what we get when looking at the generated XPS:

Image

But in the generated PDF we get a gap between the two rectangles.

Image

The problem is most obvious when drawing a long way from origin, in this example the red rectangle is placed at 160000;130000 in a scale of 1:85. The gap becomes larger the longer from origin you draw, could it be a precision/rounding error?

I've tried to resolve this problem without success. Any help finding the cause would be greatly appreciated! Note, I'm not sure if the problem is with the PDFSharp rendering or the XPS conversion.

I'm using PDFSharp 1.31 (newer versions doesn't include the XPS support).

Complete Visual Studio solution with this example (including PDFSharp lib) is available here.

Note, I posted this on stackoverflow as well, but no answer so far. Hope you guys are able to help me.


Last edited by salle55 on Wed Mar 22, 2017 12:34 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Tue Nov 29, 2016 2:17 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
Hi!
salle55 wrote:
I'm using PDFSharp 1.31 (newer versions doesn't include the XPS support).
The XPS folder from the 1.31 source code can be copied to the 1.32 source code. It may require some renaming when used with version 1.50.
But I think that won't make any difference with respect to your issue.

I cannot say anything about your transformation issue. I'll let you know when I get any feedback.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Wed Dec 07, 2016 12:26 pm 
Offline

Joined: Mon Nov 28, 2016 10:37 am
Posts: 6
Thanks Thomas,
Good to know, will try with 1.32 and possibly 1.50.
Looking forward to any feedback!


Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Wed Mar 22, 2017 12:58 pm 
Offline

Joined: Mon Nov 28, 2016 10:37 am
Posts: 6
Have tested with 1.32 now, same issue unfortunately (copied the XPS folder as you suggested).
The complete VS solution with 1.32 and the test can be found here.

Would very much appreciate if someone could take a look at this.
I added a bounty on the stackoverflow question BTW.


Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Wed Mar 22, 2017 4:21 pm 
Offline

Joined: Mon Nov 28, 2016 10:37 am
Posts: 6
Found the cause, PDFSharp is rounding the transform matrix values to 4 decimals which is not enough in this case.

When manually adding more decimals for greater precision, the PDF renders correct.

Image

Modified PdfSharp.Xps.Rendering.PdfContentWriter to output 8 decimals instead of 4 and it works! :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Mon Mar 04, 2019 5:37 pm 
Offline

Joined: Wed Sep 28, 2016 10:44 pm
Posts: 6
Location: Kona, Hawaii
salle55 wrote:
Found the cause, PDFSharp is rounding the transform matrix values to 4 decimals which is not enough in this case.

When manually adding more decimals for greater precision, the PDF renders correct.


Good catch! Searching through the code I see a lot instances where numbers are formatted to only 3 decimal places. I suppose that could be appropriate for some things and not appropriate or adequate for others.


Top
 Profile  
Reply with quote  
 Post subject: Re: Transform issue
PostPosted: Tue Mar 05, 2019 8:12 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
bradleypeet wrote:
Good catch! Searching through the code I see a lot instances where numbers are formatted to only 3 decimal places.
Are you referring to the current version 1.50?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 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