PDF文件中的水印隐藏在图像后面 [英] Watermark in PDF file is hiding behind images

查看:1233
本文介绍了PDF文件中的水印隐藏在图像后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下内容为现有PDF添加水印:

I want to add a watermark to an existing PDF by using the following:

ITextSharp将文本插入现有的pdf

第3个答案正在运行但是如果PDF包含一个图像,然后水印隐藏在它后面。

The 3rd answer is working but if the PDF contains an image then the watermark is hidden behind it.

推荐答案

对于这样的问题,请咨询 StackOverflow上最好的iText问题。本书捆绑了之前在StackOverflow上发布和回答的数百个问题,包括我们封闭的问题跟踪器的一些答案。这是一个在以前没有在StackOverflow上发布的答案:

For questions like this, please consult The Best iText Questions on StackOverflow. This book bundles hundreds of questions previously posted and answered on StackOverflow, including some answers from our closed issue tracker. This is such an answer that wasn't published on StackOverflow before:

如果您的PDF中有不透明的形状(例如图像,还有彩色形状),则需要在现有内容之上添加水印:

If you have opaque shapes in your PDF (such as images, but also colored shapes), you need to add the Watermark on top of the existing content:

PdfContentByte canvas = pdfStamper.getOverContent(i);

现在文本将覆盖图像,但它可能隐藏一些重要信息。如果你想避免这种情况,你需要引入透明度。

Now the text will cover the images, but it may hide some important information. If you want to avoid this, you need to introduce transparency.

我写了一个简单的例子来说明这是如何完成的。它被称为 TransparentWatermark
让我们来看看结果:

I have written a simple example that shows how this is done. It is called TransparentWatermark Let's take a look at the result:

首先,我在现有内容下添加在现有内容下添加此水印文本。
隐藏部分文本(正如您在问题中指出的那样)。
然后,我在现有内容的基础上添加文本此水印已添加到现有内容的顶部。这可能就足够了,除非您担心通过覆盖现有内容会丢失一些重要信息。
在这种情况下,请看看我如何添加文本此透明水印已添加到现有内容之上:

First I add the text "This watermark is added UNDER the existing content" under the existing content. Part of the text is hidden (as you indicate in your question). Then I add the text "This watermark is added ON TOP OF the existing content" on top of the existing content. This may be sufficient, unless you fear that some crucial information will get lost by covering the existing content. In that case, take a look at how I add the text "This TRANSPARENT watermark is added ON TOP OF the existing content":

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    int n = reader.getNumberOfPages();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfContentByte under = stamper.getUnderContent(1);
    Font f = new Font(FontFamily.HELVETICA, 15);
    Phrase p = new Phrase(
        "This watermark is added UNDER the existing content", f);
    ColumnText.showTextAligned(under, Element.ALIGN_CENTER, p, 297, 550, 0);
    PdfContentByte over = stamper.getOverContent(1);
    p = new Phrase("This watermark is added ON TOP OF the existing content", f);
    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 500, 0);
    p = new Phrase(
        "This TRANSPARENT watermark is added ON TOP OF the existing content", f);
    over.saveState();
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.5f);
    over.setGState(gs1);
    ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 450, 0);
    over.restoreState();
    stamper.close();
    reader.close();
}

一些额外的提示和技巧:

Some extra tips and tricks:


  • 更改时始终使用 saveState() restoreState()图形状态。如果不这样做,您可能会受到不良影响,例如受您所做更改影响的其他内容(例如,您不希望所有内容变得透明)。

  • 默认渲染模式的文本是填充,因此我更改了填充不透明度。

  • 在这种情况下,我定义了填充不透明度为50%(0.5f)。如果要更改文本的透明度,请选择介于0.0f和1.0f之间的任何值。

  • Always use saveState() and restoreState() when you change the graphics state. If you don't you may get undesirable effects such as other content that is affected by the changes you make (e.g. you don't want all the content to become transparent).
  • The default rendering mode of text is "fill", hence I change the fill opacity.
  • In this case, I defined a fill opacity of 50% (0.5f). Choose any value between 0.0f and 1.0f if you want to change the transparency of the text.

这篇关于PDF文件中的水印隐藏在图像后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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