java 能否用pdfbox把红色的戳记变成黑色
Can java use pdfbox to turn the red stamp into black
我试过用下面的代码来转换图章颜色,但是没有用
PDFDocumentSignature signature = signatures.get(i);
PDPageContentStream contents2 = new PDPageContentStream(pdDocument, pages,PDPageContentStream.AppendMode.APPEND, false, false);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
contents2.setGraphicsStateParameters(r01);
contents2.setNonStrokingColor(Color.DARK_GRAY);
contents2.addRect(signature.getX(), signature.getY(), signature.getHeight(), signature.getHeight());
contents2.fill();
contents2.close();`
正如问题评论中所说,所讨论的 red stamp 是一个签名表单字段小部件。
这解释了为什么 OP 的代码无法使标记去饱和:该代码使 静态页面内容 的一部分去饱和。但是,小部件(和一般的注释)不是静态页面内容的一部分,而是呈现在静态页面内容之上。
因此,我们必须操纵有问题的小部件注释或它上面的另一个注释的内容流。
您可以像这样操作签名外观:
PDDocument pdf = ...;
PDAcroForm acroForm = pdf.getDocumentCatalog().getAcroForm();
PDTerminalField acroField = (PDTerminalField) acroForm.getField("Signature1");
PDAnnotationWidget widget = acroField.getWidgets().get(0);
PDAppearanceStream appearance = widget.getAppearance().getNormalAppearance().getAppearanceStream();
byte[] originalBytes;
try ( InputStream oldContent = appearance.getContents() ) {
originalBytes = IOUtils.toByteArray(oldContent);
}
try ( PDPageContentStream canvas = new PDPageContentStream(pdf, appearance) ) {
canvas.appendRawCommands(originalBytes);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
canvas.setGraphicsStateParameters(r01);
canvas.setNonStrokingColor(Color.DARK_GRAY);
PDRectangle bbox = appearance.getBBox();
canvas.addRect(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getWidth(), bbox.getHeight());
canvas.fill();
}
pdf.save(RESULT);
(ChangeAppearance 测试 testRemoveAppearanceSaturation
)
before
after
我试过用下面的代码来转换图章颜色,但是没有用
PDFDocumentSignature signature = signatures.get(i);
PDPageContentStream contents2 = new PDPageContentStream(pdDocument, pages,PDPageContentStream.AppendMode.APPEND, false, false);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
contents2.setGraphicsStateParameters(r01);
contents2.setNonStrokingColor(Color.DARK_GRAY);
contents2.addRect(signature.getX(), signature.getY(), signature.getHeight(), signature.getHeight());
contents2.fill();
contents2.close();`
正如问题评论中所说,所讨论的 red stamp 是一个签名表单字段小部件。
这解释了为什么 OP 的代码无法使标记去饱和:该代码使 静态页面内容 的一部分去饱和。但是,小部件(和一般的注释)不是静态页面内容的一部分,而是呈现在静态页面内容之上。
因此,我们必须操纵有问题的小部件注释或它上面的另一个注释的内容流。
您可以像这样操作签名外观:
PDDocument pdf = ...;
PDAcroForm acroForm = pdf.getDocumentCatalog().getAcroForm();
PDTerminalField acroField = (PDTerminalField) acroForm.getField("Signature1");
PDAnnotationWidget widget = acroField.getWidgets().get(0);
PDAppearanceStream appearance = widget.getAppearance().getNormalAppearance().getAppearanceStream();
byte[] originalBytes;
try ( InputStream oldContent = appearance.getContents() ) {
originalBytes = IOUtils.toByteArray(oldContent);
}
try ( PDPageContentStream canvas = new PDPageContentStream(pdf, appearance) ) {
canvas.appendRawCommands(originalBytes);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
canvas.setGraphicsStateParameters(r01);
canvas.setNonStrokingColor(Color.DARK_GRAY);
PDRectangle bbox = appearance.getBBox();
canvas.addRect(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getWidth(), bbox.getHeight());
canvas.fill();
}
pdf.save(RESULT);
(ChangeAppearance 测试 testRemoveAppearanceSaturation
)
before | after |
---|---|