iTextSharp的 - 移动Acrofield [英] iTextSharp - move Acrofield

查看:466
本文介绍了iTextSharp的 - 移动Acrofield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程,插入内容的表格到现有Acroform,我能够跟踪,我需要启动该内容。但是,我已现有低于该点,这将需要被向上或向下移动,基于I插入表的高度Acrofields。就这样,我怎么能更改Acrofield的位置?下面是code,我可以用它来弄的立场......但现在我还需要能够套了。

...

  //初始化加工厂(输出是MemoryStream对象)
            PdfStamper压模=新PdfStamper(pdf_rdr,输出);

            //获取参考,以PDF文档字段
            AcroFields栏= stamper.AcroFields;

            //调用方法来获得该领域的当前位置
            AcroFields.FieldPosition POS = GetFieldPosition(田txt_footer);
 

// **需要明确设置栏的新位置此处

  //假设呼叫RegenerateField将被要求
            fields.RegenerateField(txt_footer);
 

...

  //辅助方法,用于捕获场的位置
    私有静态AcroFields.FieldPosition GetFieldPosition(AcroFields字段,字符串field_nm)
    {

        ////////////////////////////////////////////////// //////////////////////////////////
        //获取页面的左边距,而顶位置起始位置
        //使用regarding_line字段作为基础
        IList的< AcroFields.FieldPosition> fieldPositions = fields.GetFieldPositions(field_nm);

        AcroFields.FieldPosition POS = fieldPositions [0];

        返回POS机;

    }
 

解决方案

首先是一些有关领域的信息,并在一个或多个页面的重新presentation。 PDF表单可以包含多个字段。字段在一个特定领域有一个特定的名字有一个又一个的价值感唯一的名称。域使用的是字段字典定义的。

每个字段可以具有零个,一个或多个重presentations在文档中。这些视觉重presentations被称为的部件标注的,它们被定义使用的标注字典的。

认识到这一点,你的问题需要被改写:?我要如何改变一个特定领域的特定部件标注的位置

我做了Java中的样品名为 ChangeFieldPosition 在回答这个问题。这将是由你将它移植到C#(也许你可以在这里发布的C#的答案以备将来参考)。

您已经有 AcroFields 例如:

  AcroFields形式= stamper.getAcroFields();
 

您现在需要的是项目实例的特定领域(在我的例子:与名称字段timezone2):

 项目项目= form.getFieldItem(timezone2);
 

的位置是小部件标注的属性,所以你需要问项目其部件。在下面的行,我得到的注解词典第一小注释(与指数 0 ):

  PdfDictionary部件= item.getWidget(0);
 

在大多数情况下,将只有一个插件注解:每个字段仅具有一个视觉重新presentation

注释的位置是具有四个值的数组:LLX,LLY,URX和URY。我们可以得到这样的数组是这样的:

  PdfArray RECT = widget.getAsArray(PdfName.RECT​​);
 

在以下行我改变了右上角的x值(指数 2 对应URX):

  rect.set(2,新PdfNumber(rect.getAsNumber(2).floatValue() -  10F));
 

其结果是,场的宽度由10PT被缩短。

I have a process that inserts a table of content into an existing Acroform, and I am able to track where I need to start that content. However, I have existing Acrofields below that point that will need to be moved up or down, based on the height of the tables I insert. With that, how can I change the position of an Acrofield? Below is code that I can use to "get" the position...but now I also need to be able to "set" it.

....

            // 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");

// ** NEED TO EXPLICITLY SET A NEW POSITION FOR THE FIELD HERE

            //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;

    }

解决方案

First some info about fields and their representation on one or more pages. A PDF form can contain a number of fields. Fields have unique names in the sense that one specific field with one specific name has one and one value. Fields are defined using a field dictionary.

Each field can have zero, one or more representations in the document. These visual representations are called widget annotations and they are defined using an annotation dictionary.

Knowing this, your question needs to be rephrased: how do I change the location of a specific widget annotation of a specific field?

I've made a sample in Java named ChangeFieldPosition in answer to this question. It will be up to you to port it to C# (maybe you can post the C# answer here for further reference).

You already have the AcroFields instance:

 AcroFields form = stamper.getAcroFields();

What you need now is the Item instance for the specific field (in my example: for the field with name "timezone2"):

    Item item = form.getFieldItem("timezone2");

The position is a property of the widget annotation, so you need to ask the item for its widget. In the following line, I get the annotation dictionary for the first widget annotation (with index 0):

    PdfDictionary widget = item.getWidget(0);

In most cases there will be only one widget annotation: each field has only one visual representation.

The position of the annotation is an array with four values: llx, lly, urx and ury. We can get this array like this:

    PdfArray rect = widget.getAsArray(PdfName.RECT);

In the following line I change the x-value of the upper-right corner (index 2 corresponds with urx):

    rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() - 10f));

As a result the width of the field is shortened by 10pt.

这篇关于iTextSharp的 - 移动Acrofield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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