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

Andreas Lehmkühler edited comment on PDFBOX-3287 at 3/25/16 4:16 PM:
---------------------------------------------------------------------

Here is the code:
{code}
public void saveImagesFromPDFToFiles(PDDocument document, int page, String 
destinationFolder) throws IOException {

                int counter = 1;
                PDResources resources = document.getPage(page).getResources();
                
                Iterable<COSName> xObjectNames = resources.getXObjectNames();

 
                if (null != xObjectNames) {
                        for (COSName xObjectName : xObjectNames) {

                                PDXObject object = 
resources.getXObject(xObjectName);
 
                                if (object instanceof PDImageXObject) {
                                        PDImageXObject image = (PDImageXObject) 
object;
                                        String suffix = image.getSuffix();
                                        log.info("Suffix: "+suffix);
                                        if (suffix != null) {
                                                if ("jpx".equals(suffix)) {
                                                        suffix = "JPEG2000";
                                                }
                                                boolean writeOK = 
ImageIOUtil.writeImage(image.getImage(), suffix,
                                                                
destinationFolder + "_" + (page + 1) + "_" + counter+".png", 300);

                                                assert (writeOK);

                                                counter++;
                                        }
                                }
                        }
                }
        }
{code}
I am using Windows and Maven with the following pdfbox dependency:
{code}
                <dependency>
                        <groupId>org.apache.pdfbox</groupId>
                        <artifactId>pdfbox-tools</artifactId>
                        <version>2.0.0</version>
                </dependency>
{code}
Let me know if that helps



was (Author: mongoose54):
Here is the code:

public void saveImagesFromPDFToFiles(PDDocument document, int page, String 
destinationFolder) throws IOException {

                int counter = 1;
                PDResources resources = document.getPage(page).getResources();
                
                Iterable<COSName> xObjectNames = resources.getXObjectNames();

 
                if (null != xObjectNames) {
                        for (COSName xObjectName : xObjectNames) {

                                PDXObject object = 
resources.getXObject(xObjectName);
 
                                if (object instanceof PDImageXObject) {
                                        PDImageXObject image = (PDImageXObject) 
object;
                                        String suffix = image.getSuffix();
                                        log.info("Suffix: "+suffix);
                                        if (suffix != null) {
                                                if ("jpx".equals(suffix)) {
                                                        suffix = "JPEG2000";
                                                }
                                                boolean writeOK = 
ImageIOUtil.writeImage(image.getImage(), suffix,
                                                                
destinationFolder + "_" + (page + 1) + "_" + counter+".png", 300);

                                                assert (writeOK);

                                                counter++;
                                        }
                                }
                        }
                }

        }

I am using Windows and Maven with the following pdfbox dependency:

                <dependency>
                        <groupId>org.apache.pdfbox</groupId>
                        <artifactId>pdfbox-tools</artifactId>
                        <version>2.0.0</version>
                </dependency>

Let me know if that helps


> COSStream has been closed and cannot be read. Perhaps its enclosing 
> PDDocument has been closed?
> -----------------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-3287
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3287
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.0
>         Environment: Windows
>            Reporter: Alexandros Karargyris
>            Priority: Critical
>
> I am using the example at 
> https://github.com/apache/pdfbox/blob/93339a0fb36c2cfd80e4837cc4c4206bf3a3877a/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java#L69
> to extract images from a PDF page and save it to a file. However I am running 
> to the following error at this line : 
> https://github.com/apache/pdfbox/blob/93339a0fb36c2cfd80e4837cc4c4206bf3a3877a/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java#L77
> ``
> COSStream has been closed and cannot be read. Perhaps its enclosing 
> PDDocument has been closed?
>       at org.apache.pdfbox.cos.COSStream.checkClosed(COSStream.java:78)
>       at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:156)
>       at 
> org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:235)
>       at 
> org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.<init>(PDImageXObject.java:147)
>       at 
> org.apache.pdfbox.pdmodel.graphics.PDXObject.createXObject(PDXObject.java:70)
>       at 
> org.apache.pdfbox.pdmodel.PDResources.getXObject(PDResources.java:385)
> ``



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

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

Reply via email to