我有一个将目录插入现有 Acroform 的过程,并且我能够跟踪我需要从哪里开始该内容。但是,我现有的 Acrofields 低于该点,需要根据我插入的表格的高度向上或向下移动。有了那个,我怎样才能改变 Acrofield 的位置?下面是我可以用来“获取”位置的代码……但现在我还需要能够“设置”它。
....
// Initialize Stamper ("output" is a MemoryStream object)
PdfStamper stamper = new PdfStamper(pdf_rdr, output);
// Get Reference to PDF Document Fields
AcroFields fields = stamper.AcroFields;
//call method to get the field's current position
AcroFields.FieldPosition pos = GetFieldPosition(fields, "txt_footer");
//** 需要在此处明确设置字段的新位置
//assuming a call to "RegenerateField" will be required
fields.RegenerateField(txt_footer);
....
//helper method for capturing the position of a field
private static AcroFields.FieldPosition GetFieldPosition(AcroFields fields, string field_nm)
{
////////////////////////////////////////////////////////////////////////////////////
//get the left margin of the page, and the "top" location for starting positions
//using the "regarding_line" field as a basis
IList<AcroFields.FieldPosition> fieldPositions = fields.GetFieldPositions(field_nm);
AcroFields.FieldPosition pos = fieldPositions[0];
return pos;
}
最佳答案
首先是关于字段及其在一个或多个页面上的表示的一些信息。 PDF 表单可以包含多个字段。字段具有唯一的名称,即具有一个特定名称的特定字段具有一个和一个值。字段是使用字段字典定义的。
每个字段在文档中可以有零个、一个或多个表示。这些视觉表示称为小部件注释,它们是使用注释字典定义的。
了解这一点后,您的问题需要重新表述:如何更改特定字段的特定小部件注释的位置?
我用 Java 制作了一个名为 ChangeFieldPosition 的示例在回答这个问题。将其移植到 C# 将取决于您(也许您可以在此处发布 C# 答案以供进一步引用)。
您已经拥有 AcroFields
实例:
AcroFields form = stamper.getAcroFields();
您现在需要的是特定字段的 Item
实例(在我的示例中:对于名称为 "timezone2"
的字段):
Item item = form.getFieldItem("timezone2");
位置是小部件注释的属性,因此您需要向 item
询问其小部件。在下一行中,我获得了第一个小部件注释的注释字典(索引为 0
):
PdfDictionary widget = item.getWidget(0);
在大多数情况下,只有一个小部件注释:每个字段只有一个视觉表示。
注解的位置是一个数组,有四个值:llx、lly、urx和ury。我们可以这样得到这个数组:
PdfArray rect = widget.getAsArray(PdfName.RECT);
在下一行中,我更改了右上角的 x 值(索引 2
对应于 urx):
rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() - 10f));
因此字段的宽度缩短了 10pt。
关于c# - iTextSharp - 移动 Acrofield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22258598/