【问题标题】:How do you extract color profiles from a PDF file using pdfbox (or other open source Java lib)如何使用 pdfbox(或其他开源 Java lib)从 PDF 文件中提取颜色配置文件
【发布时间】:2017-03-24 03:29:29
【问题描述】:

加载文档后:

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

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

【问题讨论】:

  • 1) 可以分享 PDF 吗? 2)您是否知道并非所有 PDF 都有 icc 颜色配置文件? 3)你需要这个来处理很多文件还是只需要一个文件? 4) 请使用您的文件尝试 PDFDebugger。
  • 感谢您的回复。 1) 任何 PDF 2) 不,还没有尝试 groc 颜色配置文件。现在的问题更笼统了 3)任何文件,我都在尝试提取每页或整个文档(取决于可能的情况)颜色配置文件 4)尝试以编程方式执行此操作。如果这是单个文件解决方案,我只会使用 UI 工具。

标签: java pdf pdfbox


【解决方案1】:

我阅读了有关“如何在 PDF 文件中创建/添加意图”的示例。我无法获得有关“如何获得意图”的示例。使用 API/示例,我编写了以下(未经测试的代码)来获取每个 Intent 的 COSStream 对象。看看这对你是否有用。

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

  PDDocumentCatalog cat = doc.getDocumentCatalog();
  List<PDOutputIntent> list = cat.getOutputIntents();

  for (PDOutputIntent e : list) {
    p("PDOutputIntent Found:");
    p("Info="+e.getInfo());
    p("OutputCondition="+e.getOutputCondition());
    p("OutputConditionIdentifier="+e.getOutputConditionIdentifier());
    p("RegistryName="+e.getRegistryName());
    COSStream cstr = e.getDestOutputIntent();
  }

  static void p(String s) {
    System.out.println(s);
  }
}

【讨论】:

  • 我相信 2.0.21.8.11 的版本不同
  • @stevedbrown,我处理了流,但在尝试将流的内容解释为“ICC 配置文件”时没时间了。你得到你想要的了吗?如果您的问题没有得到解决,请告诉我们您的问题。我们将在这里(单独或一起)尽最大努力获得完整的画面。
【解决方案2】:

我不知道这段代码有没有帮助,在搜索下面的链接后,

How do I add an ICC to an existing PDF document

PdfBox - PDColorSpaceFactory.createColorSpace(document, iccColorSpace) throws nullpointerexception

https://pdfbox.apache.org/docs/1.8.11/javadocs/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.html

我找到了一些代码,看看有没有帮助,

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

  PDDocumentCatalog cat = doc.getDocumentCatalog();
  List<PDOutputIntent> list = cat.getOutputIntents();

  PDDocumentCatalog cat = doc.getDocumentCatalog();
  COSArray cosArray = doc.getCOSObject();
  PDICCBased pdCS = new PDICCBased( cosArray );

  pdCS.getNumberOfComponents()

  static void p(String s) {
    System.out.println(s);
  }
}

【讨论】:

  • 在这段代码中留下未使用的静态 void p 让这感觉很像 blackpen 的答案。
  • PDDocumentCatalog cat = doc.getDocumentCatalog(); COSArray cosArray = doc.getCOSObject(); PDICCBased pdCS = new PDICCBased( cosArray ); 复制了他的代码并添加了这 3 行 ;)
  • @Hemakumar "复制了他的代码并添加了这 3 行 ;)" 我称之为抄袭,你不应该从其他答案中复制和粘贴代码。真丢脸。
  • @Marcs 你将如何修复别人代码中的错误......而不使用他们的代码......?我没有复制和粘贴相同的答案...我修改了代码,我觉得需要进行一些修改。问题需要正确的解决方案,我试着给出我的观点就是这样。谢谢你让我感到羞耻。我对此并不担心。 :)
  • @Hemakumar 你将如何修复别人代码中的错误...你在答案中发表评论并解决错误,你所做的只是简单的窃取.
【解决方案3】:

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

    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();

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

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

【讨论】:

  • 你知道是否有任何方法可以检测到 Pantone 匹配吗?
  • @stevedbrown 不知道这些。我只能告诉你哪里有 icc 配置文件,而不是它们的质量。
【解决方案4】:

使用 itext pdf 库(旧版本 4.2.1 的分支)你可以做到。喜欢:

PdfReader reader = new com.lowagie.text.pdf.PdfReader(Path pathToPdf);
PRStream stream = (PRStream) reader.getCatalog().getAsDict(PdfName.DESTOUTPUTPROFILE);
if (stream != null)
 {
  byte[] destProfile = PdfReader.getStreamBytes(stream);
 }

为了从每个页面中提取配置文件,您可以遍历每个页面,例如

for(int i = 1; i <= pdfReader.getNumberOfPages(); i++)
 {
  PRStream prStream = (PRStream) pdfReader.getPageN(i).getDirectObject(PdfName.DESTOUTPUTPROFILE);
 if (stream != null)
  {
   byte[] destProfile = PdfReader.getStreamBytes(stream);
  }
 }

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 2014-07-11
    • 2018-12-10
    • 2015-11-19
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2018-01-31
    相关资源
    最近更新 更多