如何使用 pdfbox(或其他开源 Java 库)从 PDF 文件中提取颜色配置文件

How do you extract color profiles from a PDF file using pdfbox (or other open source Java lib)

加载文档后:

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

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

我阅读了 "How to create/add Intents to a PDF file" 上的示例。我无法在 "How to get intents" 上找到示例。使用 API/examples,我编写了以下(未经测试的代码)来为每个 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);
  }
}

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

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

这会获取输出意图(您将获得高质量的 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 形式、软掩码

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

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