Java iText:为页面上的图像设置图像插值
我想遍历PDF的页面并编写一个新的PDF,其中所有图像的插值都设置为false。我希望能够做如下的事情,但我找不到一种方法来访问PDF页面上的图像或矩形Java iText:为页面上的图像设置图像插值,java,pdf-generation,itext,interpolation,Java,Pdf Generation,Itext,Interpolation,我想遍历PDF的页面并编写一个新的PDF,其中所有图像的插值都设置为false。我希望能够做如下的事情,但我找不到一种方法来访问PDF页面上的图像或矩形 PdfCopy copy = new PdfCopy(document, new FileOutputStream(outFileName)); copy.newPage(); PdfReader reader = new PdfReader(inFileName); for(int i = 1; i <= reader.getNumbe
PdfCopy copy = new PdfCopy(document, new FileOutputStream(outFileName));
copy.newPage();
PdfReader reader = new PdfReader(inFileName);
for(int i = 1; i <= reader.getNumberOfPages(); i++) {
PdfImportedPage importedPage = copy.getImportedPage(reader, i);
for(Image image : importedPage.images())
image.isInterpolated(false);
copy.addPage(importedPage);
}
reader.close();
PdfCopy copy=newpdfcopy(文档,新文件输出流(outFileName));
copy.newPage();
PdfReader reader=新PdfReader(填充名);
对于(int i=1;i这不会那么容易。没有高级方法可以实现您想要的功能。您必须枚举查找XObject图像的资源,并清除它们的/Interpolate标志
您必须在创建PDFIImportedPage之前执行此操作,因为没有公共方式访问他们的资源。Grr
void removeInterpolation( int pageNum ) {
PdfDictionary page = someReader.getPageN(pageNum);
PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
enumResources(resources);
}
void enumResource( PdfDictionary resources) {
PdfDictionary xobjs = resources.getAsDict(PdfName.XOBJECTS);
Set<PdfName> xobjNames = xobjs.getKeys();
for (PdfName name : xobjNames) {
PdfStream xobjStream = xobjs.getAsStream(name);
if (PdfName.FORM.equals( xobjStream.getAsName(PdfName.SUBTYPE))) {
// xobject forms have their own nested resources.
PdfDictionary nestedResources = xobjStream.getAsDict(PdfName.RESOURCES);
enumResources(nestedResources);
} else {
xobjStream.remove(PdfName.INTERPOLATE);
}
}
}
void removeInterpolation(int pageNum){
PdfDictionary page=someReader.getPageN(pageNum);
PdfDictionary resources=page.getAsDict(PdfName.resources);
资源(资源);
}
无效枚举资源(PdfDictionary资源){
PdfDictionary xobjs=resources.getAsDict(PdfName.XOBJECTS);
设置xobjNames=xobjs.getKeys();
用于(PdfName名称:XOBJNAME){
PdfStream xobjStream=xobjs.getAsStream(名称);
if(PdfName.FORM.equals(xobjStream.getAsName(PdfName.SUBTYPE))){
//xobject表单有自己的嵌套资源。
PdfDictionary nestedResources=xobjStream.getAsDict(PdfName.RESOURCES);
枚举资源(嵌套资源);
}否则{
xobjStream.remove(PdfName.INTERPOLATE);
}
}
}
上面的代码中跳过了很多空检查。一个页面不必有资源字典,尽管它们几乎总是这样。XObject表单也是如此。如果给定的键丢失或类型不同,所有getAs*
函数都将返回空值……你明白了