Example usage for org.apache.pdfbox.pdmodel.interactive.form PDAcroForm setFields

List of usage examples for org.apache.pdfbox.pdmodel.interactive.form PDAcroForm setFields

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.form PDAcroForm setFields.

Prototype

public void setFields(List<PDField> fields) 

Source Link

Document

Set the documents root fields.

Usage

From source file:com.fangxin365.core.utils.PDFMerger.java

License:Apache License

/**
 * Merge the contents of the source form into the destination form for the
 * destination file./*from   ww  w.java 2s. co  m*/
 * 
 * @param cloner
 *            the object cloner for the destination document
 * @param destAcroForm
 *            the destination form
 * @param srcAcroForm
 *            the source form
 * @throws IOException
 *             If an error occurs while adding the field.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void mergeAcroForm(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm)
        throws IOException {
    List destFields = destAcroForm.getFields();
    List srcFields = srcAcroForm.getFields();
    if (srcFields != null) {
        if (destFields == null) {
            destFields = new COSArrayList();
            destAcroForm.setFields(destFields);
        }
        Iterator srcFieldsIterator = srcFields.iterator();
        while (srcFieldsIterator.hasNext()) {
            PDField srcField = (PDField) srcFieldsIterator.next();
            PDField destField = PDFieldFactory.createField(destAcroForm,
                    (COSDictionary) cloner.cloneForNewDocument(srcField.getDictionary()));
            // if the form already has a field with this name then we need
            // to rename this field
            // to prevent merge conflicts.
            if (destAcroForm.getField(destField.getFullyQualifiedName()) != null) {
                destField.setPartialName("dummyFieldName" + (nextFieldNum++));
            }
            destFields.add(destField);
        }
    }
}