如何使用 pdfbox(或其他开源 Java 库)从 PDF 文件中提取颜色配置文件 [英] How do you extract color profiles from a PDF file using pdfbox (or other open source Java lib)

查看:83
本文介绍了如何使用 pdfbox(或其他开源 Java 库)从 PDF 文件中提取颜色配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载文档后:

public static void main(String[] args) throws IOException {
    PDDocument doc = PDDocument.load(new File("blah.pdf"));

如何从 PDDocument 中逐页获取打印颜色意图?我阅读了文档,但没有看到报道.

How do you get the page by page printing color intent from the PDDocument? I read the docs, didn't see coverage.

推荐答案

这将获得输出意图(您将通过高质量的 PDF 文件获得这些意图)以及色彩空间和图像的 icc 配置文件:

This gets the output intents (you'll get these with high quality PDF files) and also the icc profiles for colorspaces and images:

    PDDocument doc = PDDocument.load(new File("XXXXX.pdf"));
    for (PDOutputIntent oi : doc.getDocumentCatalog().getOutputIntents())
    {
        COSStream destOutputIntent = oi.getDestOutputIntent();
        String info = oi.getOutputCondition();
        if (info == null || info.isEmpty())
        {
            info = oi.getInfo();
        }
        InputStream is = destOutputIntent.createInputStream();
        FileOutputStream fos = new FileOutputStream(info + ".icc");
        IOUtils.copy(is, fos);
        fos.close();
        is.close();
    }
    for (int p = 0; p < doc.getNumberOfPages(); ++p)
    {
        PDPage page = doc.getPage(p);
        for (COSName name : page.getResources().getColorSpaceNames())
        {
            PDColorSpace cs = page.getResources().getColorSpace(name);
            if (cs instanceof PDICCBased)
            {
                PDICCBased iccCS = (PDICCBased) cs;
                InputStream is = iccCS.getPDStream().createInputStream();
                FileOutputStream fos = new FileOutputStream(System.currentTimeMillis() + ".icc");
                IOUtils.copy(is, fos);
                fos.close();
                is.close();
            }
        }
        for (COSName name : page.getResources().getXObjectNames())
        {
            PDXObject x = page.getResources().getXObject(name);
            if (x instanceof PDImageXObject)
            {
                PDImageXObject img = (PDImageXObject) x;
                if (img.getColorSpace() instanceof PDICCBased)
                {
                    InputStream is = ((PDICCBased) img.getColorSpace()).getPDStream().createInputStream();
                    FileOutputStream fos = new FileOutputStream(System.currentTimeMillis() + ".icc");
                    IOUtils.copy(is, fos);
                    fos.close();
                    is.close();
                }
            }
        }
    }
    doc.close();

这不会做什么(但如果需要,我可以添加一些):

What this doesn't do (but I could add some of it if needed):

  • 阴影、图案、xobject 形式、外观流资源的颜色空间
  • 在 DeviceN 和 Separation 等色彩空间中递归
  • 模式中的递归、xobject 形式、软掩码

这篇关于如何使用 pdfbox(或其他开源 Java 库)从 PDF 文件中提取颜色配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆