This is my code:
这是我的代码:
try {
dozen = magazijn.getFfd().vraagDozenOp();
for (int i = 0; i < dozen.size(); i++) {
PdfWriter.getInstance(doc, new FileOutputStream("Order" + x + ".pdf"));
System.out.println("Writer instance created");
doc.open();
System.out.println("doc open");
Paragraph ordernummer = new Paragraph(order.getOrdernummer());
doc.add(ordernummer);
doc.add( Chunk.NEWLINE );
for (String t : text) {
Paragraph klant = new Paragraph(t);
doc.add(klant);
}
doc.add( Chunk.NEWLINE );
Paragraph datum = new Paragraph (order.getDatum());
doc.add(datum);
doc.add( Chunk.NEWLINE );
artikelen = magazijn.getFfd().vraagArtikelenOp(i);
for (Artikel a : artikelen){
artikelnr.add(a.getArtikelNaam());
}
for (String nr: artikelnr){
Paragraph Artikelnr = new Paragraph(nr);
doc.add(Artikelnr);
}
doc.close();
artikelnr.clear();
x++;
System.out.println("doc closed");
}
} catch (Exception e) {
System.out.println(e);
}
I get this exception: com.itextpdf.text.DocumentException: The document has been closed. You can't add any Elements.
我得到此异常:com.itextpdf.text.DocumentException:文档已关闭。您无法添加任何元素。
can someone help me fix this so that the other pdf can be created and paragrphs added?
有人可以帮我解决这个问题,以便可以创建其他pdf并添加paragrphs吗?
2 个解决方案
#1
2
Alright, your intent is not very clear from your code and question so I'm going to operate under the following assumptions:
好吧,你的意图并不是很清楚你的代码和问题,所以我将在以下假设下运作:
- You are creating a report for each box you're processing
- Each report needs to be a separate PDF file
您正在为正在处理的每个框创建报告
每个报告都需要是一个单独的PDF文件
You're getting a DocumentException
on the second iteration of the loop, you're trying to add content to a Document that has been closed in the previous iteration via doc.close();
. 'doc.close' will finalize the Document
and write everything still pending to any linked PdfWriter
.
你在循环的第二次迭代中得到了一个DocumentException,你试图通过doc.close();将内容添加到上一次迭代中已经关闭的Document中。 'doc.close'将最终确定文档并将所有尚未处理的内容写入任何链接的PdfWriter。
If you wish to create separate pdfs for each box, you need to create a seperate Document
in your loop statement as well, since creating a new PdfWriter
via PdfWriter.getInstance(doc, new FileOutputStream("Order" + x + ".pdf"));
will not create a new Document
on its own.
如果你想为每个盒子创建单独的pdf,你需要在你的循环语句中创建一个单独的Document,因为通过PdfWriter.getInstance创建一个新的PdfWriter(doc,new FileOutputStream(“Order”+ x +“。pdf” ));不会自己创建新文档。
If I'm wrong with assumption 2 and you wish to add everything to a single PDF, move doc.close();
outside of the loop and create only a single PdfWriter
如果我对假设2错了,并且您希望将所有内容添加到单个PDF中,请移动doc.close();在循环之外,只创建一个PdfWriter
#2
-1
You can try something like this using Apache PDFBox
您可以使用Apache PDFBox尝试这样的事情
File outputFile = new File(path);
outputFile.createNewFile();
PDDocument newDoc = new PDDocument();
then create a PDPage
and write what you wanna write in that page. After your page is ready, add it to the newDoc
and in the end save it and close it
然后创建一个PDPage并写下你想在该页面中写的内容。页面准备好后,将其添加到newDoc,最后保存并关闭它
newDoc.save(outputFile);
newDoc.close()
repeat this dozen.size()
times and keep changing the file's name in path
for every new document.
重复此dozen.size()次并继续为每个新文档更改路径中的文件名。
#1
2
Alright, your intent is not very clear from your code and question so I'm going to operate under the following assumptions:
好吧,你的意图并不是很清楚你的代码和问题,所以我将在以下假设下运作:
- You are creating a report for each box you're processing
- Each report needs to be a separate PDF file
您正在为正在处理的每个框创建报告
每个报告都需要是一个单独的PDF文件
You're getting a DocumentException
on the second iteration of the loop, you're trying to add content to a Document that has been closed in the previous iteration via doc.close();
. 'doc.close' will finalize the Document
and write everything still pending to any linked PdfWriter
.
你在循环的第二次迭代中得到了一个DocumentException,你试图通过doc.close();将内容添加到上一次迭代中已经关闭的Document中。 'doc.close'将最终确定文档并将所有尚未处理的内容写入任何链接的PdfWriter。
If you wish to create separate pdfs for each box, you need to create a seperate Document
in your loop statement as well, since creating a new PdfWriter
via PdfWriter.getInstance(doc, new FileOutputStream("Order" + x + ".pdf"));
will not create a new Document
on its own.
如果你想为每个盒子创建单独的pdf,你需要在你的循环语句中创建一个单独的Document,因为通过PdfWriter.getInstance创建一个新的PdfWriter(doc,new FileOutputStream(“Order”+ x +“。pdf” ));不会自己创建新文档。
If I'm wrong with assumption 2 and you wish to add everything to a single PDF, move doc.close();
outside of the loop and create only a single PdfWriter
如果我对假设2错了,并且您希望将所有内容添加到单个PDF中,请移动doc.close();在循环之外,只创建一个PdfWriter
#2
-1
You can try something like this using Apache PDFBox
您可以使用Apache PDFBox尝试这样的事情
File outputFile = new File(path);
outputFile.createNewFile();
PDDocument newDoc = new PDDocument();
then create a PDPage
and write what you wanna write in that page. After your page is ready, add it to the newDoc
and in the end save it and close it
然后创建一个PDPage并写下你想在该页面中写的内容。页面准备好后,将其添加到newDoc,最后保存并关闭它
newDoc.save(outputFile);
newDoc.close()
repeat this dozen.size()
times and keep changing the file's name in path
for every new document.
重复此dozen.size()次并继续为每个新文档更改路径中的文件名。