Problem with coordinate system and displaced Text in different PDFs using iTextSharp
-
21-08-2019 - |
Question
I'm working with iTextSharp and .NET 3.5 SP1 (in C#, as you can guess) and modify some existing PDFs using the PdfStamper
class. Now I've got two problems:
Transforming the coordinate system
As I see it, iTextSharp is using (postscript?) points as unit of measurement, but I'd like to transform these coordinates to millimeter. Now, how's the best way to achieve this?
Taking a look in the documentation revealed the following approach:
PdfContentByte cb = new PdfContentByte();
System.Drawing.Drawing2D.Matrix scale = new System.Drawing.Drawing2D.Matrix(
0.352777778, 0, 0.352777778, 0, 0, 0); // 1 point = 0.352777778 mm
cb.Transform(scale);
I hope I got the transformation matrix right. But the problem is: There is no System.Drawing.Drawing2D
Assembly! Was this assembly dropped or what happened to it? What can I do to transform the coordinates of iTextSharp to millimeter. Am I on the wrong way here?
Text in PDF gets displaced in a different PDF using the same coordinates
I noticed that while modifying two different PDF files with the same content, that the same coordinates got displaced and the text is not being placed at the exact same positions. What is causing this and how can I prevent it?
This is the first PDF:
This is the second PDF, created using the exact same coordinates in iTextSharp:
Any help is appreciated.
Solution
But the problem is: There is no System.Drawing.Drawing2D Assembly! Was this assembly dropped or what happened to it? What can I do to transform the coordinates of iTextSharp to millimeter. Am I on the wrong way here?
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx (in System.Drawing.dll)
OTHER TIPS
Okay, I solved the second problem with the text being displaced despite the same coordinates being used.
As it turned out, the first PDF included some non-visible trimming space, the second didn't. The trimming space isn't visible 'cause the view space was cropped using the PDF creator, but as it appears the trimming space was still there and counted for the coordinates.