itextsharp中的水印未正确显示 [英] Watermark in itextsharp is not displaying properly

查看:581
本文介绍了itextsharp中的水印未正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp将数据填充到PDF模板,这是在OpenOffice中创建的。它填写得很好,我得到了正确的PDF,水印显示不正确。

I'm using iTextSharp to populate the data to PDF Templates, which is created in OpenOffice. it populating fine, I'm getting proper PDF, Watermark is not displaying properly.

下面是我的代码:

public void addWaterMark(PdfStamper stamper, int pageNumber, Watermark watermark)
    {
        List<WatermarkField> watermarkFields = watermark.getWatermarkFieldAsReference();
        for (WatermarkField watermarkField : watermarkFields) {
            // setting font and font size for the watermark text
            Font FONT = new Font(FontFamily.HELVETICA, watermarkField.getFontSize(), Font.BOLD, new GrayColor(0.75f));
            // setting alignment for the watermark
            ColumnText.showTextAligned(stamper.getUnderContent(pageNumber), Element.ALIGN_CENTER, new Phrase(watermarkField.getText(), FONT), watermarkField.getXDirection(), watermarkField.getYDirection(), watermarkField.getRotation());
        }

    } 

当我输入背景文字时 - 盒子正在隐藏水印。

当我放入前景水印时隐藏文字。

两个屏幕短裤都附在下方。

When i putting in background text-boxes are hiding the watermark.
When i putting in foreground watermark is hiding the text.
Both screen shorts are attaching below.

请告诉我解决方案。
谢谢。

Please Suggest me the solution. Thanks.

推荐答案

这个答案已经有一些关于如何添加水印的想法(在内容中,在内容中,位于内容之下和位图之前的内容中。,,,)。

In this answer there already are some ideas on how to add watermarks (in the under content, in the over content, both in the under content and in the over content before bitmaps.,,,).

可以生成缺少的用例,即过度内容中的透明标记像这样:

The missing use case, a transparent mark in the over content, can be generated like this:

void addSimpleTransparentPatternToOverContent(File source, File target) throws IOException, DocumentException
{
    PdfReader reader = new PdfReader(source.getPath());
    OutputStream os = new FileOutputStream(target);
    PdfStamper stamper = new PdfStamper(reader, os);

    PdfPatternPainter painter = stamper.getOverContent(1).createPattern(200, 150);
    painter.setColorFill(BaseColor.GREEN);
    painter.beginText();
    painter.setTextMatrix(AffineTransform.getTranslateInstance(0, 50));
    painter.setFontAndSize(BaseFont.createFont(), 100);
    painter.showText("Test");
    painter.endText();

    PdfGState state = new PdfGState();
    state.setFillOpacity(0.5f);

    for (int i = reader.getNumberOfPages(); i > 0; i--)
    {
        PdfContentByte overContent = stamper.getOverContent(i);

        overContent.setGState(state);
        overContent.setColorFill(new PatternColor(painter));
        overContent.rectangle(200, 300, 200, 150);
        overContent.fill();
    }

    stamper.close();
    os.close();
}

根据其他答案的精神,使用模式生成标记以防止出现在复制和粘贴输出中的水印。

In the spirit of that other answer the mark is generated using a pattern to prevent the watermark from appearing in copy&paste outputs.

您可以通过在 state.setFillOpacity(0.5f)中选择不同的值来更改透明度/不透明度)

这篇关于itextsharp中的水印未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆