[英]Replace black color in PDF file
我正在尝试替换 PDF 文件中的黑色(0,0,0)颜色(它不是带名称的专色,它是正常的填充颜色)但我还没有找到办法做到这一点,谁能帮忙我用这个?
附上PDF文件: https://gofile.io/d/AB1Bil
使用此答案中的PdfContentStreamEditor
,您可以替换在页面内容流中选择黑色 RGB 颜色的说明,如下所示:
float[] replacementColor = new float[] {.1f, .7f, .6f};
PDDocument document = ...;
for (PDPage page : document.getDocumentCatalog().getPages()) {
PdfContentStreamEditor identity = new PdfContentStreamEditor(document, page) {
@Override
protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException {
String operatorString = operator.getName();
if (RGB_FILL_COLOR_OPERATORS.contains(operatorString))
{
if (operands.size() == 3) {
if (isApproximately(operands.get(0), 0) &&
isApproximately(operands.get(1), 0) &&
isApproximately(operands.get(2), 0)) {
for (int i = 0; i < replacementColor.length; i++) {
operands.set(i, new COSFloat(replacementColor[i]));
}
}
}
}
super.write(contentStreamWriter, operator, operands);
}
boolean isApproximately(COSBase number, float compare) {
return (number instanceof COSNumber) && Math.abs(((COSNumber)number).floatValue() - compare) < 1e-4;
}
final List<String> RGB_FILL_COLOR_OPERATORS = Arrays.asList("rg", "sc", "scn");
};
identity.processPage(page);
}
document.save("gridShapesModified-RGBBlackReplaced.pdf");
( EditPageContent测试testReplaceRGBBlackGridShapesModified
)
当然,对于您的示例 PDF ,可以简单地获取内容 stream 并将0 0 0 rg
替换为.1.7.6 rg
以获得相同的效果。 但总的来说,情况可能会更复杂,所以我提出了这种方法。
但请注意:
sc
和scn
操作都一味地对待,就好像它们设置了 RGB colors。 事实上,他们也可以参考 Lab colors 和(对于scn
)Pattern, Separation, DeviceN 和 ICCBased colors。 严格来说应该在这里测试当前的非描边色彩空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.