[ 
https://issues.apache.org/jira/browse/PDFBOX-3612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15721682#comment-15721682
 ] 

André Heuner commented on PDFBOX-3612:
--------------------------------------

Ok, got it - for the notes:
in order to avoid any warnings it's important to close the Overlay instance. In 
addition, overlay should not already be closed at the point of time when the 
copy with watermark is saved. So my new version of addWatermark(...) returns 
the overlay instance being used for the watermark:

{code}
        public static Overlay addWatermark(PDDocument document) {
                try {
                HashMap<Integer, String> overlayGuide = new HashMap<Integer, 
String>();
                
                for(int no = 0; no < document.getNumberOfPages(); no++) {
                        overlayGuide.put(no +1, "C:\\Software\\watermark.pdf");
                }
            
                Overlay overlay = new Overlay();
                overlay.setInputPDF(document);
                overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
                
                overlay.overlay(overlayGuide);
        
                return overlay;
                
                } catch (Exception e) {
                        log.error("Error while adding watermark: " 
+e.getMessage(), e);
                        
                        return null;
                }
        }
{code}

... and closes it after saving:

{code}
        PDDocument pdfDocument = PDDocument.load(new 
File("C:\\Software\\invoice.pdf"));
        PDDocument pdfCopy = Pdf.extract(pdfDocument, 1, 1);
        Overlay overlay = Pdf.addWatermark(pdfCopy);
                    
        ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();
        pdfCopy.save(byteArrayOutputStream);
        byteArrayOutputStream.close();
                  
        overlay.close();
        pdfCopy.close();
        pdfDocument.close();
{code}

Seems to work and does not show up any warnings.

> ScratchFileBuffer not closed message related to overlay.overlay(overlayGuide);
> ------------------------------------------------------------------------------
>
>                 Key: PDFBOX-3612
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3612
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.3, 2.1.0
>         Environment: Windows 7 Professional, Eclipse, JDK 1.8
>            Reporter: André Heuner
>            Priority: Minor
>
> I'd like to add a watermark to a PDF and have the following code:
> {code:java}
>       @Test
>       public void testAddWatermark() {
>           try {
>               // test runs well with n < 3; with n < 10 and w/o 
> overlay.close() warnings are shown
>               for (int n = 0; n < 10; n++) { 
>                   PDDocument pdfDocument = PDDocument.load(new 
> File("C:\\data\\somePdfFile.pdf"));
>                   PDDocument pdfCopy = Pdf.extract(pdfDocument, 1, 1);
>                   PDDocument watermarkCopy = Pdf.addWatermark(pdfCopy);
>                           
>                   ByteArrayOutputStream byteArrayOutputStream = new 
> ByteArrayOutputStream();
>                   // save as bytes; exception in comment thrown here if 
> overlay.close() is called
>                   watermarkCopy.save(byteArrayOutputStream); 
>                   byteArrayOutputStream.close();
>                               
>                   watermarkCopy.close();
>                   pdfCopy.close();
>                   pdfDocument.close();
>               }       
>           } catch (Exception e) {
>               e.printStackTrace();
>           }    
>       }
>       ....
>       public static PDDocument addWatermark(PDDocument document) {
>           try {
>               HashMap<Integer, String> overlayGuide = new HashMap<Integer, 
> String>();
>               
>               for(int no = 0; no < document.getNumberOfPages(); no++) {
>                       overlayGuide.put(no +1, "C:\\data\\watermark.pdf");
>               }
>           
>               Overlay overlay = new Overlay();
>               overlay.setInputPDF(document);
>               overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
>               
>               PDDocument result = overlay.overlay(overlayGuide);
>               // overlay.close();
>               
>               return result;
>               
>           } catch (Exception e) {
>               log.error("Error while adding watermark: " +e.getMessage(), e);
>                       
>               return document;
>           }
>       }{code}
> overlay.overlay(...) seems to cause lot's of warnings:
> {code}
> ...
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] WARN  [org.apache.pdfbox.cos.COSDocument] Warning: You 
> did not close a PDF Document 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] WARN  [org.apache.pdfbox.cos.COSDocument] Warning: You 
> did not close a PDF Document 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> [2016-12-02-09:55:44] DEBUG [org.apache.pdfbox.io.ScratchFileBuffer] 
> ScratchFileBuffer not closed! 
> ...
> {code}
> Is there anything, that can be done to fix this issue? I tried to close 
> anything that is closable - but the issue remains. Or can it safely be 
> ignored?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to