iText 生成PDF 报错的问题
代码大致如下:
FileOutputStream fileOutputStream = null;
PdfWriter write = null;
try {
fileOutputStream = new FileOutputStream(outputFilePath);
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
write = PdfWriter.getInstance(document, fileOutputStream);
write.open();
Paragraph p1 = new Paragraph();
p1.add(contentBuffer.toString());
document.add(p1);
} catch (
IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
结果报了个错:com.itextpdf.text.DocumentException: The document is not open yet; you can only add Meta information.
我调试的时候发现 document的isOpen方法返回 false
------解决方案--------------------document.open();