首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFBox锁定JPEG输入文件,直到应用程序退出

PDFBox锁定JPEG输入文件,直到应用程序退出
EN

Stack Overflow用户
提问于 2016-01-19 08:40:55
回答 1查看 269关注 0票数 1

我在Windows7环境中使用PDFBox RC2,Java 1.8_66。我正在使用它从一个200dpi页面大小的图像文件集合中创建一个PDF,包括JPEG和PNG。

事实证明,在将JPEG文件添加到PDF时,PDImageXObject.createFromFile()例程无法关闭内部文件句柄,从而在应用程序的生命周期内锁定图像文件。将PNG文件添加到PDF时,没有问题。

下面是一些重现该问题的示例代码。使用进程资源管理器(从sysinternals),查看java.exe进程的打开文件句柄并运行此代码。我的测试使用了大约20个全尺寸的JPEG文件。请注意,在该方法退出后,仍会保留几个锁定的文件。

代码语言:javascript
复制
public Boolean CreateFromImages_Broken(String pdfFilename, String[] imageFilenames) {

    PDDocument doc = new PDDocument();        
    for (String imageFilename : imageFilenames) {

        try {
            PDPage page = new PDPage();
            doc.addPage(page);

            PDImageXObject pdImage = PDImageXObject.createFromFile(imageFilename, doc);

            // at this point, if the imageFilename is a jpeg, pdImage holds onto a handle for 
            // the given imageFilename and that file remains locked until the application is closed

            try (PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
                float scale = (float)72.0 / 200;
                page.setMediaBox(new PDRectangle((int)(pdImage.getWidth() * scale), (int)(pdImage.getHeight() * scale)));
                contentStream.drawImage(pdImage, 0, 0, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
            }
        } catch (IOException ioe) {
            return false;
        }                          
    }

    try {
        doc.save(pdfFilename);
        doc.close();                  
    } catch (IOException ex) {
        return false;
    }

    return true;
} 
EN

回答 1

Stack Overflow用户

发布于 2016-01-19 08:40:55

作为一种变通方法,我回顾了PNG和JPEG处理的源代码,我已经成功地实现了这一点,似乎这两种文件类型都适用:

代码语言:javascript
复制
public Boolean CreateFromImages_FIXED(String pdfFilename, String[] imageFilenames) {

    PDDocument doc = new PDDocument();        
    for (String imageFilename : imageFilenames) {

        FileInputStream fis = null;

        try {
            PDPage page = new PDPage();
            doc.addPage(page);

            PDImageXObject pdImage = null;

            // work around JPEG issue by opening up our own stream, with which
            // we can close ourselves instead of PDFBOX leaking it. For PNG
            // images, the createFromFile seems to be OK
            if (imageFilename.toLowerCase().endsWith(".jpg")) {
                fis = new FileInputStream(new File(imageFilename));
                pdImage = JPEGFactory.createFromStream(doc, fis);
            } else {
                pdImage = PDImageXObject.createFromFile(imageFilename, doc);
            }

            try (PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
                float scale = (float)72.0 / 200;
                page.setMediaBox(new PDRectangle((int)(pdImage.getWidth() * scale), (int)(pdImage.getHeight() * scale)));
                contentStream.drawImage(pdImage, 0, 0, pdImage.getWidth()*scale, pdImage.getHeight()*scale);                    

                if (fis != null) {
                    fis.close();
                    fis = null;
                }                    
            }

        } catch (IOException ioe) {
            return false;
        }                          
    }

    try {
        doc.save(pdfFilename);
        doc.close();                  
    } catch (IOException ex) {
        return false;
    }

    return true;        
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34866671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档