iText背景不透明度

时间:2019-03-05 15:10:04

标签: opacity itext7

我想使用iText 7在现有文本上覆盖半透明背景的文本。设置text元素的背景不透明度似乎不起作用(第1行),我只能对其进行设置整个paragraph(第2行):

enter image description here

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);
        }
    }    
}

我如何覆盖具有第2行所示的半透明背景的文本,而只覆盖覆盖的文本,而不是整个段落?所需的输出: enter image description here

1 个答案:

答案 0 :(得分: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的请求请求(尽管请遵循contribution guidelines)。储存库位于https://github.com/itext/itext7