opacity - iText 背景不透明度
问题描述
我想使用 iText 7 在现有文本上覆盖具有半透明背景的文本。为text
元素设置背景不透明度似乎不起作用(第 1 行),我只能为整个设置它paragraph
(第 2 行):
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import java.io.IOException;
public class TextBackgroundOpacityTest {
public static void main(String[] args) throws IOException {
try (Document doc = new Document( new PdfDocument(new PdfWriter("TextBackgroundOpacityTest.pdf")))) {
doc.add(new Paragraph(new String(new char[130]).replace("\0", "A")));
// opacity doesn't work for text element
doc.showTextAligned(new Paragraph(new Text("missing background transparency").setBackgroundColor(ColorConstants.WHITE, .8f)), 500, 805, 0, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
// opacity for the whole paragraph works, but this is not what I want
doc.showTextAligned(new Paragraph("whole pharagraph background transparancy").setBackgroundColor(ColorConstants.WHITE, .8f), 500, 785, 0, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
}
}
}
解决方案
要解决该解决方案,您可以使用自定义渲染器。如果您查看在BlockRenderer#drawBackground
将透明背景设置为段落的情况下调用的内容,您可以在那里看到以下几行:
TransparentColor backgroundColor = new TransparentColor(background.getColor(), background.getOpacity());
drawContext.getCanvas().saveState().setFillColor(backgroundColor.getColor());
backgroundColor.applyFillTransparency(drawContext.getCanvas());
TextRenderer
但是,它有自己的实现,并且不尊重透明背景。但是我们可以自定义渲染器的实现。我们需要从当前TextRenderer
实现中复制粘贴相当多的代码,但好消息是我们不需要更改很多代码。只需在正确的位置插入两行:
TransparentColor backgroundColor = new TransparentColor(background.getColor(), background.getOpacity());
backgroundColor.applyFillTransparency(drawContext.getCanvas());
总的来说,我们得到以下实现:
private static class TextRendererWithBackgroundOpacity extends TextRenderer {
public TextRendererWithBackgroundOpacity(Text textElement) {
super(textElement);
}
@Override
public void drawBackground(DrawContext drawContext) {
Background background = this.<Background>getProperty(Property.BACKGROUND);
Float textRise = this.getPropertyAsFloat(Property.TEXT_RISE);
Rectangle bBox = getOccupiedAreaBBox();
Rectangle backgroundArea = applyMargins(bBox, false);
float bottomBBoxY = backgroundArea.getY();
float leftBBoxX = backgroundArea.getX();
if (background != null) {
boolean isTagged = drawContext.isTaggingEnabled();
PdfCanvas canvas = drawContext.getCanvas();
if (isTagged) {
canvas.openTag(new CanvasArtifact());
}
boolean backgroundAreaIsClipped = clipBackgroundArea(drawContext, backgroundArea);
canvas.saveState().setFillColor(background.getColor());
TransparentColor backgroundColor = new TransparentColor(background.getColor(), background.getOpacity());
backgroundColor.applyFillTransparency(drawContext.getCanvas());
canvas.rectangle(leftBBoxX - background.getExtraLeft(), bottomBBoxY + (float) textRise - background.getExtraBottom(),
backgroundArea.getWidth() + background.getExtraLeft() + background.getExtraRight(),
backgroundArea.getHeight() - (float) textRise + background.getExtraTop() + background.getExtraBottom());
canvas.fill().restoreState();
if (backgroundAreaIsClipped) {
drawContext.getCanvas().restoreState();
}
if (isTagged) {
canvas.closeTag();
}
}
}
@Override
public IRenderer getNextRenderer() {
return new TextRendererWithBackgroundOpacity((Text)modelElement);
}
}
要使Text
元素使用自定义渲染器实现,只需调用setNextRenderer
方法:
Text customTextElement = new Text("missing background transparency");
customTextElement.setNextRenderer(new TextRendererWithBackgroundOpacity(customTextElement));
顺便说一句,非常欢迎您将修复作为拉取请求提交给 iText(不过请遵循贡献指南)。存储库位于https://github.com/itext/itext7
推荐阅读
- arduino - 如何在 ESP8266 中切换云和本地网络之间的连接
- javascript - 在 Ace Editor 中突出显示单词
- airflow - 为什么在 Apache Airflow 中使用 CustomOperator 而不是 PythonOperator?
- c++ - 发送一组自定义数据。我发送的和我收到的不一样
- machine-learning - 是否有任何模型/分类器最适合此类基于 NLP 的项目?
- python - 如何解决错误 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 55: invalid continuation byte
- python - Word Mover 的距离与余弦相似度
- git - 从上游 11-base-blog 获取最新更新到我的分叉博客的推荐方法是什么?
- c# - Google API 身份验证服务器端
- python - 删除列表列表中的所有重复项(不包括原始项)