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

Adrian Sandor commented on PDFBOX-4294:
---------------------------------------

The javadoc helps, but I'm still generally unsatisfied with the way the class 
works. It has too much code to manage loading and closing of various documents, 
and it's quite clearly made mainly for a particular use case of overlaying all 
the pages the same way (and only once), with potential differences for the 
first/last page and even/odd pages. That's not how I want to use it.

> What you do (calling {{overlay()}} several times) does not really make sense.

Well, it makes sense for what I want to do, but I had a wrong assumption that I 
could reuse the original document, while it was in fact modifying it (rather 
than a copy) in the process. So in order to redo the overlay, I'd have to 
reload the document every time, which is quite inefficient.

 

Fortunately the code is free/open source, so I was able to modify it to work 
exactly the way I wanted it. I'll attach the class I came up with (which is 
very different from an API point of view) in case you're interested.

> Scratch file already closed when redoing overlay
> ------------------------------------------------
>
>                 Key: PDFBOX-4294
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4294
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.0.11
>            Reporter: Adrian Sandor
>            Priority: Major
>         Attachments: MyOverlay.java
>
>
> Here's some sample code to reproduce the problem:
>  
> {code:java}
> final String path = "orig.pdf";
> final String stampPath = "stamp.pdf";
> final PDDocument doc = PDDocument.load(new File(path));
> final Overlay o = new Overlay();
> o.setInputPDF(doc);
> o.setOverlayPosition(Position.FOREGROUND);
> final HashMap<Integer, String> m = new HashMap<>();
> m.put(0, stampPath);
> PDDocument res = o.overlay(m);
> res.close();
> res = o.overlay(m);
> {code}
> Basically I'm doing an overlay, closing the resulting document, then doing it 
> again (normally I would make some changes first, but I didn't include that 
> for the sake of brevity).
> The last line throws java.io.IOException: Scratch file already closed
> If I remove "res.close()" then the code works, but if I do the overlay 
> multiple times, I get warnings like: org.apache.pdfbox.cos.COSDocument - 
> Warning: You did not close a PDF Document
> which suggests that I should actually close the document when I'm done with 
> it.
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to