iTextSharp PdfStamper根据最新矩阵将条形码放入现有PDF中(iTextSharp PdfStamper puts barcode into an existing PDF acco

编程入门 行业动态 更新时间:2024-10-23 23:32:07
iTextSharp PdfStamper根据最新矩阵将条形码放入现有PDF中(iTextSharp PdfStamper puts barcode into an existing PDF according to the latest matrix)

我有一个现有的PDF,它将此作为最后一个矩阵转换:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm(旋转45度,有些过渡)

我使用PdfStamper将图像插入OverContent(使用方法GetOverContent)。 问题是,最后一次转换设置了45度旋转,因此条形码也会旋转。 我无法重置此矩阵。 我甚至无法获得最后的转换矩阵。

我已经尝试了PdfReaderContentParser来获取所有矩阵并将它们相乘,这样我就可以得到倒置矩阵以使用带有这个反转矩阵的ConcatCTM再次生成Identity矩阵,但解析器不会返回所有矩阵。 它只能获取图像的矩阵,而不能获取文本的矩阵。

我已经花了好几个小时尝试不同的解决方案(创建模板,图层......),没有工作,一切都根据最后一个矩阵旋转。

如何将矩阵重置回现有PDF中的标识? 因此,PdfStamper插入的条形码不会旋转45度。

请求不建议为条形码图像设置-45度旋转,它不会解决任何问题,因为PDF可以有或没有最后的旋转矩阵。

我真的需要将转换矩阵重置为OverContent的单位矩阵。

非常感谢你们...

编辑:这是完整的内容:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

问BT 141.73 274.96 Td 5 Tr(某些文字)Tj ET

Q

0.57瓦

BT / F1 20.00 Tf ET

q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm

问BT 155.91 246.61 Td 5 Tr(某些文字)Tj ET

Q

I have an existing PDF, that has this as the last matrix transformation:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm (which is 45 deg rotation and some transition)

I use PdfStamper to insert an image into OverContent (using method GetOverContent). The problem is, that the last transformation sets 45 degrees rotation, so the barcode is rotated as well. And I cannot reset this matrix. I cannot even get the last transformation matrix.

I have already tried PdfReaderContentParser to get all the matrices and multiply them, so that I could get inverted matrix to use ConcatCTM with this inverted matrix to produce Identity matrix again, but the parser does not return all the matrices. It only gets matrices for images, but not matrices for texts.

I have already spend hours trying different solutions (creating templates, layers, ...), none working, everything is rotated according to the last matrix.

How to reset the matrix back to identity in existing PDF? So that the inserted barcode by PdfStamper is not rotated by 45 degrees.

Pleas don't propose setting -45 degrees rotation for the barcode image, it does not resolve anything, because the PDF can have or does not have to have the rotation matrix at the end.

I really need to reset the transformation matrix to identity matrix for OverContent.

Thank you very much, guys...

Edit: This is the full content:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

q BT 141.73 274.96 Td 5 Tr (some text) Tj ET

Q

0.57 w

BT /F1 20.00 Tf ET

q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm

q BT 155.91 246.61 Td 5 Tr (some text) Tj ET

Q

最满意答案

您的PDF有语法问题。 我看到一个“保存状态”运算符q ,但我没有看到“恢复状态”运算符Q

代替:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

你应该有:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm Q

嗯,实际上,如果您在共享的PDF语法后面没有任何其他内容,则应删除该完整代码段,因为引入坐标系转换没有意义,然后不执行任何操作。

如果要为此特定PDF修复此问题,可以执行以下操作:

overContent.SetLiteral(" Q ");

这将恢复图形状态(并在您放置时“重置矩阵”)。 不要对所有PDF都这样做,只针对这个有缺陷的PDF。

更新:

您说代码的语法如下所示:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q

这段代码错了。 创建此PDF的人使用的是蹩脚的工具,或者他以非常糟糕的方式使用了一个好工具。 正确的语法是:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q Q

您可以像这样解决问题:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q Q Q

现在q / Q序列是平衡的。 我正在添加数字以显示哪个q对应哪个Q :

q[1] 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q[2] BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q[2] 0.57 w BT /F1 20.00 Tf ET q[3] 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q[4] BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q[4] Q[3] Q[1]

这个语法是错误的(显然),但数字显示为什么缺少两个Q语句。

Your PDF has a syntax problem. I see a "save state" operator q, but I don't see a "restore state" operator Q.

Instead of:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

You should have:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm Q

Well, actually, if you don't have any other content following the PDF syntax you share, that full snippet should be removed, because it doesn't make sense to introduce coordinate system transformations, and then do nothing with it.

If you want to fix this for this specific PDF, you could do this:

overContent.SetLiteral(" Q ");

This will restore the graphics state (and "reset the matrix" as you put it). Don't do this for all your PDFs, only for this faulty one.

Update:

You say that the syntax of your code looks like this:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q

This code is wrong. The person who created this PDF either used a crappy tool, or he used a good tool in a very bad way. The correct syntax would be:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q Q

You can fix the problem like this:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q 0.57 w BT /F1 20.00 Tf ET q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q Q Q

Now the q/Q sequences are balanced. I'm adding numbers to show you which q corresponds with which Q:

q[1] 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm q[2] BT 141.73 274.96 Td 5 Tr (some text) Tj ET Q[2] 0.57 w BT /F1 20.00 Tf ET q[3] 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm q[4] BT 155.91 246.61 Td 5 Tr (some text) Tj ET Q[4] Q[3] Q[1]

This syntax is wrong (obviously), but the numbers show you why there are two Q statements missing.

更多推荐

本文发布于:2023-07-24 20:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条形码   矩阵   放入   最新   iTextSharp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!