Is it possible to modify PDF Form Field Names?

前端 未结 9 2181
一个人的身影
一个人的身影 2021-02-06 06:05

Here\'s the situation. I have a PDF with automatically generated pdf form field names. The problem is that these names are not very user friendly. They look something like :

相关标签:
9条回答
  • 2021-02-06 06:25

    Here is a java example found in the book, "iText in Action"

    This is taken from their example source code for the book and helped me immensely with this same issue. part2.chapter06.ConcatenateForms2

    public static void main(String[] args)
        throws IOException, DocumentException {
        // Create a PdfCopyFields object
        PdfCopyFields copy
            = new PdfCopyFields(new FileOutputStream(RESULT));
        // add a document
        PdfReader reader1 = new PdfReader(renameFieldsIn(DATASHEET, 1));
        copy.addDocument(reader1);
        // add a document
        PdfReader reader2 = new PdfReader(renameFieldsIn(DATASHEET, 2));
        copy.addDocument(reader2);
        // Close the PdfCopyFields object
        copy.close();
        reader1.close();
        reader2.close();
    }
    
    
    private static byte[] renameFieldsIn(String datasheet, int i)
        throws IOException, DocumentException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // Create the stamper
        PdfStamper stamper = new PdfStamper(new PdfReader(datasheet), baos);
        // Get the fields
        AcroFields form = stamper.getAcroFields();
        // Loop over the fields
        Set<String> keys = new HashSet<String>(form.getFields().keySet());
        for (String key : keys) {
            // rename the fields
            form.renameField(key, String.format("%s_%d", key, i));
        }
        // close the stamper
        stamper.close();
        return baos.toByteArray();
    }
    
    0 讨论(0)
  • 2021-02-06 06:26

    Maybe you can consider this:

    • iText is not free, in a propietary software enviroment (2.1.7 it/s last 'free' version).
    • AbcPDF is not free too.

    I have to say that we have AbcPDF license, and we use iText 2.1.7 too in our Java projects... so I can say I agree with previous answers, BUT, it you can't use/buy this products, you can try to replace the name into the pure pdf code (as a plain txt file), following PDF specification:

    Example for a signature field:

    35 0 obj
    <<
    /AP <<
    /N 37 0 R
    >>
    /DA (/TimesRoman 0 Tf 0 g)
    /F 4
    /FT /Sig
    /P 29 0 R
    /Rect [ 86 426 266 501 ]
    /Subtype /Widget
    /T (FIELD_MODIF)
    /Type /Annot
    /V 36 0 R
    >>
    endobj
    

    Where "FIELD_MODIF" it's the place where put the NEW name.

    0 讨论(0)
  • 2021-02-06 06:28

    The good news: you can change field names in iTextSharp.

    You can't actually edit a PDF though. You'd read in an existing PDF, update your field names in memory and then write out your revised PDF. To change a field name call the AcroFields.RenameField method.

    Here's a snippet:

    PdfReader reader = new PdfReader(PDF_PATH);
    using (FileStream fs = new FileStream("Test Out.pdf", FileMode.Create)) {
        PdfStamper stamper = new PdfStamper(reader, fs);
        AcroFields fields = stamper.AcroFields;
        fields.RenameField("oldFieldName", "newFieldName");
        stamper.Close();
    }
    

    Now the bad news: there appear to be limitations in the characters you can use in the renamed fields.

    I tested the snippet above with your example field name and it didn't work. Remove the periods though and it does work. I'm not sure if there's a workaround but this may be a problem for you,

    0 讨论(0)
  • 2021-02-06 06:29

    Yes, it's possible to rename form fields. I don't have any experience with an source code API that will help you with this, but my companies PDF SDK can help you do this, and from a little bit of searching it appears that iText will indeed let you rename form fields.

    0 讨论(0)
  • 2021-02-06 06:31

    I had the problem yesterday and after trying out answers in the forum and others but made no headway. My code looked like this.

    // Open up the file and read the fields on it.
    var pdfReader = new PdfReader(PATH_TO_PDF);
    var fs = new FileStream(pdfFilename, FileMode.Create, FileAccess.ReadWrite)
    var stamper = new PdfStamper(pdfReader, fs);
    var pdfFields = stamper.AcroFields;
    
    //I thought this next line of code I commented out will do it
    //pdfFields.RenameField("currentFieldName", "newFieldName");
    
    // It did for some fields, but returned false for others.
    // Then I looked at the AcroFields.RenameField method in itextSharp source and noticed some restrictions. You may want to do the same.
    //  So I replaced that line  pdfFields.RenameField(currentFieldName, newFieldName); with these 5 lines
    
    AcroFields.Item item = pdfFields.Fields[currentFieldName];
    PdfString ss = new PdfString(newFieldName, PdfObject.TEXT_UNICODE);
    item.WriteToAll(PdfName.T, ss, AcroFields.Item.WRITE_VALUE | AcroFields.Item.WRITE_MERGED);
    item.MarkUsed(pdfFields, AcroFields.Item.WRITE_VALUE);
    pdfFields.Fields[newFieldName] = item;
    

    And that did the job

    0 讨论(0)
  • 2021-02-06 06:31

    best way is to store fields rect and then create new field with desired name and stored rect position, new field will be on same position as old field, ta da :) here is the code:

    Sub CreateNewField()
     Create = "var f = this.getField('" & oldFieldName & "'); var rect = f.rect; 
     this.addField('" & newFieldName & "','text',0,rect);"
     FormFields.ExecuteThisJavascript Create
     DeleteField 'sub to delete old field
    End Sub
    
    0 讨论(0)
提交回复
热议问题