itextsharp中的水印未正确显示

时间:2014-06-27 05:01:02

标签: java pdf itextsharp itext

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

下面是我的代码:

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

    } 

当我放入背景文本框时隐藏了水印 当我放入前景水印时隐藏文本。
两个屏幕短裤都附在下面。

enter image description here

enter image description here

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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分:0)

这是实际代码。

public void addWaterMark(PdfStamper stamper, int pageNumber, Watermark watermark){
    List<WatermarkField> watermarkFields = watermark.getWatermarkFieldAsReference();
    PdfReader reader = stamper.getReader();
    Rectangle pageSize = reader.getPageSize(1);
    BaseFont font = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

    PdfPatternPainter painter = stamper.getOverContent(1).createPattern(pageSize.getWidth(), pageSize.getHeight());
    painter.setColorStroke(new BaseColor(192, 192, 192));
    painter.setLineDash(0.4f, 0.4f, 0.2f);
    painter.beginText();
    painter.setTextMatrix(AffineTransform.getTranslateInstance(0, 50));
    for (WatermarkField watermarkField : watermarkFields) {
        painter.setFontAndSize(font, watermarkField.getFontSize());
        painter.showTextAlignedKerned(Element.ALIGN_MIDDLE, watermarkField.getText(), watermarkField.getXDirection(), watermarkField.getYDirection(),
        watermarkField.getRotation());
    }
    painter.endText();
    PdfGState state = new PdfGState();
    state.setFillOpacity(0.2f);
    PdfContentByte overContent = stamper.getOverContent(pageNumber);
    overContent.setGState(state);
    overContent.setColorFill(new PatternColor(painter));
    overContent.rectangle(pageSize.getLeft(), pageSize.getBottom(), pageSize.getWidth(), pageSize.getHeight());
    overContent.fill();
}