如何使用pdfbox(或其他开源Java库)从PDF文件中提取颜色配置文件
加载文档后:如何使用pdfbox(或其他开源Java库)从PDF文件中提取颜色配置文件,java,pdf,pdfbox,Java,Pdf,Pdfbox,加载文档后: public static void main(String[] args) throws IOException { PDDocument doc = PDDocument.load(new File("blah.pdf")); 如何从PDM文档中获得逐页打印的颜色意图?我读了文档,没有看到覆盖范围。我读了关于“如何创建/添加意图到PDF文件”的示例。我找不到关于“如何获得意图”的例子。使用API/示例,我编写了以下(未测试的代码)来获取每个意图的对象。看看这对你是否有
public static void main(String[] args) throws IOException {
PDDocument doc = PDDocument.load(new File("blah.pdf"));
如何从PDM文档中获得逐页打印的颜色意图?我读了文档,没有看到覆盖范围。我读了关于“如何创建/添加意图到PDF文件”的示例。我找不到关于“如何获得意图”的例子。使用API/示例,我编写了以下(未测试的代码)来获取每个意图的对象。看看这对你是否有用
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);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
PDDocument doc=PDDocument.load(新文件(“blah.pdf”);
PDDocumentCatalog cat=doc.getDocumentCatalog();
List=cat.getOutputinents();
用于(PDOutputE:列表){
p(“找到的PDOutputIntent:”);
p(“Info=“+e.getInfo());
p(“OutputCondition=“+e.getOutputCondition());
p(“OutputConditionIdentifier=“+e.getOutputConditionIdentifier());
p(“RegistryName=“+e.getRegistryName());
costream cstr=e.GetDestOutputinent();
}
静态空隙p(字符串s){
系统输出打印项次;
}
}
我不知道这段代码是否有用,在搜索了下面的链接后
我找到一些代码,检查是否有用
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);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
PDDocument doc=PDDocument.load(新文件(“blah.pdf”);
PDDocumentCatalog cat=doc.getDocumentCatalog();
List=cat.getOutputinents();
PDDocumentCatalog cat=doc.getDocumentCatalog();
COSArray COSArray=doc.getCOSObject();
PDICCBased pdCS=新的PDICCBased(cosArray);
pdCS.getNumberOfComponents()
静态空隙p(字符串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();
PDDocument doc=PDDocument.load(新文件(“XXXXX.pdf”);
对于(PDOutputIntent oi:doc.getDocumentCatalog().getOutputinents())
{
costream destOutputIntent=oi.getDestOutputIntent();
String info=oi.getOutputCondition();
if(info==null | | info.isEmpty())
{
info=oi.getInfo();
}
InputStream is=destOutputinent.createInputStream();
FileOutputStream fos=新的FileOutputStream(info+“.icc”);
IOUtils.副本(is、fos);
fos.close();
is.close();
}
对于(int p=0;p
这没有什么作用(但如果需要,我可以添加一些):
- 着色、图案、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);
}
}
for(inti=1;i1)你能分享这个PDF吗?2) 您是否知道并非所有PDF都有icc颜色配置文件?3) 你需要很多文件还是只需要一个?4) 请使用您的文件尝试PDFDebugger。谢谢您的回复。1) 任何PDF(2)不,我还没有尝试对颜色配置文件进行groc。现在的问题更一般了3)任何文件,我都会尝试从每页或整个文档(取决于可能的情况)中提取颜色配置文件4)尝试通过编程实现这一点。如果这是一个单文件解决方案,我只会使用UI工具。我相信版本与@stevedbrown不同,我得到了流的句柄,但在试图将流的内容解释为“ICC配置文件”时,时间用完了。你得到你想要的了吗?如果您的问题没有解决,请告知我们您的问题。在这里,我们将尽最大努力(单独或一起)获得完整的图片。在代码中留下未使用的静态空白p会让人觉得这很像blackpen的答案。PDDocumentCatalog cat=doc.getDocumentCatalog();COSArray COSArray=doc.getCOSObject();PDICCBased pdCS=新的PDICCBased(cosArray)代码>复制了他的代码并添加了这3行;)@Hemakumar“复制了他的代码并添加了这3行;)”我认为这是剽窃,你们不应该复制和粘贴其他答案的代码。你真可耻。@Marcs你将如何修复别人代码中的错误。。。。没有使用他们的代码。。。?我没有复制和粘贴相同的答案。。。我修改了代码,我觉得需要做一些修改。问题需要正确的解决方案,我试着给出我的观点,就是这样。谢谢你为我感到羞耻。我对此不担心。:)@Hemakumar您将如何修复其他人代码中的错误。。。你在答案a上发表评论