如何用itext对网站生成的pdf文件每页加水印

如何用itext对网站生成的pdf文件每页加水印,第1张

具体是这样的,我们现在在开发一个项目,其中有个功能是将用户填写的报表在线生成pdf下载,是通过流直接输出到用户的电脑里,服务器上不会存在生成的pdf文件,但是现在有一个问题,因为pdf文件页数是不确定的,所以在每一页加水印现在无法实现,请问怎样弄才能实现这一功能?我之前的思路是这样的,在生成pdf的java代码最后加个循环,通过得到document的页数然后用pdfContentByte在每一页加水印,但是我没找到可以定位到第几页的方法。部分代码:Java coderesponse.setContentType("text/htmlcharset=GBK")

response.setContentType("application/pdf")

response.setHeader("Content-Disposition", "attachmentfilename="

+ fileName + ".pdf")

OutputStream outs = response.getOutputStream()// 获取输出流

PdfWriter writer = PdfWriter.getInstance(doc, outs)

document.open()

PdfContentByte under = writer.getDirectContentUnder()

Java code//添加水印

under.beginText()

under.setColorFill(BaseColor.LIGHT_GRAY)

under.setFontAndSize(bfTitle, 100)

under.setTextMatrix(70, 0)

int rise = 200

for (int k = 0k <waterMarkName.length()k++) {

under.setTextRise(rise)

char c = waterMarkName.charAt(k)

under.showText(c + " ")

rise += 100

}

under.endText()

document.close()//关闭

import com.lowagie.text.pdf.PdfStamper

iTextSharp的确好用,但我加水印一般在初始化的时候写两行就行了,不需要这么麻烦,只是图片得自己做淡一些

if(System.IO.File.Exists("./waterMark/waterMark.JPG") == false)

MessageBox.Show("waterMark加载失败,请确定文件waterMark.JPG放在waterMark文件夹下!")

else

{

Watermark watermark = new Watermark(iTextSharp.text.Image.getInstance("./waterMark/waterMark.JPG"), wm_left, wm_top)

document.Add(watermark)

}

wm_left, wm_top是水印的位置,从第几页开始显示水印就把上面的代码加到第几页

补充:watermark是iTextSharp里的,不需要其他引用

iTextSharp.text.watermark 检查是否输入正确

.\itextsharp-0.04-src\iTextSharp\text\watermark.cs 。。。。。。

或者你可以搜索一下?

代码全文:

using System

/*

* $Id: Watermark.cs,v 1.3 2003/05/15 01:49:58 geraldhenson Exp $

* $Name: $

*

* Copyright 2000, 2001, 2002 by Bruno Lowagie.

*

* The contents of this file are subject to the Mozilla Public License Version 1.1

* (the "License")you may not use this file except in compliance with the License.

* You may obtain a copy of the License at http://www.mozilla.org/MPL/

*

* Software distributed under the License is distributed on an "AS IS" basis,

* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License

* for the specific language governing rights and limitations under the License.

*

* The Original Code is 'iText, a free JAVA-PDF library'.

*

* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by

* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.

* All Rights Reserved.

* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer

* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.

*

* Contributor(s): all the names of the contributors are added in the source code

* where applicable.

*

* Alternatively, the contents of this file may be used under the terms of the

* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the

* provisions of LGPL are applicable instead of those above. If you wish to

* allow use of your version of this file only under the terms of the LGPL

* License and not to allow others to use your version of this file under

* the MPL, indicate your decision by deleting the provisions above and

* replace them with the notice and other provisions required by the LGPL.

* If you do not delete the provisions above, a recipient may use your version

* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.

*

* This library is free softwareyou can redistribute it and/or modify it

* under the terms of the MPL as stated above or under the terms of the GNU

* Library General Public License as published by the Free Software Foundation

* either version 2 of the License, or any later version.

*

* This library is distributed in the hope that it will be useful, but WITHOUT

* ANY WARRANTYwithout even the implied warranty of MERCHANTABILITY or FITNESS

* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more

* details.

*

* If you didn't download this code from the following link, you should check if

* you aren't using an obsolete version:

* http://www.lowagie.com/iText/

*/

namespace iTextSharp.text {

/// <summary>

/// A <CODE>Watermark</CODE>is a graphic element (GIF or JPEG)

/// that is shown on a certain position on each page.

/// </summary>

/// <seealso cref="T:iTextSharp.text.Element"/>

/// <seealso cref="T:iTextSharp.text.Jpef"/>

/// <seealso cref="T:iTextSharp.text.Gif"/>

/// <seealso cref="T:iTextSharp.text.Png"/>

public class Watermark : Image, IElement {

// membervariables

/// <summary>This is the offset in x-direction of the Watermark. </summary>

private float offsetX = 0

/// <summary>This is the offset in y-direction of the Watermark. </summary>

private float offsetY = 0

// Constructors

/// <summary>

/// Constructs a <CODE>Watermark</CODE>-object, using an <CODE>Image</CODE>.

/// </summary>

/// <param name="image">an <CODE>Image</CODE>-object</param>

/// <param name="offsetX">the offset in x-direction</param>

/// <param name="offsetY">the offset in y-direction</param>

public Watermark(Image image, float offsetX, float offsetY) : base(image) {

this.offsetX = offsetX

this.offsetY = offsetY

}

// implementation of the Element interface

/// <summary>

/// Gets the type of the text element.

/// </summary>

/// <value>a type</value>

public override int Type {

get {

return type

}

}

// methods to retrieve information

/// <summary>

/// Returns the offset in x direction.

/// </summary>

/// <value>a value</value>

public float OffsetX {

get {

return offsetX

}

}

/// <summary>

/// Returns the offset in y direction.

/// </summary>

/// <value>an offset</value>

public float OffsetY {

get {

return offsetY

}

}

}

}

恩,给你发过去了,带有word教程和示例代码,国庆快乐


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11943005.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存