使用 iTextSharp 处理不同 PDF 中的坐标系和文本移位问题

发布于 2024-07-20 07:29:28 字数 1142 浏览 1 评论 0 原文

我正在使用 iTextSharp 和 .NET 3.5 SP1(在 C# 中,您可以猜到)并使用 PdfStamper 类修改一些现有的 PDF。 现在我有两个问题:

转换坐标系

正如我所见,iTextSharp 使用(后记?)点作为测量单位,但我想将这些坐标转换为毫米。 现在,实现这一目标的最佳方法是什么?

查看文档揭示了以下方法:

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);

我希望我得到了正确的变换矩阵。 但问题是:没有 System.Drawing.Drawing2D 程序集! 这个程序集是被删除了还是发生了什么? 我该怎么做才能将 iTextSharp 的坐标转换为毫米。 我这里是不是走错路了?

PDF 中的文本在使用相同坐标的不同 PDF 中被替换

我注意到,在修改具有相同内容两个不同 PDF 文件时,相同的坐标被替换,并且文本没有放置在完全相同的位置。 造成这种情况的原因是什么以及如何预防?

这是第一个 PDF:

这是第二个 PDF,使用 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. 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:

alt text

This is the second PDF, created using the exact same coordinates in iTextSharp:

alt text

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-07-27 07:29:28

但问题是:没有
System.Drawing.Drawing2D 组装! 曾是
该程序集掉落或发生了什么
到它? 我能做些什么来改变
iTextSharp 的坐标为
毫米。 我是不是走错路了
在这里?

http://msdn.microsoft.com/en- us/library/system.drawing.drawing2d.matrix.aspx(在 System.Drawing.dll 中)

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)

末が日狂欢 2024-07-27 07:29:28

好吧,我解决了第二个问题,尽管使用了相同的坐标,但文本仍然被移位。

事实证明,第一个 PDF 包含一些不可见的修剪空间,而第二个 PDF 则没有。 修剪空间不可见,因为视图空间是使用 PDF 创建器裁剪的,但看起来修剪空间仍然存在并计入坐标。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文