简体   繁体   English

使用iTextSharp在不同PDF中的坐标系和置换文本存在问题

[英]Problem with coordinate system and displaced Text in different PDFs using iTextSharp

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. 我正在使用iTextSharp和.NET 3.5 SP1(使用C#,您可能会猜到),并使用PdfStamper类修改一些现有的PDF。 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. 如我所见,iTextSharp将(postscript?)点用作度量单位,但我想将这些坐标转换为毫米。 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! 但是问题是:没有System.Drawing.Drawing2D程序集! Was this assembly dropped or what happened to it? 这个程序集掉了还是发生了什么? What can I do to transform the coordinates of iTextSharp to millimeter. 如何将iTextSharp的坐标转换为毫米。 Am I on the wrong way here? 我在这里走错路了吗?

Text in PDF gets displaced in a different PDF using the same coordinates PDF中的文本使用相同的坐标替换为其他PDF

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. 我注意到,在修改两个具有相同内容的 不同 PDF文件时,会替换相同的坐标,并且文本不会放置在完全相同的位置。 What is causing this and how can I prevent it? 是什么原因造成的,如何预防?

This is the first PDF: 这是第一个PDF:

替代文字

This is the second PDF, created using the exact same coordinates in iTextSharp: 这是第二个PDF,使用iTextSharp中完全相同的坐标创建:

替代文字

Any help is appreciated. 任何帮助表示赞赏。

But the problem is: There is no System.Drawing.Drawing2D Assembly! 但是问题是:没有System.Drawing.Drawing2D程序集! Was this assembly dropped or what happened to it? 这个程序集掉了还是发生了什么? What can I do to transform the coordinates of iTextSharp to millimeter. 如何将iTextSharp的坐标转换为毫米。 Am I on the wrong way here? 我在这里走错路了吗?

http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx (in System.Drawing.dll) http://msdn.microsoft.com/zh-cn/library/system.drawing.drawing2d.matrix.aspx (位于System.Drawing.dll中)

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. 事实证明,第一个PDF包含一些不可见的修剪空间,第二个却没有。 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. 修剪空间不可见,因为视图空间是使用PDF创建器裁剪的,但是看起来修剪空间仍然存在并计算坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM