iText7:如何忽略PDF页面旋转 [英] iText7:How to ignore PDF Page Rotation

查看:637
本文介绍了iText7:如何忽略PDF页面旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在努力解决这个问题.我只是尝试使用iText7.NET实现冲压功能.这个问题首先来自透明性.如果我使用图片(使用透明png尝试过),则pdf会被加盖印章,但是背景是黑色,而不是透明的:

I have been struggling with this issue for a long time now. I am simply trying to implement a stamping feature using iText7.NET. The issue first arises from transparency. If I use an image (tried it with transparent png), the pdf gets stamped, but the background is color black as opposed to being transparent:

为了有趣,相关代码如下:

For the sake of interest, the associated code is the following:

Rectangle location = new Rectangle(crop.GetLeft(),crop.GetBottom(),iWidth/4,iHeight/4);

PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));

PdfFormXObject xObj = new PdfFormXObject(new Rectangle(iWidth, iHeight));
PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
canvas.AddImage(img, 0, 0,iWidth, false);
stamp.SetNormalAppearance(xObj.GetPdfObject());

stamp.SetFlags(PdfAnnotation.PRINT);

pdfDoc.GetFirstPage().AddAnnotation(stamp);
pdfDoc.Close();

为了解决该问题,我尝试模拟Adobe对图章插入的操作-将图章PDF合并到现有文档中.事后看来,结果很好,但事实证明,一旦将图章文档复制为 PdfFormXObject ,合并过程就将旋转考虑在内-结果是,碰巧有一个图章文档的图章文档.非零旋转值(通常是由于扫描导致)的方向错误(否则,由于纵横比的翻转会导致过度拉伸)

In an effort to resolve the issue, I tried to emulate what Adobe does with stamp insertion - Merging the stamp PDF into the existing document. The results were great in hindsight, but it turns out, once you copy the stamp document as a PdfFormXObject, the merge process takes the rotation into account -- as result, a stamping document that happens to have a nonzero rotation value (commonly due to scans) is oriented in the wrong direction (or otherwise will get overstretched due to flipped aspect ratio)

关联的代码如下:

// Document to be edited and documented to be merged in
PdfDocument newDoc = new PdfDocument(reader, writer);
PdfDocument srcDoc = new PdfDocument(new PdfReader(stampsrc));

// CropBox And Dimensions
Rectangle crop = newDoc.GetFirstPage().GetCropBox();
float width = crop.GetWidth();
float height = crop.GetHeight();

// Create FormXObject and Canvas
PdfFormXObject page = srcDoc.GetPage(1).CopyAsFormXObject(newDoc);
//Extract Page Dimensions
float xWidth = srcDoc.GetFirstPage().GetCropBox().GetWidth();
float xHeight = srcDoc.GetFirstPage().GetCropBox().GetHeight();

Rectangle location = new Rectangle(crop.GetLeft(), crop.GetBottom(), xWidth , xHeight );

Debug.WriteLine(location.GetWidth());

PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));
PdfCanvas canvas = new PdfCanvas(newDoc.GetFirstPage().NewContentStreamBefore(), newDoc.GetFirstPage().GetResources(), newDoc);

// canvas.AddXObject(page,location.GetLeft(),location.GetBottom(),page.GetWidth());
stamp.SetNormalAppearance(page.GetPdfObject());
stamp.SetFlags(PdfAnnotation.PRINT);
newDoc.GetFirstPage().AddAnnotation(stamp);

srcDoc.Close();
newDoc.Close();

另一种方法涉及使用 CopyPageTo 方法,但是它没有令人满意的结果,因为我无法将其与图章相关联,也不包含透明度.因此,可以看出,我陷入了试图解决两个问题之一之间的困境.我要么设法找到一种使图像背景保持透明而不用担心旋转的方法,要么干脆抵消合并页面的旋转(更可取).

Another method involved using the CopyPageTo method, but it had no desirable result as I couldn't associate it with a stamp, nor did it contain transparency. So, as can be seen, I am stuck between trying to resolve one of two problems. It's either I manage to find a way to keep my Image background transparent without worrying about rotation, or simply counteract the rotation of the merged page (more desirable).

此外,我一直在探索其他选项,例如使用 AffineTransform ConcatMatrix 方法进行较低级别的操作-我还不熟悉它们,但是似乎它不直接适用于我的冲压方案吗?我将ConcatMatrix与值(0 -1 1 0 1 1)一起使用时认为与90度旋转相对应,但我的戳记只是消失了.我相信最后一对数字是不正确的-但同样,那里没有完整的理解,所以我无法提出要求.

Additionally, I have been exploring other options such as lower level manipulation using AffineTransform and the ConcatMatrix methods - I am not well versed in them yet, but it seems that it isn't directly applicable to my stamping scenario? I used the ConcatMatrix with values (0 -1 1 0 1 1) thinking that corresponds to a 90 degrees rotation, but my stamp simply disappeared. I believe the last pair of numbers are incorrect - but again, no complete understanding there, so I can't make claims.

我尝试(但不清楚理解)的另一件事是弄乱了 setIgnorePageRotationForContent 方法,以查看两个文档是否影响放置.但是,将其设置为true或false不会改变结果.

Another thing I tried (but not clearly understand) is messing with the setIgnorePageRotationForContent method for either documents to see if it affects the placement. Setting it to either true or false did not alter the result however.

还有其他建议吗?这个问题真的像我说的那么难吗?我找不到任何直接解决此问题的在线讨论.

Any further advice? Is this issue really as difficult as I make it sound? I couldn't find any online discussion that addressed this directly.

预先感谢

推荐答案

添加透明图像的代码是完全有效的,并且实际上应该适用于大多数PNG图像.

The code for adding a transparent image is perfectly valid and should actually work for most of PNG images.

如果由于某种原因它不起作用,您可以通过以下方式显式设置图像蒙版:

In case it doesn't work for some reason, you can set image mask explicitly in the following way:

// Circle mask
byte circledata[] = {(byte) 0x3c, (byte) 0x7e, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7e,
            (byte) 0x3c};
ImageData mask = ImageDataFactory.Create(8, 8, 1, 1, circledata, null);
mask.MakeMask();
mask.SetInverted(true);
img.SetImageMask(mask);

PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
canvas.AddImage(img, 0, 0, iWidth, false);

您还可以从磁盘加载掩码.只需创建与邮票原始图像尺寸相同的图像,然后在该蒙版图像中提供透明度即可.之后,您可以加载遮罩并将其设置为图像,方法与上述类似:

You can also load a mask from disk. Just create an image of the same dimensions as your original image for the stamp, and provide the levels of transparency in that mask image. After that, you can load the mask and set it to your image in a way similar to the described above:

ImageData mask = ImageDataFactory.create(MASK_PATH);
mask.MakeMask();
img.SetImageMask(mask);

另请参见

See also this sample for more details.

这篇关于iText7:如何忽略PDF页面旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆