有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

PDFBOX org出现java Proc Groovy错误。阿帕奇。pdfbox。余弦。COSDocument finalize警告:警告:您没有关闭PDF文档

我正在运行以下代码,以便创建组合PDF文件。代码可以与一些PDF一起使用,例如,如果我想将10个结合在一起。但是当我尝试合并所有的PDF文件时,比如50个,我会在我的日志中收到以下消息,并且不会创建PDF文件

org.apache.pdfbox.cos.COSDocument finalize
WARNING: Warning: You did not close a PDF Document

代码:

proc groovy;
   add classpath = "K:/qa/common/java/groovy-json-2.5.6.jar";
   add classpath = "K:/qa/common/java/pdfbox-app-2.0.15.jar";

   submit;
       import org.apache.pdfbox.pdmodel.PDDocument;
       import org.apache.pdfbox.pdmodel.PDPage;
       import org.apache.pdfbox.pdmodel.PDPageContentStream;
       import org.apache.pdfbox.pdmodel.font.PDType1Font;
       import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
       import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
       import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
       import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
       import org.apache.pdfbox.pdmodel.common.PDStream;
       import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
       import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo;
       import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
       import org.apache.pdfbox.pdmodel.common.PDRectangle;
       import org.apache.pdfbox.pdmodel.font.PDFont;
       import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;

       import java.io.BufferedReader;
       import java.io.File;
       import java.io.FileReader;
       import java.io.IOException;
       import java.nio.file.Files;
       import java.nio.file.Path;
       import java.nio.file.Paths;
       import java.util.List;
       import java.awt.Color;


       public class ListFiles 
       {
           public static PDDocument doc;
           public static PDDocumentOutline outline;
           public static PDOutlineItem pagesOutline;

           public static PDPage toc;

           public static PDPageXYZDestination tocMain;
           public static PDActionGoTo action;
           public static PDAnnotationLink link;
           public static PDRectangle rect;
           public static PDPageDestination pdest;
           public static PDPageContentStream stream;
           public static PDFont font = PDType1Font.HELVETICA_BOLD;

           private static String tlfloc = "K:/qa/for_pdf/NSCLC/";
           private static double X_TOC_POS = 85;
           private static double Y_TOC_POS = 720;
           private static double INCREM = -35;

           private static float LOWER_LEFT_X = 72.0f;
           private static float LOWER_LEFT_Y = 700.0f;
           private static float UPPER_RIGHT_X = 610.0f;
           private static float UPPER_RIGHT_Y = 730.0f;
           private static float RECT_INCREM = -35.0f;

           private static float TOC_ROW_FONT_SIZE = 6.0f;

           public static ListFiles listFiles;

           public static void main(String[] args)  
           {  
               doc = new PDDocument();

               toc = new PDPage();
               doc.addPage(toc);

               PDDocumentOutline outline = new PDDocumentOutline();
               doc.getDocumentCatalog().setDocumentOutline(outline);
               pagesOutline = new PDOutlineItem();
               pagesOutline.setTitle("All TFLs");
               outline.addLast(pagesOutline);

               File file = new File("K:/qa/for_pdf/NSCLC/NSCLC_Titles.csv"); 
               listFiles = new ListFiles();
               listFiles.listAllFiles(file);
  
               pagesOutline.openNode();
               outline.openNode();

               tocMain = new PDPageXYZDestination();
               tocMain.setPage(toc);
               tocMain.setLeft(0);
               tocMain.setTop(0); // link to top of page, this is the XYZ part

               doc.save(new File("K:/qa/for_pdf/NSCLC/Combined_PDF_NSCLC3.pdf"));
               doc.close();
           }

           // Uses listFiles method  
           public  void tocLink(PDPage thePage, String theRowText, double theIncrem , float llx, float lly, float urx, float ury) throws IOException
           {  
               try
               {
                   PDPageContentStream stream = new PDPageContentStream(doc, toc , true, true);

                   action = new PDActionGoTo();
                   action.setDestination(tocMain);

                   // add the GoTo action
                   pdest = new PDPageFitWidthDestination();
                   pdest.setPage(thePage);
                   action.setDestination(pdest);

                   link = new PDAnnotationLink();

                   link.setAction(action);
                   stream.setNonStrokingColor(Color.white);
                   rect = new PDRectangle();
                   rect.setLowerLeftX(llx);
                   rect.setLowerLeftY(lly);
                   rect.setUpperRightX(urx);
                   rect.setUpperRightY(ury);
                   link.setRectangle(rect);

                   toc.getAnnotations().add(link);
                   
                   stream.setNonStrokingColor(Color.black);
                   stream.beginText();
                   stream.setFont(font, TOC_ROW_FONT_SIZE);
                   stream.setTextTranslation(X_TOC_POS,  Y_TOC_POS + theIncrem); 
                   stream.drawString(theRowText);
                   stream.endText(); 
                   stream.close(); 
               }
               catch (Exception e)
               {   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }

           // Uses listFiles method  
           public void listAllFiles(File theFile)
           {
               BufferedReader br = new BufferedReader(new FileReader(theFile)); 
  
               String st; 
               int tlgCount = 1;
               st = br.readLine();
               while ((st = br.readLine()) != null) 
               {   
                   String[] array2 = st.split(",",2);
                   File pdf = new File(tlfloc + array2[0] + ".pdf");
                    
                   readContent(pdf, array2[1], tlgCount);
                   tlgCount ++;
               }
           }

           public void readContent(File file, String theTLGTitle, int tlgIndex) throws IOException
           {
               try
               {   File file2 = new File(file.getCanonicalPath());
                   PDDocument tempDoc = PDDocument.load(file2);
                   
                   int numOfPages = tempDoc.getNumberOfPages();
                   
                   // Stores the first page of TLG. This will be used as the bookmark destination.
                   PDPage page = (PDPage) tempDoc.getDocumentCatalog().getPages().get(0);

                   for(int i = 0; i < numOfPages; i++)
                   {   PDPage firstPage = (PDPage) tempDoc.getDocumentCatalog().getPages().get(i);
                       doc.addPage(firstPage);

                       // Set the bookmark title(file name) to reference first page of TFL. 
                       if (i == 0) 
                       {
                           PDPageDestination dest = new PDPageFitWidthDestination();
                           dest.setPage(page);
                           PDOutlineItem bookmark = new PDOutlineItem();
                           bookmark.setDestination(dest);
                           String title = theTLGTitle;
                           bookmark.setTitle(title);
                           pagesOutline.addLast(bookmark);

                           listFiles.tocLink(firstPage, theTLGTitle, INCREM * (tlgIndex), (float) LOWER_LEFT_X ,(float) LOWER_LEFT_Y + (RECT_INCREM * (tlgIndex)),
                                         (float) UPPER_RIGHT_X ,(float) UPPER_RIGHT_Y + (RECT_INCREM * (tlgIndex)));

                       }               
                   }    
               }
               catch (Exception e)
               {   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           } 

     }
   endsubmit;
quit; 

提前谢谢你


共 (0) 个答案