Hallo,
ich habe schon Google angeschmissen und ein schönen Programmcode gefunden nur hätte ich da noch zwei kleine Probleme:
-Checkboxen von dem PDF werden nicht erkannt
-Kombinationsfelder werden überschrieben, nur im PDF nicht angezeigt.
in Textfeldern funzt es alles tadellos.
hier mal der bisherige code
ich habe schon Google angeschmissen und ein schönen Programmcode gefunden nur hätte ich da noch zwei kleine Probleme:
-Checkboxen von dem PDF werden nicht erkannt
-Kombinationsfelder werden überschrieben, nur im PDF nicht angezeigt.
in Textfeldern funzt es alles tadellos.
hier mal der bisherige code
Code:
import java.io.*;
import java.util.*;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.interactive.form.*;
public class PdfFormularFelder
{
public static void main(String[] args) throws IOException, COSVisitorException
{
System.out.println(
"\nSetzt Werte in die Formularfelder eines PDF-Dokuments ein (oder listet die vorhandenen Formularfelder auf)." +
"\nAufruf mit den Parametern: InputPdfFile OutputPdfFile Name1 Value1 Name2 Value2 ...\n" );
String inputPdfFile = "d:/MeinPdfFormular.pdf";
String outputPdfFile = "d:/MeinPdfFormular1.pdf";
String[] nameValuePairs = {"Kombinationsfeld7","XXX"};
//String[] nameValuePairs = (args.length > 2) ? Arrays.copyOfRange( args, 2, args.length ) : null;
setFields( inputPdfFile, inputPdfFile, nameValuePairs );
}
public static void setFields( String inputPdfFile, String outputPdfFile, String[] nameValuePairs )
throws IOException, COSVisitorException
{
if( inputPdfFile == null ) { return; }
PDDocument pdDoc = PDDocument.load( new File( inputPdfFile ) );
PDDocumentCatalog pdCat = pdDoc.getDocumentCatalog();
PDAcroForm acroForm = pdCat.getAcroForm();
if( acroForm == null ) {
System.out.println( "Das Dokument '" + inputPdfFile + "' enthaelt kein PDF-Formular." );
return;
}
if( outputPdfFile == null || nameValuePairs == null ) {
printFieldNames( acroForm );
return;
}
int i = 0;
while( i < nameValuePairs.length - 1 ) {
setField( acroForm, nameValuePairs[i++], nameValuePairs[i++] );
}
pdDoc.save( outputPdfFile );
pdDoc.close();
}
public static void setField( PDAcroForm acroForm, String name, String value ) throws IOException
{
PDField field = acroForm.getField( name );
if( field != null ) {
field.setValue( "jgjfjfjh" );
} else {
System.err.println( "Es gibt kein Formularfeld mit dem Namen '" + name + "'." );
printFieldNames( acroForm );
}
}
public static void printFieldNames( PDAcroForm acroForm ) throws IOException
{
System.out.println( "Das PDF-Dokument enthaelt folgende Formularfelder:" );
@SuppressWarnings("unchecked")
List<PDField> fields = acroForm.getFields();
for( PDField f : fields ) {
System.out.println( f.getFullyQualifiedName()+" : " + f.getValue() );
}
}
}