2016-11-10 53 views
15

一旦你载入一个文档中提取颜色配置文件:你如何从PDF文件使用PDFBOX(或其他开源Java LIB)

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

你如何通过页打印颜色意图从页面PDDocument?我阅读文档,没有看到报道。

+3

1)可以共享PDF的每一页中提取的个人资料? 2)你是否知道并非所有的PDF都有icc颜色配置文件? 3)你是否需要这个为许多文件或只为一个? 4)请用您的文件尝试PDFDebugger。 –

+0

感谢您的回复。 1)任何PDF 2)不,还没有试图通过颜色配置文件。问题是现在更一般 3)任何文件,我试图拉每页或整个文档(取决于可能的)颜色配置文件 4)试图以编程方式做到这一点。如果这是单个文件解决方案,我只需使用UI工具。 – stevedbrown

回答

5

这得到了输出意向(你会得到这些高品质的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(); 

什么这并未” (如果需要的话,但我可以添加一些吧)做T:阴影,图案,X对象的形式,外观流资源

  • 递归在科罗拉多州的

    • 的色彩空间rspaces喜欢的DeviceN和分离
    • 递归的方式,X对象的形式,软口罩
  • +0

    你碰巧知道是否有任何方法检测Pantone匹配? – stevedbrown

    +0

    @stevedbrown不知道这些。我只能告诉你哪里有icc配置文件,而不是它们的质量。 –

    2

    我阅读了“如何创建/添加内容到PDF文件”的例子。我无法获得“如何获得意图”的例子。使用API​​ /示例,我编写了以下(未经测试的代码)以获取每个Intents的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); 
        } 
    } 
    
    +0

    not'getOutputIntents()'它是'getOutputIntent()' – Hemakumar

    +0

    我相信区别在于版本[2.0.2](https://pdfbox.apache.org/docs/2.0.2/javadocs/org/apache/pdfbox /pdmodel/PDDocumentCatalog.html)与[1.8.11](http://pdfbox.apache.org/docs/1.8.11/javadocs/org/apache/pdfbox/pdmodel/PDDocumentCatalog。html) – blackpen

    +0

    @stevedbrown,我处理了流,但在尝试将流的内容解释为“ICC配置文件”时耗尽了时间。你有没有得到你想要的东西?如果你的问题没有解决,让我们知道你的问题。我们会尽力(单独或一起)获得完整的照片。 – blackpen

    1

    我不知道我是否该代码的帮助与否,搜索下面的链接后,

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

    在这段代码中留下未使用的静态void p使得这与Blackpen的答案非常相似。 – stevedbrown

    +0

    'PDDocumentCatalog cat = doc.getDocumentCatalog(); COSArray cosArray = doc.getCOSObject(); PDICCBased pdCS = new PDICCBased(cosArray);'复制了他的代码并添加了这3行;) – Hemakumar

    +0

    @Hemakumar _“复制了他的代码并添加了这3行;)”_ _我会称这种抄袭,而且你不应该复制并粘贴来自其他答案的代码。真丢脸。 – Marcs

    2

    使用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); 
        } 
    } 
    
    +0

    @TilmanHausherr他还把大括号的其他开源Java库放进去, –