pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Barry Neu <barry....@gmail.com>
Subject Form Filling/Flattening Problem
Date Thu, 31 Mar 2016 16:54:48 GMT

> 
> 
> 
> 
> Hello.
> Iā€™m trying out PDFBox 2.0 for an application where I need to flatten forms with data
collected from a user interface.
> Using the below code, I set some sample fields and set the read only flag to true. The
resulting output has the data populated on the form but the CheckBox is still editable.
> When using the PDAcroForm.flatten() method, the CheckBox is not editable, but the values
set in the Text fields do not appear populated on the resulting form.
> Using Java 1.7 on OSX.
> Sample form attached.
> 
> Probably something simple I am missing. Any help appreciated.
> 
> 
> import java.io <http://java.io/>.*;
> import java.util.Collection;
> import java.util.ArrayList;
> import org.apache.pdfbox.pdmodel.*;
> import org.apache.pdfbox.pdmodel.common.COSArrayList;
> import org.apache.pdfbox.pdmodel.font.PDFont;
> import org.apache.pdfbox.pdmodel.font.PDType1Font;
> import org.apache.pdfbox.pdmodel.interactive.form.*;
> import org.apache.pdfbox.cos.COSDictionary;
> import org.apache.pdfbox.cos.COSArray;
> import org.apache.pdfbox.cos.COSBase;
> 
> public class PdfFiller {
> 
> 	public static void main(String[] args) {
> 		String template  = ā€œ/mypath/alabama-abandoned-motor-vehicle-bill-of-sale.pdf";
> 		String flattened = ā€œ/mypath/FILLED-alabama-abandoned-motor-vehicle-bill-of-sale.pdf";
> 		try {
> 			File myFile = new File(template);
> 			PDDocument pdDoc = PDDocument.load( myFile );
> 			PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
> 			PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
> 			PDField field = pdAcroForm.getField("Make");
> 			field.setValue("Jeep");
> 			field.setReadOnly(true);
> 			field = pdAcroForm.getField("Model");
> 			field.setValue("Grand Cherokee");
> 			field.setReadOnly(true);
> 			field = pdAcroForm.getField("Check Box1");			
> 			field.setValue("Yes");
> 			field.setReadOnly(true);
> 			//pdAcroForm.flatten();  //uncommenting line results in missing values in text fields
> 			pdDoc.save(flattened);
> 			pdDoc.close();
> 		}
> 		catch (Exception e) {
> 			e.printStackTrace();
> 		}
> 	}
> }
> 


Mime
View raw message