简体   繁体   English

替换 PDF 文件中的黑色

[英]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:但请注意:

  • The content stream editor only manipulates the immediate page content stream.内容 stream 编辑器仅操作即时页面内容 stream。 But colors can also be set and used in XObjects and patterns used from there.但是 colors 也可以在 XObjects 和从那里使用的模式中设置和使用。 So strictly speaking one has to descent into the XObjects and patterns in the page resources.所以严格来说,必须深入到页面资源中的 XObjects 和模式。
  • I blindly treat all three-parameter sc and scn operation as if they set RGB colors.我将所有三参数scscn操作都一味地对待,就好像它们设置了 RGB colors。 In truth they can also refer to Lab colors and (for scn ) to Pattern, Separation, DeviceN and ICCBased colors.事实上,他们也可以参考 Lab colors 和(对于scn )Pattern, Separation, DeviceN 和 ICCBased colors。 Strictly speaking on should test the current non-stroking colorspace here.严格来说应该在这里测试当前的非描边色彩空间。
  • I completely ignore that content added over other content with interesting blend modes may result in a displayed color differing from the current fill color.我完全忽略了使用有趣的混合模式添加到其他内容上的内容可能会导致显示的颜色与当前的填充颜色不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM