Renaming a field

31st May 2016
iText PDF

Switch code for this example

RenameField.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21344750/itextsharp-renamefield-bug
  11.  *
  12.  * When renaming a field, you need to respect the existing hierarchy.
  13.  */
  14. package com.itextpdf.samples.sandbox.acroforms;
  15.  
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20. import com.itextpdf.forms.PdfAcroForm;
  21. import com.itextpdf.forms.fields.PdfFormField;
  22.  
  23. import java.io.File;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;
  27.  
  28. import org.junit.Assert;
  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import org.junit.experimental.categories.Category;
  32.  
  33. @Category(SampleTest.class)
  34. public class RenameField {
  35.     public static final String DEST = "./target/test/resources/sandbox/acroforms/rename_field.pdf";
  36.     public static final String SRC = "./src/test/resources/pdfs/subscribe.pdf";
  37.     public static List CMP_RESULT;
  38.  
  39.     static {
  40.         CMP_RESULT = new ArrayList();
  41.         CMP_RESULT.add("personal");
  42.         CMP_RESULT.add("personal.name");
  43.         CMP_RESULT.add("personal.login");
  44.         CMP_RESULT.add("personal.password");
  45.         CMP_RESULT.add("personal.reason");
  46.     }
  47.  
  48.     @BeforeClass
  49.     public static void beforeClass() throws Exception {
  50.         File file = new File(DEST);
  51.         file.getParentFile().mkdirs();
  52.     }
  53.  
  54.     @Test
  55.     public void manipulatePdf() throws Exception {
  56.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  57.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  58.  
  59.         PdfFormField login = form.getField("personal.loginname");
  60.         login.setFieldName("login");
  61.         form.getFormFields().remove("personal.loginname");
  62.         form.getFormFields().put("login", login);
  63.  
  64.         pdfDoc.close();
  65.  
  66.         pdfDoc = new PdfDocument(new PdfReader(DEST));
  67.  
  68.         Map fields = PdfAcroForm.getAcroForm(pdfDoc, true).getFormFields();
  69.         List result = new ArrayList();
  70.         for (String name : fields.keySet()) {
  71.             System.out.println(name);
  72.             result.add(name);
  73.         }
  74.  
  75.         pdfDoc.close();
  76.  
  77.         Assert.assertArrayEquals(CMP_RESULT.toArray(), result.toArray());
  78.     }
  79. }

Resources

Contact

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