首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用pdfbox将pdf转换为png时文本周围的红色区域

使用pdfbox将pdf转换为png时文本周围的红色区域
EN

Stack Overflow用户
提问于 2019-01-22 01:15:18
回答 1查看 157关注 0票数 2

我正在尝试使用pdfbox将pdf转换为png文件。不幸的是,在输出的某些地方,我得到了奇怪的红色区域。我不知道有什么问题。只有一些pdf文件有这个问题。

下面是我使用的一些代码:

代码语言:javascript
复制
    public static BufferedImage generateFromPdf(String ref, InputStream stream, int pageIndex, PreviewMode mode) throws IOException {
        PDDocument doc = null;
        try (InputStream buffered = new BufferedInputStream(stream)) {
            doc = PDDocument.load(buffered, PDF_LOADING_MEMORY_SETTING);
            if (pageIndex > doc.getNumberOfPages()) {
                return null;
            }
            PDFRenderer renderer = new PDFRenderer(doc);
            return rasterizePdfBox(ref, pageIndex, renderer, mode);
        } finally {
            if (doc != null) {
                doc.close();
            }
        }
    }

然后:

代码语言:javascript
复制
    private static BufferedImage rasterizePdfBox(String ref, int pageIndex, PDFRenderer renderer, PreviewMode mode) throws IOException {
        Future<BufferedImage> result = executorService.submit(() -> {
            LOGGER.info(String.format("Generate preview for ref: %s, page: %s, mode: %s ", ref, pageIndex, mode.name()));
            return renderer.renderImageWithDPI(pageIndex - 1, mode.getDpi(), ImageType.RGB);
        });

        try {
            return result.get();
        } catch (InterruptedException | ExecutionException e) {
            LOGGER.error(String.format("Error when generating preview: %s", e.getMessage()));
            Thread.currentThread().interrupt();
            throw new IOException(e.getMessage());
        }
    }

到目前为止,我只知道当我在linux上的Master PDF editor中打开它们时,输出中红色的地方是空白的。不过,当我用Document Viewer打开它们时,它们看起来很正常。

一些提示:-有问题的pdf已被扫描。我可以选择工作部件周围的文本,但不能选择覆盖有红色覆盖的地方。-如果我使用linux工具convert not-working-pdf.pdf converted.pdf,然后尝试将这个文件转换为png,那么问题就不再存在了。

下面是一个示例文件:https://ufile.io/3or9l

pdfbox版本: 2.0.13

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 04:00:53

这是一个PDFBox错误,原因是一个带有蒙版的黑白图像,这是不寻常的。光栅中只有一个颜色元素,因此只应用"R“,而不是RGB目标的所有3个。正因为如此,白色显示为红色。

在issue PDFBOX-4470中有关此错误的更多详细信息,它将在版本2.0.14中修复。在此之前,您可以使用snapshot

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

https://stackoverflow.com/questions/54294852

复制
相关文章

相似问题

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