BookmarkSubscribeRSS Feed
djrisks
Barite | Level 11
I am running the following code so that I can create combined PDF files. And the code works with some PDF's, for example, if I want to combine 10 together. But when I try to combine all of the PDF's, such as 50, I get the following messages in my log, and the PDF file is not created.

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; 

Many thanks in advance.


Kriss Harris

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Unsure how many people here can help you on this, but reading the symptoms, is there a way to 

1. Easy: add a timeout after closing a document

2. Harder: add some logic to check the document if closed

?

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1951 views
  • 0 likes
  • 2 in conversation