Renaming a field

This is a code example of iText PDF, discover more.

7th November 2015

Switch code for this example

Example that explains how to rename a field.
 * Example written by Bruno Lowagie in answer to:
 * When renaming a field, you need to respect the existing hierarchy.
package sandbox.acroforms;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.util.Map;
import sandbox.WrapToTest;

public class RenameField {

    public static final String SRC = "resources/pdfs/subscribe.pdf";
    public static final String DEST = "results/acroforms/subscribe_renamed.pdf";
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        new RenameField().manipulatePdf(SRC, DEST);

    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        form.renameField("personal.loginname", "personal.login");
        reader = new PdfReader(dest);
        form = reader.getAcroFields();
        Map fields = form.getFields();
        for (String name : fields.keySet()) {

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now