我正在使用Itext 7.1.6生成PDF。当我尝试运行时,我得到此错误文档已关闭。无法执行该动作。
我为此检查了stackoverflow中的解决方案,但他们说这与字体有关。每次使用实例时,我都会通过创建实例来修改字体,但即使这样我也会遇到同样的问题。
我无法弄清楚如何解决此问题。谁能帮我解决这个问题?
请找到以下代码和异常
Document doc = null;
PdfPage sourcePage = null;
try ( InputStream resource = new FileInputStream(new File(Paths.get("Output").toAbsolutePath()+"/source.pdf"));
PdfReader pdfReader = new PdfReader(resource);
PdfDocument pdfDocument = new PdfDocument(pdfReader) ) {
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
MarginFinder strategy = contentParser.processContent(1, new MarginFinder());
sourcePage = pdfDocument.getPage(1);
sourcePage.setCropBox(strategy.getBoundingBox());
sourcePage.setMediaBox(strategy.getBoundingBox());
}
@SuppressWarnings("resource")
PdfWriter writer = new PdfWriter(new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/final.pdf"))).setSmartMode(true);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.setDefaultPageSize(PageSize.A3.rotate());
String fonts[] = {Paths.get("fonts").toAbsolutePath() + "/TREBUC.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBD.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBI.TTF",Paths.get("fonts").toAbsolutePath() + "/TREBUCIT.TTF"};
FontProvider fontProvider = new FontProvider();
Map<String, PdfFont> pdfFontMap = new HashMap<String, PdfFont>();
for (String font : fonts) {
FontProgram fontProgram = FontProgramFactory.createFont(font);
if (font.endsWith("TREBUC.TTF")) {
pdfFontMap.put("NORMAL", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBD.TTF")) {
pdfFontMap.put("BOLD", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBI.TTF")) {
pdfFontMap.put("BOLD_ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCIT.TTF")) {
pdfFontMap.put("ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
}
fontProvider.addFont(fontProgram);
}
TestVisualSummaryNew testVisualSummaryNew = new TestVisualSummaryNew();
NormalPageHeader headerHandler = testVisualSummaryNew.new NormalPageHeader(Paths.get("images").toAbsolutePath() + "\\logo.png", pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
PageEndEvent pageEndEvent = testVisualSummaryNew.new PageEndEvent(Paths.get("images").toAbsolutePath() + "\\FooterLineExternal.png" ,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, pageEndEvent);
doc = new Document(pdfDoc);
doc.setTopMargin(55);
PdfFormXObject xobject = sourcePage.copyAsFormXObject(pdfDoc);
Rectangle xobjectBoundaryBox = xobject.getBBox().toRectangle();
xobject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[] {1, 0, 0, 1, -xobjectBoundaryBox.getLeft(), -xobjectBoundaryBox.getBottom()}));
Image image = new Image(xobject);
image.setAutoScale(true);
doc.add(image);
System.out.println("Converted to PDF Succesfully >>> source.pdf");
例外
com.itextpdf.kernel.PdfException:文档已关闭。它是 无法执行动作。在 com.itextpdf.kernel.pdf.PdfDocument.checkClosingStatus(PdfDocument.java:2041) 在 com.itextpdf.kernel.pdf.PdfDocument.getWriter(PdfDocument.java:706) 在 com.itextpdf.kernel.pdf.PdfIndirectReference.getWriter(PdfIndirectReference.java:270) 在com.itextpdf.kernel.pdf.PdfObject.copyTo(PdfObject.java:318)处 com.itextpdf.kernel.pdf.PdfPage.copyAsFormXObject(PdfPage.java:439)
答案 0 :(得分:2)
已向您的in comment previous question表示
您似乎已经关闭了源文档。然后必须将其关闭才能允许从中复制。
因此,请不要过早关闭它,既不要显式关闭也不可以在try-with-resources
中创建它:
Document doc = null;
PdfDocument pdfDocument = null; //!!!
PdfPage sourcePage = null;
try ( InputStream resource = new FileInputStream(new File(Paths.get("Output").toAbsolutePath()+"/test.pdf")); ) { //!!!
PdfReader pdfReader = new PdfReader(resource); //!!!
pdfDocument = new PdfDocument(pdfReader); //!!!
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
MarginFinder strategy = contentParser.processContent(1, new MarginFinder());
sourcePage = pdfDocument.getPage(1);
sourcePage.setCropBox(strategy.getBoundingBox());
sourcePage.setMediaBox(strategy.getBoundingBox());
//pdfDocument.close(); //!!!
}
try {
@SuppressWarnings("resource")
PdfWriter writer = new PdfWriter(new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/final.pdf"))).setSmartMode(true);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.setDefaultPageSize(PageSize.A3.rotate());
String fonts[] = {Paths.get("fonts").toAbsolutePath() + "/TREBUC.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBD.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBI.TTF",Paths.get("fonts").toAbsolutePath() + "/TREBUCIT.TTF"};
FontProvider fontProvider = new FontProvider();
Map<String, PdfFont> pdfFontMap = new HashMap<String, PdfFont>();
for (String font : fonts) {
FontProgram fontProgram = FontProgramFactory.createFont(font);
if (font.endsWith("TREBUC.TTF")) {
pdfFontMap.put("NORMAL", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBD.TTF")) {
pdfFontMap.put("BOLD", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBI.TTF")) {
pdfFontMap.put("BOLD_ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCIT.TTF")) {
pdfFontMap.put("ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
}
fontProvider.addFont(fontProgram);
}
TestVisualSummaryNew testVisualSummaryNew = new TestVisualSummaryNew();
NormalPageHeader headerHandler = testVisualSummaryNew.new NormalPageHeader(Paths.get("images").toAbsolutePath() + "\\logo.png", pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
PageEndEvent pageEndEvent = testVisualSummaryNew.new PageEndEvent(Paths.get("images").toAbsolutePath() + "\\FooterLineExternal.png" ,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, pageEndEvent);
doc = new Document(pdfDoc);
doc.setTopMargin(55);
PdfFormXObject xobject = sourcePage.copyAsFormXObject(pdfDoc);
Rectangle xobjectBoundaryBox = xobject.getBBox().toRectangle();
xobject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[] {1, 0, 0, 1, -xobjectBoundaryBox.getLeft(), -xobjectBoundaryBox.getBottom()}));
Image image = new Image(xobject);
image.setAutoScale(true);
doc.add(image);
pdfDoc.close();
doc.close();
System.out.println("Converted to PDF Succesfully >>> convertedSvg_" + uuid + ".pdf");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error Occured while converting to PDF = " + e.getMessage());
}
pdfDocument.close(); //!!!