Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解决使用iText 7.1.6关闭文档的错误?_Java_Pdf_Itext_Itext7 - Fatal编程技术网

Java 如何解决使用iText 7.1.6关闭文档的错误?

Java 如何解决使用iText 7.1.6关闭文档的错误?,java,pdf,itext,itext7,Java,Pdf,Itext,Itext7,我正在使用Itext 7.1.6生成PDF。当我尝试运行时,我发现此错误文档已关闭。执行该操作是不可能的 我检查了stackoverflow中的解决方案,但他们说这与字体有关。我修改了字体创建实例,每次我使用它,但即使这样,我得到相同的问题 我不知道如何解决这个问题。谁能帮我修一下吗 请查找以下代码和异常 Document doc = null; PdfPage sourcePage = null; try ( InputStream resource = new FileInput

我正在使用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");
单据单据=null;
PdfPage sourcePage=null;
try(InputStream resource=newfileinputstream(新文件(path.get(“Output”).toabsolutionpath()+“/source.pdf”);
PdfReader PdfReader=新PdfReader(资源);
PdfDocument PdfDocument=新PdfDocument(pdfReader)){
PdfDocumentContentParser contentParser=新的PdfDocumentContentParser(pdfDocument);
MarginFinder strategy=contentParser.processContent(1,新的MarginFinder());
sourcePage=pdfDocument.getPage(1);
setCropBox(strategy.getBoundingBox());
setMediaBox(strategy.getBoundingBox());
}
@抑制警告(“资源”)
PdfWriter writer=new PdfWriter(新文件outputStream(新文件(path.get(“Output”).toabsolutionPath()+“/final.pdf”)))。setSmartMode(true);
PdfDocument pdfDoc=新的PdfDocument(writer);
setDefaultPageSize(PageSize.A3.rotate());
字符串字体[]={Paths.get(“字体”).toAbsolutePath()+“/TREBUC.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCBD.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCBI.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCIT.TTF”};
FontProvider FontProvider=新FontProvider();
Map pdfFontMap=newhashmap();
用于(字符串字体:字体){
FontProgram FontProgram=FontProgramFactory.createFont(字体);
if(font.endsWith(“TREBUC.TTF”)){
pdfFontMap.put(“正常”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}else if(font.endsWith(“TREBUCBD.TTF”)){
pdfFontMap.put(“粗体”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}else if(font.endsWith(“TREBUCBI.TTF”)){
pdfFontMap.put(“粗体”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}else if(font.endsWith(“TREBUCIT.TTF”)){
pdfFontMap.put(“ITALIC”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}
addFont(fontProgram);
}
TestVisualSummaryNew TestVisualSummaryNew=新建TestVisualSummaryNew();
NormalPageHeader headerHandler=testVisualSummaryNew.new NormalPageHeader(path.get(“images”).ToAbsolutionPath()+“\\logo.png”,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.START_页面,标题处理程序);
PageEndEvent PageEndEvent=testVisualSummaryNew.new PageEndEvent(path.get(“images”).ToAbsolutionPath()+“\\FooterLineExternal.png”,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.END_第页,第EndEvent页);
doc=新文件(pdfDoc);
setTopMargin文件(55);
PdfFormXObject xobject=sourcePage.copyAsFormXObject(pdfDoc);
矩形xobjectBoundaryBox=xobject.getBBox().toRectangle();
put(PdfName.Matrix,new PdfArray(new float[]{1,0,0,1,-xobjectBoundaryBox.getLeft(),-xobjectBoundaryBox.getBottom());
图像=新图像(xobject);
image.setAutoScale(真);
文档添加(图像);
System.out.println(“成功转换为PDF>>>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)

正如已经向您的

您当时似乎已经关闭了源文档。然后不能关闭它以允许从中复制

因此,不要过早地关闭它,既不要显式地关闭,也不要通过在
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();  //!!!
单据单据=null;
PdfDocument PdfDocument=null;/!!!
PdfPage sourcePage=null;
试试(InputStream resource=newfileinputstream(新文件(path.get(“Output”).toabsolutionpath()+“/test.pdf”);){/!!!
PdfReader PdfReader=新PdfReader(资源);/!!!
pdfDocument=新pdfDocument(pdfReader);/!!!
PdfDocumentContentParser contentParser=新的PdfDocumentContentParser(pdfDocument);
MarginFinder strategy=contentParser.processContent(1,新的MarginFinder());
sourcePage=pdfDocument.getPage(1);
setCropBox(strategy.getBoundingBox());
setMediaBox(strategy.getBoundingBox());
//pdfDocument.close();/!!!
}
试一试{
@抑制警告(“资源”)
PdfWriter writer=new PdfWriter(新文件outputStream(新文件(path.get(“Output”).toabsolutionPath()+“/final.pdf”)))。setSmartMode(true);
PdfDocument pdfDoc=新的PdfDocument(writer);
setDefaultPageSize(PageSize.A3.rotate());
字符串字体[]={Paths.get(“字体”).toAbsolutePath()+“/TREBUC.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCBD.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCBI.TTF”,Paths.get(“字体”).toAbsolutePath()+“/TREBUCIT.TTF”};
FontProvider FontProvider=新FontProvider();
Map pdfFontMap=newhashmap();
用于(字符串字体:字体){
FontProgram FontProgram=FontProgramFactory.createFont(字体);
if(font.endsWith(“TREBUC.TTF”)){
pdfFontMap.put(“正常”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}else if(font.endsWith(“TREBUCBD.TTF”)){
pdfFontMap.put(“粗体”,PdfFontFactory.createFont(fontProgram,PdfEncodings.WINANSI,true));
}else if(font.endsWith(“TREBUCBI.TTF”)){