多行水印重叠问题

时间:2016-01-26 17:07:31

标签: java itext watermark

我正在尝试开发一种支持i18n和多线水印的水印压模。我可以创建它,但问题是水印与前一行的文本重叠,如下图所示。enter image description here

我的水印文字包含以下内容:123\n456\n789。我愿意做的是设置水印而不重叠线条。我怎样才能做到这一点?以下是我的代码的一部分:

WaterMarkStamper.java

public void insertWaterMark() throws Exception {
    final File pdfFile = this.getFile();
    if (pdfFile != null && pdfFile.exists()) {
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            reader = new PdfReader(pdfFile.getAbsolutePath());
            final int n = reader.getNumberOfPages();
            temp = this.getNewFile();
            // Create a stamper that will copy the document to a new file
            stamp = new PdfStamper(reader, new FileOutputStream(temp));
            PdfContentByte over;
            int pageIndex = 1;
            while (pageIndex <= n) {
                over = stamp.getOverContent(pageIndex);
                this.addWatermark(wmVO, reader, over, pageIndex, position);
                pageIndex++;
            }
        } catch (final Exception e) {
            WaterMarkStamper.logger.error(e.getMessage(), e);
        } finally {
            if (stamp != null) {
                stamp.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
}

private void addWatermark(final WaterMarkVO wmVo, final PdfReader reader, final PdfContentByte contentByte, final Integer pageIndex) throws Exception {
    final Rectangle page = reader.getPageSizeWithRotation(pageIndex);

    // This is where the magic happens
    final Image img = wmVo.getImage(contentByte);

    // Get margins
    final int leftMargin = wmVo.getLeftMargin();
    final int topMargin = wmVo.getTopMargin();
    final int rightMargin = wmVo.getRightMargin();
    final int bottomMargin = wmVo.getBottomMargin();

    // Absolute position
    final Point pt = this.getImageInsertionPoint(img, page, leftMargin, topMargin, rightMargin, bottomMargin);
    img.setAbsolutePosition((float) pt.getX(), (float) pt.getY());

    // Add image
    contentByte.addImage(img);
}

WaterMarkVO.java

public Image getImage(final PdfContentByte contentByte) throws Exception {
    final Paragraph paragraph = this.getParagraph();
    final Rectangle paragraphRectangle = this.getParagraphRectangle();
    final float paragraphHeight = paragraphRectangle.getHeight();
    final float paragraphWidth = paragraphRectangle.getWidth();

    final PdfTemplate xObject = contentByte.createTemplate(paragraphWidth, paragraphHeight + this.getFontSize());
    final ColumnText column = new ColumnText(xObject);
    column.setSimpleColumn(0, 0, paragraphWidth, paragraphHeight);
    column.setExtraParagraphSpace(0f);
    column.addElement(paragraph);
    column.go();

    final Image img = Image.getInstance(xObject);
    final int rotation = this.getRotation();
    img.setRotationDegrees(rotation);
    return img;
}

public Paragraph getParagraph() throws Exception {
    final FontSelector fontSelector = this.getFontSelector();
    final String text = "123\n456\n789";
    this.fontSelectorPhrase = fontSelector.process(text);
    final Paragraph paragraph = new Paragraph(this.fontSelectorPhrase);

    return paragraph;
}

public Rectangle getParagraphRectangle() throws Exception {
    final String text = "123\n456\n789";
    final float fontSize = this.getFontSize();
    float paragraphWidth = 0f;
    float paragraphHeight = 0f;
    float leading = 0f;
    final String[] lines = text.split("\n");

    List<Chunk> chunks = this.fontSelectorPhrase.getChunks();
    for(Chunk c : chunks) {
        int indexer = 0;
        final Paragraph p = new Paragraph(" ", c.getFont());
        do {
            final float currentLineWidth = c.getFont().getBaseFont().getWidthPoint(" " + lines[indexer] + " ", fontSize);
            final float currentLineHeight = c.getFont().getBaseFont().getAscentPoint(lines[indexer], fontSize) + c.getFont().getBaseFont().getDescentPoint(lines[indexer], fontSize);
            final float curentLineLeading = p.getLeading();

            paragraphWidth = currentLineWidth > paragraphWidth ? currentLineWidth : paragraphWidth;
            paragraphHeight = currentLineHeight > paragraphHeight ? currentLineHeight : paragraphHeight;
            leading = currentLineLeading > leading ? currentLineLeading : leading; 
            indexer++;
        } while (indexer < lines.length);
    }

    paragraphHeight += leading / lines.length;

    return new Rectangle(paragraphWidth, paragraphHeight);
}

public FontSelector getFontSelector() throws Exception {
    // Adding fonts to support i18n
    FontSelector fontSelector = new FontSelector();
    fontSelector.addFont(new Font(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED))); // Does not support some turkish glyphs 
    fontSelector.addFont(new Font(BaseFont.createFont("helvetica.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED))); // Support some turkish glyphs
    return fontSelector;
}

显然,我隐藏了一些不必要的代码。在这个例子中,我没有在水印上使用特殊字符,但是我的代码必须遵守这个要求(我遵循p378中“iText in Action 2nd edition”中所述的内容)。

3 个答案:

答案 0 :(得分:0)

看起来Paragraph领先不足。设置与字体大小相同的前导或1.25以获得令人愉快的排版效果。

答案 1 :(得分:0)

您将currentLineHeight计算为AscentDescent总和

final float currentLineHeight = c.getFont().getBaseFont().getAscentPoint(lines[indexer], fontSize) + c.getFont().getBaseFont().getDescentPoint(lines[indexer], fontSize);

Descent不是正数,而是零甚至是负数,参见JavaDocs:

/**
 * Gets the descent of a <CODE>String</CODE> in points. The descent will always be
 * less than or equal to zero even if all the characters have an higher descent.
 * @param text the <CODE>String</CODE> to get the descent of
 * @param fontSize the size of the font
 * @return the descent in points
 */
public float getDescentPoint(String text, float fontSize)

因此,您很可能需要差异(或绝对值的总和):

final float currentLineHeight = c.getFont().getBaseFont().getAscentPoint(lines[indexer], fontSize) - c.getFont().getBaseFont().getDescentPoint(lines[indexer], fontSize);

(这至少会正确计算矩形,特别是当您开始使用具有相关下降的字符创建水印时。)

答案 2 :(得分:0)

基于mkl's answerPaulo's answer我可以注意到我的微积分上的一些错误。以下是现在为我工作的代码:

<强> WaterMarkVO.java

public Image getImage(final PdfContentByte contentByte) throws Exception {
    final Paragraph paragraph = this.getParagraph();
    final Rectangle paragraphRectangle = this.getParagraphRectangle();
    final float paragraphHeight = paragraphRectangle.getHeight();
    final float paragraphWidth = paragraphRectangle.getWidth();

    final PdfTemplate xObject = contentByte.createTemplate(paragraphWidth, paragraphHeight + this.getFontSize());
    final ColumnText column = new ColumnText(xObject);
    column.setSimpleColumn(0, 0, paragraphWidth, paragraphHeight);
    column.setExtraParagraphSpace(0f);
    column.addElement(paragraph);
    column.go();

    final Image img = Image.getInstance(xObject);
    final int rotation = this.getRotation();
    img.setRotationDegrees(rotation);
    return img;
}

public Rectangle getParagraphRectangle(final WatermarkPosition position, Paragraph para) throws Exception {
    int numberOfLines = 1;
    float paragraphWidth = 0f;
    float paragraphHeight = 0f;
    float leading = 0f;
    float chunkFontSize = 0f;
    float currentChunkWidth = 0f;
    float currentChunkHeight = 0f;
    Font f = null;
    BaseFont bf = null;
    String content = null;

    List<Chunk> chunks = this.fontSelectorPhrase.getChunks();
    for(Chunk c : chunks) {
        f = c.getFont();
        bf = f.getBaseFont();
        content = c.getContent();
        chunkFontSize = f.getSize();
        currentChunkWidth = bf.getWidthPoint(" " + content + " ", chunkFontSize);

        currentChunkHeight = chunkFontSize + bf.getAscentPoint(content, chunkFontSize) - bf.getDescentPoint(content, chunkFontSize);

        if(!c.getContent().contains(System.getProperty("line.separator"))) {
            paragraphWidth += currentChunkWidth > paragraphWidth ? currentChunkWidth : paragraphWidth;
        } else {
            paragraphWidth = currentChunkWidth;
            numberOfLines++;
        }

        paragraphHeight = currentChunkHeight > paragraphHeight ? currentChunkHeight : paragraphHeight;

        leading = chunkFontSize > leading ? chunkFontSize : leading; 
    }

    para.setLeading(leading);

    paragraphHeight += (leading * numberOfLines);

    return new Rectangle(paragraphWidth, paragraphHeight);
}