Hallo,
ich habe folgenden Code geschrieben/modifiziert um Formularfelder eines Dokuments zu beschreiben .. Genauer gesagt nur ein Formularfeld als Test.
Soweit komme ich jedoch nicht, da mir gesagt wird, meine ausgewählte Datei sei keine PDF. Ich lasse die Datei extra von Windows öffnen, um zu sehen, ob der Pfad richtig ist.
Vielleicht kann mir jemand helfen?
ich habe folgenden Code geschrieben/modifiziert um Formularfelder eines Dokuments zu beschreiben .. Genauer gesagt nur ein Formularfeld als Test.
Soweit komme ich jedoch nicht, da mir gesagt wird, meine ausgewählte Datei sei keine PDF. Ich lasse die Datei extra von Windows öffnen, um zu sehen, ob der Pfad richtig ist.
Vielleicht kann mir jemand helfen?
Java:
private void openPDFDocument(final DataSetDocument document, final String template, Shell shell) {
if(document.getStringValueByKey("name").charAt(0) == 'R')
{
String workspace = Activator.getDefault().getPreferenceStore().getString("GENERAL_WORKSPACE");
String outputPdfFile = workspace + document.getStringValueByKey("name")+".pdf";
String inputPdfFile = workspace + DocumentFilename.getRelativeFolder(documentEditor.getDocumentType()) + "SchlStat-WS201314-V06-4auf1.pdf";
String[] s = new String[1];
s[0] = "ss";
try {
setFields(inputPdfFile, outputPdfFile, s );
} catch (COSVisitorException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void setFields( String inputPdfFile, String outputPdfFile, String[] nameValuePairs )
throws IOException, COSVisitorException {
if( inputPdfFile == null ) { return; }
PDDocument pdDoc = PDDocument.load( inputPdfFile );
try{
new ProcessBuilder( "cmd", "/c", inputPdfFile ).start();
}catch (Exception exp){
}
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 ) {
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( "TEXT1" );
if( field != null ) {
field.setValue( "geklappt" );
} else {
System.err.println( "Es gibt kein Formularfeld mit dem Namen '" + name + "'." );
}
}