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

Transform issue
http://forum.pdfsharp.com/viewtopic.php?f=3&t=3507
Page 1 of 1

Author:  salle55 [ Tue Nov 29, 2016 12:25 pm ]
Post subject:  Transform issue

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.

Author:  Thomas Hoevel [ Tue Nov 29, 2016 2:17 pm ]
Post subject:  Re: Transform issue

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.

Author:  salle55 [ Wed Dec 07, 2016 12:26 pm ]
Post subject:  Re: Transform issue

Thanks Thomas,
Good to know, will try with 1.32 and possibly 1.50.
Looking forward to any feedback!

Author:  salle55 [ Wed Mar 22, 2017 12:58 pm ]
Post subject:  Re: Transform issue

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.

Author:  salle55 [ Wed Mar 22, 2017 4:21 pm ]
Post subject:  Re: Transform issue

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! :)

Author:  bradleypeet [ Mon Mar 04, 2019 5:37 pm ]
Post subject:  Re: Transform issue

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.

Author:  TH-Soft [ Tue Mar 05, 2019 8:12 am ]
Post subject:  Re: Transform issue

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?

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