Compilare il modulo pdf utilizzando iText in problemi java avere
Domanda
Sto usando iText per compilare il modulo pdf, nessun errore in console, ma quando sto aprendo il pdf uscita che sta dando "Ci aspettavamo un oggetto dict" e dopo di esso dà un altro messaggio che dice "Questo documento abilitato Funzioni estese in Adobe reader . Il documento è stato modificato da quando è stato creato e l'utilizzo di funzionalità estese non è più disponibile ".
Anche il pdf uscita hanno i cambiamenti che ho fatto, ma non ha le caratteristiche del documento PDF originale, come mostra sopra il messaggio.
Ho utilizzato al di sotto di codice ...
PdfReader reader = new PdfReader("C:\\Users\\asfs\\Downloads\\1013-Form22.pdf");
String name = "C:\\Users\\asfs\\Downloads\\Temp.pdf";
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(name));
AcroFields form = stamp.getAcroFields();
String last = "Form22_Dtls[0].Page1[0].Country_C[0]";
form.setField(last, "Sample Country name");
HashMap map = new HashMap();
map = form.getFields();
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext())
System.out.println("Field is >>>"+iterator.next());
// close pdf stamper
stamp.setFormFlattening(true);
stamp.close();
Si prega di aiutare me.
Grazie
Soluzione
Try:
// open the pdf stamper in edit mode
stamp = new PdfStamper(reader, new FileOutputStream(name), '\0', true);
The PdfStamper needs to be in append mode.
Altri suggerimenti
if you want a Java code, here's a snippet I got from one a blog. But it uses pdf Nitro, in case this helps or let me know
private ByteArrayOutputStream editPdfDocument() throws Exception {
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
HashMap fieldsWithValues = new HashMap();
int user_id = 1234; // unique key for the object
String dirPath = “D:/abc”;// directory path
String fileName = “def.pdf”;// name of the file
try {
reader = new PdfReader(dirPath + “/” + fileName);
stamper = new PdfStamper(reader, baosPDF);
AcroFields form = stamper.getAcroFields();
HashMap fields = form.getFields();
Set keys = fields.keySet(); // keys represents the names of all the form fields
fieldsWithValues = fetchFieldValuesForObject(user_id, keys);
// fetchFieldValuesForObject(user_id, keys) method will fetch the values of the fields from Database for object identified by user_id
Iterator itr = keys.iterator();
while (itr.hasNext()) {
String fieldName = (String) itr.next();
String fieldValue = fieldsWithValues.get(fieldName) != null ? (String)(fieldsWithValues.get(fieldName)) : “”;
form.setField(fieldName, fieldValue);
}
stamper.setFormFlattening(true);
} catch (Exception dex) {
if (stamper != null)
stamper.close();
if (reader != null)
reader.close();
throw dex;
}
return baosPDF;
}`enter code here`
It's Pdf form filling using Java that directly retrieves data from database, hope it works Visit Java Codes for Automatic form fill
as far as i know, the stamp.setFormFlattening(true); you are calling, remove fields and replace that with content, that's why your new document doesn't preserve fields