iTextSharp - move Acrofield

空扰寡人 提交于 2019-12-07 11:26:44

问题


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;

    }

回答1:


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.



来源:https://stackoverflow.com/questions/22258598/itextsharp-move-acrofield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!