A
alexyz
I'm tring to update some text fields with Etymon PJX.
Here there's the code that I use:
import com.etymon.pjx.*;
...
long start = System.currentTimeMillis();
PdfReader r = new PdfReader(new PdfInputFile(new File("c:\
\sample.pdf")));
PdfManager m = new PdfManager(r);
PdfDictionary td = m.getTrailerDictionary();
PdfDictionary info =
(PdfDictionary)m.getObjectIndirect((PdfReference)td.getMap().get(new
PdfName("Root")));
PdfDictionary inf =
(PdfDictionary)m.getObjectIndirect((PdfReference)info.getMap().get(new
PdfName("AcroForm")));
List list = ((PdfArray)inf.getMap().get(new
PdfName("Fields"))).getList();
System.out.println("total interactive fields count: "+list.size());
for(Iterator it = list.iterator(); it.hasNext(){
PdfReference ref = (PdfReference)it.next();
PdfDictionary fieldDictionary =
(PdfDictionary)m.getObjectIndirect(ref);
System.out.println(fieldDictionary);
PdfString key = (PdfString)fieldDictionary.getMap().get(new
PdfName("T"));
if(key.getString().equals("TextField14")){
Map newInfo = new HashMap(fieldDictionary.getMap());
newInfo.put(new PdfName("V"), new PdfString("sample text ...."));
PdfDictionary replacedObj = new PdfDictionary(newInfo);
System.out.println("Replacing object with: ");
System.out.println(replacedObj);
m.setObject(replacedObj, ref.getObjectNumber());
break;
}
}
System.out.println("Writing document ...");
PdfWriter w = new PdfWriter(new File("c:\\sample_output.pdf"));
m.writeDocument(w, true);
w.close();
System.out.println("time elapsed: "+(System.currentTimeMillis()-
start));
The text field with name equals to "TextField14" refers to the pdf
downloaded from:
http://www.realobjects.com/fileadmin/pdfreactor/samples/us/forms/acro-forms-sample.pdf
When I open the modifed document, it seems to be not modified. But if
I click on the "Highlight field" checkbox of Acrobat Reader 7.0.9 then
the string "sample text ...." will appear in the field.
I don't know if I use the library in the wrong manner or is the
library that doesn't work correctly (I've tried iText and it works
great, but PJX is much faster) ...
Is there anybody that can help me to solve this problem?
Thanks in advance,
Alessandro
Here there's the code that I use:
import com.etymon.pjx.*;
...
long start = System.currentTimeMillis();
PdfReader r = new PdfReader(new PdfInputFile(new File("c:\
\sample.pdf")));
PdfManager m = new PdfManager(r);
PdfDictionary td = m.getTrailerDictionary();
PdfDictionary info =
(PdfDictionary)m.getObjectIndirect((PdfReference)td.getMap().get(new
PdfName("Root")));
PdfDictionary inf =
(PdfDictionary)m.getObjectIndirect((PdfReference)info.getMap().get(new
PdfName("AcroForm")));
List list = ((PdfArray)inf.getMap().get(new
PdfName("Fields"))).getList();
System.out.println("total interactive fields count: "+list.size());
for(Iterator it = list.iterator(); it.hasNext(){
PdfReference ref = (PdfReference)it.next();
PdfDictionary fieldDictionary =
(PdfDictionary)m.getObjectIndirect(ref);
System.out.println(fieldDictionary);
PdfString key = (PdfString)fieldDictionary.getMap().get(new
PdfName("T"));
if(key.getString().equals("TextField14")){
Map newInfo = new HashMap(fieldDictionary.getMap());
newInfo.put(new PdfName("V"), new PdfString("sample text ...."));
PdfDictionary replacedObj = new PdfDictionary(newInfo);
System.out.println("Replacing object with: ");
System.out.println(replacedObj);
m.setObject(replacedObj, ref.getObjectNumber());
break;
}
}
System.out.println("Writing document ...");
PdfWriter w = new PdfWriter(new File("c:\\sample_output.pdf"));
m.writeDocument(w, true);
w.close();
System.out.println("time elapsed: "+(System.currentTimeMillis()-
start));
The text field with name equals to "TextField14" refers to the pdf
downloaded from:
http://www.realobjects.com/fileadmin/pdfreactor/samples/us/forms/acro-forms-sample.pdf
When I open the modifed document, it seems to be not modified. But if
I click on the "Highlight field" checkbox of Acrobat Reader 7.0.9 then
the string "sample text ...." will appear in the field.
I don't know if I use the library in the wrong manner or is the
library that doesn't work correctly (I've tried iText and it works
great, but PJX is much faster) ...
Is there anybody that can help me to solve this problem?
Thanks in advance,
Alessandro