添加注释与获取字段位置时,Itext旋转调整是不同的 [英] Itext rotation adjustment is different when adding annotation vs getting field postion

查看:68
本文介绍了添加注释与获取字段位置时,Itext旋转调整是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新注释的坐标,并注意到当获得场位置与首先添加注释时,位置调整iText对于旋转270和90是相反的。这不是我的预期。



internal void AddAnnotation(PdfAnnotation annot,PdfDictionary pageN)





但如果提供外观,Adobe Reader会在注释矩形内绘制该外观:





CreateEllipse .testCreateEllipseOnRotated() CreateEllipse.testCreateCorrectEllipseAppearanceOnRotated()使用相应的测试已选择注释)



如何解决此问题



由于执行的限制iText中的矩形注释旋转校正(它既适用于注释创建,也适用于它的非典型方式),我建议您也可以完全关闭iText旋转校正码并自行完成所有旋转。



不幸的是,没有简单的属性来关闭它。相反,您需要使用解决方法,如已在相关主题中引用的堆栈溢出答案中所述:首先删除页面旋转条目,然后将注释添加到页面,然后恢复旋转条目。


I am trying to update the coordinates of an annotation and noticed that the position adjustment iText makes is reversed for rotations of 270 and 90 when getting a field position vs adding the annotation in the first place. This was not what I expected.

internal void AddAnnotation(PdfAnnotation annot, PdfDictionary pageN)

https://github.com/itext/itextsharp/blob/develop/src/core/iTextSharp/text/pdf/PdfStamperImp.cs#L1428

virtual public IList GetFieldPositions(String name)

https://github.com/itext/itextsharp/blob/develop/src/core/iTextSharp/text/pdf/AcroFields.cs#L1639

Anyone know why this is and how to work around this? I basically need to update the position of an added annotation and run through the same transform iText makes when first adding it as an annotation. I could do this adjustment myself but would prefer not to if there is a clean way.

My attempt was

  1. Add annotation
  2. Update annotation rectangle directly
  3. Get field position of the annotation
  4. Re-Set the rectangle of the field with the transformed coordinates from the call to GetFieldPositions

This works perfectly for a page rotation of 180 degrees but 90 and 270 are do not work because the transform is flip-flopped.

解决方案

Anyone know why this is and how to work around this?

Why this is

As you wrote in the comments, you meanwhile found this stack overflow answer on a related topic. To quote from it:

The rationale behind this is that for rotated pages iText attempts to lift the burden of adding the rotation and translation to page content required to draw upright text and have the coordinate system origin in the lower left of the page of the users' shoulders, so that the users don't have to deal with page rotation at all. Consequently, it also does so for annotations.

Unfortunately for your use case, though, iText only rotates the annotation rectangle when adding a new annotation; if you attempt to read the annotation rectangle later, iText returns the rotated rectangle, it does not undo its "correction"; if you attempt to set the annotation rectangle later, iText does not rotate it.

Even worse, iText's code to rotate the rectangle creates a Rect array in which the coordinates do not appear in the order lower-left, then upper right corner. While the order lower-left, then upper-right is not required by the specification, some programs have issues with other orders.

E.g. if one adds an ellipse annotation to a rotated page using iText without providing an appearance, Adobe Reader (both 9.5 and DC) draw the ellipse outside the annotation rectangle:

But if one provides an appearance, Adobe Reader draws that appearance inside the annotation rectangle:

(Screenshots of outputs of CreateEllipse.testCreateEllipseOnRotated() and CreateEllipse.testCreateCorrectEllipseAppearanceOnRotated() tests with the respective annotation selected)

How to work around this

Because of the limitations of the implementation of the rectangle annotation rotation correction in iText (both that it only is applied upon annotation creation and the untypical way it does so), I would suggest in your case, too, to switch off the iText rotation correction code entirely and do all the rotation yourself.

Unfortunately there is no simple property to switch that off. Instead you need to use a work-around as also explained in the already referenced stack overflow answer on a related topic: First remove the page Rotate entry, then add the annotation to the page, then restore the Rotate entry.

这篇关于添加注释与获取字段位置时,Itext旋转调整是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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