旋转PDFContentByte覆盖内容

时间:2016-11-10 06:50:26

标签: java itext

我创建了一个垂直居中的水印文字。

这是我使用的行

PdfPatternPainter.showTextAlignedKerned(Element.ALIGN_MIDDLE,    string,x,y,-90);

我现在要做的是使水印对角线。改变角度值将使其成为对角线,但随后它的x,y位置不再居中。

这是我目前的方法

public static void createWaterMarkPDF(ArrayList<String> watermark, PdfReader reader, PdfStamper stamper) throws Exception {
   Rectangle pageSize = reader.getPageSize(1);
    final float WATERMARK_PAGE_ANGLE = 270;

    BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

    PdfPatternPainter painter = stamper.getOverContent(1).createPattern(pageSize.getWidth(), pageSize.getHeight());
    painter.setColorStroke(new BaseColor(192, 192, 192));

    int FONT_SIZE = 80;
    painter.beginText();
    painter.setTextRenderingMode(PdfPatternPainter.TEXT_RENDER_MODE_FILL);
    painter.setFontAndSize(font, FONT_SIZE);

    for (int i=0; i<watermark.size(); i++) {
        String string = watermark.get(i);
        // Values are opposite since we are doing a vertical alignment angle
        float width = font.getAscentPoint(string, FONT_SIZE) + font.getDescentPoint(string, FONT_SIZE);
        float height = painter.getEffectiveStringWidth(string, true);

        float x = (pageSize.getWidth() - width) / 2;

        if (i == 0)
            x += (width * 3);
        else if (i == 2)
            x -= (width * 3);

        painter.showTextAlignedKerned(Element.ALIGN_MIDDLE,
            string,  x, 
            Util.transformY(pageSize, (pageSize.getHeight() - height) / 2),
            WATERMARK_PAGE_ANGLE);

    }

    painter.endText();

    // Transparency of watermark
    PdfGState state = new PdfGState();
    state.setFillOpacity(0.1f);

    for (int i=reader.getNumberOfPages(); i>0; i--) {
        Rectangle thisPageSize = reader.getPageSize(i);
        PdfContentByte overContent = stamper.getOverContent(i);
        overContent.setColorFill(new PatternColor(painter));
        overContent.setGState(state);
        overContent.rectangle(thisPageSize.getLeft(), thisPageSize.getBottom(), thisPageSize.getWidth(), thisPageSize.getHeight());
        overContent.fill();
    }
}

它绘制垂直线并居中。 watermark是一个ArrayList。你可以添加3个字符串。

如果我将角度更改为300(或-60)以使其成为对角线,则定位会受到影响。我需要在这里使用AffineTransform吗?虽然我已经尝试过,除非我在这里缺少什么,否则对我没有用?

0 个答案:

没有答案