java - iText7 : com.itextpdf.kernel.PdfException: Document was closed. It is impossible to execute action -
i got com.itextpdf.kernel.pdfexception: document closed. impossible execute action.
error on itext7.
1 // update here 2 pdffont font; 3 { 4 gcsfilename gcsfilename = new gcsfilename("fonts", "msgothic001.ttf"); 5 try (gcsinputchannel inputchannel = 6 gcsservice.openprefetchingreadchannel(gcsfilename, 0, buffer_size)) { 7 font = 8 pdffontfactory.createfont( 9 getbytes(channels.newinputstream(inputchannel)), 10 pdfencodings.identity_h, 11 true); 12 } 13 } 14 // update until here 15 16 writerproperties wp = new writerproperties(); 17 wp.usesmartmode(); 18 try (pdfdocument writedoc = new pdfdocument(new pdfwriter(outputstream, wp))) { 19 20 list<integer> keylist = arrays.aslist(integer.valueof(1), integer.valueof(2), integer.valueof(3)); 21 (integer keynumber : keylist) { 22 logger.info(keynumber); // (1) 23 bytearrayoutputstream baos = new bytearrayoutputstream(); 24 try (pdfwriter writer = new pdfwriter(baos); 25 pdfdocument readdoc = 26 new pdfdocument(new pdfreader(new bytearrayinputstream(inputbytes)), writer)) { 27 pdfacroform pdfacroform = pdfacroform.getacroform(readdoc, false); 28 map<string, pdfformfield> fieldmap = pdfacroform.getformfields(); 29 if (fieldmap != null && fieldmap.size() > 0) { 30 set<string> fieldnameset = new hashset<>(fieldmap.keyset()); 31 (string fieldname : fieldnameset) { 32 pdfacroform.renamefield(fieldname, fieldname + "_" + keynumber); 33 } 34 fieldmap = pdfacroform.getformfields(); 35 } 36 37 38 // update here 39 pdfformfield form = fieldmap.get("customer_" + keynumber); 40 form.setfont(font).setvalue("test test"); 41 // update until here 42 43 } // (2) got error on line 44 45 try (pdfdocument readdoc = 46 new pdfdocument(new pdfreader(new bytearrayinputstream(baos.tobytearray())))) { 47 readdoc.copypagesto(1, readdoc.getnumberofpages(), writedoc, new pdfpageformcopier()); 48 } 49 } 50 }
i got output.
13:55:45.962 1 // (1) 13:55:47.252 2 // (1) 13:55:47.782 com.itextpdf.kernel.pdfexception: document closed. impossible execute action. @ com.itextpdf.kernel.pdf.pdfdocument.checkclosingstatus(pdfdocument.java:1887) @ com.itextpdf.kernel.pdf.pdfdocument.getwriter(pdfdocument.java:645) @ com.itextpdf.kernel.pdf.pdfobject.makeindirect(pdfobject.java:228) @ com.itextpdf.kernel.pdf.pdfdictionary.makeindirect(pdfdictionary.java:491) @ com.itextpdf.kernel.pdf.pdfdictionary.makeindirect(pdfdictionary.java:57) @ com.itextpdf.kernel.pdf.pdfobject.makeindirect(pdfobject.java:249) @ com.itextpdf.kernel.pdf.pdfdictionary.makeindirect(pdfdictionary.java:479) @ com.itextpdf.kernel.pdf.pdfdictionary.makeindirect(pdfdictionary.java:57) @ com.itextpdf.kernel.font.pdffont.makeobjectindirect(pdffont.java:600) @ com.itextpdf.kernel.font.pdftype0font.getfontdescriptor(pdftype0font.java:672) @ com.itextpdf.kernel.font.pdftype0font.flushfontdata(pdftype0font.java:828) @ com.itextpdf.kernel.font.pdftype0font.flush(pdftype0font.java:600) @ com.itextpdf.kernel.pdf.pdfdocument.flushfonts(pdfdocument.java:1848) @ com.itextpdf.kernel.pdf.pdfdocument.close(pdfdocument.java:800) @ (our source (2) )
why got error? how can fix?
[update] found setting value font "ms gothic" (standard font on japanese windows) cause error. seems fonts cause error while others not. tried helvetica, not cause error. have updated program (from line 1 14, , line 38 41).
i have made literal copy of code:
package com.itextpdf.samples; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.util.hashset; import java.util.map; import java.util.set; import com.itextpdf.forms.pdfacroform; import com.itextpdf.forms.pdfpageformcopier; import com.itextpdf.forms.fields.pdfformfield; import com.itextpdf.io.font.fontprogram; import com.itextpdf.io.font.fontprogramfactory; import com.itextpdf.kernel.font.pdffontfactory; import com.itextpdf.kernel.pdf.pdfdocument; import com.itextpdf.kernel.pdf.pdfreader; import com.itextpdf.kernel.pdf.pdfwriter; import com.itextpdf.kernel.pdf.writerproperties; public class test { public static void main(string[] args) throws ioexception { fontprogram fontprogram = fontprogramfactory.createfont("c:/windows/fonts/msgothic.ttc,1"); fileoutputstream outputstream = new fileoutputstream("test.pdf"); writerproperties wp = new writerproperties(); wp.usesmartmode(); try (pdfdocument writedoc = new pdfdocument(new pdfwriter(outputstream, wp))) { (int keynumber = 0; keynumber < 3; keynumber++) { bytearrayoutputstream baos = new bytearrayoutputstream(); try (pdfwriter writer = new pdfwriter(baos); pdfdocument readdoc = new pdfdocument(new pdfreader("form.pdf"), writer)) { pdfacroform pdfacroform = pdfacroform.getacroform(readdoc, false); map<string, pdfformfield> fieldmap = pdfacroform.getformfields(); if (fieldmap != null && fieldmap.size() > 0) { set<string> fieldnameset = new hashset<>(fieldmap.keyset()); (string fieldname : fieldnameset) { pdfacroform.renamefield(fieldname, fieldname + "_" + keynumber); } fieldmap = pdfacroform.getformfields(); } pdfformfield form = fieldmap.get("name_" + keynumber); form.setfont(pdffontfactory.createfont(fontprogram)).setvalue("test test"); } try (pdfdocument readdoc = new pdfdocument(new pdfreader(new bytearrayinputstream(baos.tobytearray())))) { readdoc.copypagesto(1, readdoc.getnumberofpages(), writedoc, new pdfpageformcopier()); } } } } }
it doesn't throw errors when run it.
i had make changes, because didn't know parameters such keylist
about. can execute example, , tell me if problem persists?
- if example still throws error: maybe aren't using recent version of itext 7. please upgrade , try anew.
- if example doesn't throw error: try adapting example step step until error happens again. tell last step performed before error occurred.
update:
when create pdffont
, when use pdffont
instance in context of pdfdocument
, pdffont
"belongs" document, , can no longer reuse it. should create new pdffont
instance every document. doesn't mean can't reuse fontprogram
though.
i'll have updated example. use fontprogramfactory
create fontprogram
(i used quick & dirty way). don't reuse pdffont
, use fontprogram
create new pdffont
every pdfdocument
.
Comments
Post a Comment