[英]Replace black color in PDF file
I'm trying to replace the black (0,0,0) color in a PDF file (it's not a spot color with name, it's normal fill color) but I could not find a way to do that yet, could anyone please help me with this?我正在尝试替换 PDF 文件中的黑色(0,0,0)颜色(它不是带名称的专色,它是正常的填充颜色)但我还没有找到办法做到这一点,谁能帮忙我用这个?
Attached the PDF file: https://gofile.io/d/AB1Bil附上PDF文件: https://gofile.io/d/AB1Bil
Making use of the PdfContentStreamEditor
from this answer , you can replace the instructions selecting a black'ish RGB color in the page content streams like this:使用此答案中的
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 test testReplaceRGBBlackGridShapesModified
) ( EditPageContent测试
testReplaceRGBBlackGridShapesModified
)
Of course, for your example PDF one could have simply taken the content stream and replaced 0 0 0 rg
by .1.7.6 rg
for the same effect.当然,对于您的示例 PDF ,可以简单地获取内容 stream 并将
0 0 0 rg
替换为.1.7.6 rg
以获得相同的效果。 In general, though, the situation can be more complicated, so I propose this approach.但总的来说,情况可能会更复杂,所以我提出了这种方法。
Beware, though:但请注意:
sc
and scn
operation as if they set RGB colors.sc
和scn
操作都一味地对待,就好像它们设置了 RGB colors。 In truth they can also refer to Lab colors and (for scn
) to Pattern, Separation, DeviceN and ICCBased colors.scn
)Pattern, Separation, DeviceN 和 ICCBased colors。 Strictly speaking on should test the current non-stroking colorspace here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.