<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Proc Groovy Error with PDFBOX - You did not close a PDF Document in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Groovy-Error-with-PDFBOX-You-did-not-close-a-PDF-Document/m-p/715758#M80092</link>
    <description>&lt;DIV class="votecell post-layout--left"&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="postcell post-layout--right"&gt;
&lt;DIV class="s-prose js-post-body"&gt;
&lt;P&gt;org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt; 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; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks in advance.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Kriss Harris&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Mon, 01 Feb 2021 12:15:31 GMT</pubDate>
    <dc:creator>djrisks</dc:creator>
    <dc:date>2021-02-01T12:15:31Z</dc:date>
    <item>
      <title>Proc Groovy Error with PDFBOX - You did not close a PDF Document</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Groovy-Error-with-PDFBOX-You-did-not-close-a-PDF-Document/m-p/715758#M80092</link>
      <description>&lt;DIV class="votecell post-layout--left"&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="postcell post-layout--right"&gt;
&lt;DIV class="s-prose js-post-body"&gt;
&lt;P&gt;org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt; 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; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks in advance.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Kriss Harris&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 01 Feb 2021 12:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Groovy-Error-with-PDFBOX-You-did-not-close-a-PDF-Document/m-p/715758#M80092</guid>
      <dc:creator>djrisks</dc:creator>
      <dc:date>2021-02-01T12:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Groovy Error with PDFBOX - You did not close a PDF Document</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Groovy-Error-with-PDFBOX-You-did-not-close-a-PDF-Document/m-p/730530#M80353</link>
      <description>&lt;P&gt;Unsure how many people here can help you on this, but reading the symptoms, is there a way to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Easy: add a timeout after closing a document&lt;/P&gt;
&lt;P&gt;2. Harder: add some logic to check the document if closed&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 21:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Groovy-Error-with-PDFBOX-You-did-not-close-a-PDF-Document/m-p/730530#M80353</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-31T21:45:31Z</dc:date>
    </item>
  </channel>
</rss>

