c# - iTextSharp - 移动 Acrofield

标签 c# .net itextsharp acrofields

我有一个将目录插入现有 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/

相关文章:

c# - 仅 Twitter API 应用程序身份验证(使用 TweetSharp)

c# - ConfigurationManager.AppSettings - 如何修改和保存?

c# - XmlSerializer 不遵守默认命名空间?

C# HttpWebRequest 验证/指定使用的密码

pdf - 从带有 CID 字体的 PDF 中提取文本

c# - 将换行符附加到字符串的最佳方法,最后一个除外

javascript - 将用户登录到 OneDrive

c# - iTextSharp Html 到 Pdf 图像 src

c# - 使用 ServiceStack.Text 将 json 字符串反序列化为对象

c# - 如何在 C# 中使用 iTextSharp 获取 pdf 文件中的特定段落?