pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lukas Baab" <19lu...@web.de>
Subject Flattening
Date Tue, 22 Jan 2013 12:25:42 GMT

How do I "flatten" a PDF-form (remove the form-field but keep the text of the field)?

Jason asked this question already.
(see http://mail-archives.apache.org/mod_mbox/pdfbox-users/201009.mbox/%3C4C9A326F.60902@rayman2200.de%3E)
The answer was this:
> a quick way to do this, is to remove the fields from the acrofrom.
> For this you just need to get the document catalog, then the acroform and then remove
all fields from this acroform.
> The graphical representation is linked with the annotation and stay in the document.

So I wrote the following code.
I can write some text in the form-field. But I donĀ“t know how to remove the form-field.
Any ideas?

Thanks for your help!

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

public class PdfBoxTest
  public void test() throws Exception
    PDDocument pdDoc = PDDocument.load(new File("E:\\Form-Test.pdf"));
    PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
    PDAcroForm acroForm = pdCatalog.getAcroForm();
    if (acroForm == null)
      System.out.println("No form-field --> stop");

    List<PDField> fields = acroForm.getFields();

    // set the text in the form-field <-- does work
    for (PDField field : fields)
      if (field.getFullyQualifiedName().equals("formfield1"))

    // remove form-field but keep text ???
    // acroForm.getFields().clear(); <-- does not work
    // acroForm.setFields(null); <-- does not work
    // acroForm.setFields(new ArrayList()); <-- does not work
    // ???


View raw message