⑴ 使用iText给pdf文件添加水印不能正常显示
首先需要的jar包为iText-2.1.2u.jar、iTextAsian.jar。
import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class TestWaterPrint {
public static void main(String[]args) throws DocumentException, IOException{
//要输出的旁伍pdf文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/abc.pdf")));
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//将pdf文件先加水印然后输出
setWatermark(bos,"E:/pdf源文件.pdf",format.format(cal.getTime()) + " 下载使用人:" + "测试user", 16);
}
public static void setWatermark(BufferedOutputStream bos, String input,
String waterMarkName, int permission)
throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);//在内容上方加水印
//content = stamper.getUnderContent(i);//在内容下方加水印
gs.setFillOpacity(0.2f);
/枝迅/ content.setGState(gs);
content.beginText();
content.setColorFill(Color.LIGHT_GRAY);
content.setFontAndSize(base, 50);
content.setTextMatrix(70, 200);
content.showTextAligned(Element.ALIGN_CENTER, "公司内部文件,请注意保密猛启此!", 300,
350, 55);
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 8);
content.showTextAligned(Element.ALIGN_CENTER, "下载时间:"
+ waterMarkName + "", 300, 10, 0);
content.endText();
}
stamper.close();
}
}
⑵ 虚心求教,向PDF文件里面加水印问题,做过的请进来看看
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 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 software; you 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 WARRANTY; without 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教程和示例代码,国庆快乐
⑶ 怎么用itext对网站生成的pdf文件每页加水印
具体是这样的,我们现在在开发一个项目,其中有个功能是将用户填写的报表在线生成pdf下载,是通过流直接输出到用户的电脑里,服务器上不会存在生成的pdf文件,但是现在有一个问题,因为pdf文伍盯物件页数是不确定的,所以在每一页则喊加水印现在无法实现,请问怎样弄才能实现这一功能?我之前的思路是这腔液样的,在生成pdf的java代码最后加个循环,通过得到document的页数然后用pdfContentByte在每一页加水印,但是我没找到可以定位到第几页的方法。部分代码:Javacoderesponse.setContentType( ext/html;charset=GBK);response.setContentType(application/pdf);response.setHeader(Content-Disposition
⑷ java pdf itext 水印 怎么做到 汉字水印 平铺或者换行 不想每页只插入一行水印
// 仅设置一个图片实例对象,整个PDF文档只应用一个图片对象,极大减少因为增加图片水印导致PDF文档大小增加
Image waterMarkImage = null ; //这里是图片对象,
if (waterMarkImage == null) {
waterMarkImage = Image.getInstance(path);//path 根据自己的实际情况填写。
}
// 添加水印图片,文档正文内容采用横向三列,竖向两列模式增加图片水印
for(int i = 0 ; i < 100; i ++){
for(int j = 0 ; j < 4 ; j ++ ){
waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage,pageWidth * 0.3f * j , pageHeight * 0.05f * i));
}
}
public static Image getWaterMarkImage(Image waterMarkImage,
float xPosition, float yPosition) {
waterMarkImage.setAbsolutePosition(xPosition, yPosition);// 坐标
//waterMarkImage.setRotation(-20);// 旋转 弧度
//waterMarkImage.setRotationDegrees(-30);// 旋转 角度
waterMarkImage.scalePercent(100);// 依照比例缩放
return waterMarkImage;
}
⑸ PDF文件添加二维码水印
maven配置iText的jar,正燃碧主要不是所有私服都有iText的jar,maven仓库没有举举的,可以去https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.5.12 这里下载
同样先写个工具类,这里是加文字水印和图片水印的
【拓展功能】
ok,这只是基本功能,然后要对其进行拓展
业务场景:要在上传的pdf文件自动加上二维码水印,用户可以扫描二维码获取对应数据
首先二维码里面其实也就是一些数据,比如一个链接,或者一堆文字等等,这里可以通过Google开源的zxing库来事项生成二维码图片,然后附加到图片,形成水印
maven配置zxing对应jar:
写个工具类用于生成二维码图片:
对于上传的文件,我们怎么知道类型?如果用Spring提供的MultipartFile,这里可以获取ContentType来判断,这里只提供思路
有了工具类之后,我们需要段迟获取文件上传的inputStream
⑹ itext给pdf文件添加居中水印
public class StampStationery {
public static final String filename = "F://demo.pdf";
public static void main(String[] args) throws IOException,
DocumentException{
Document document = new Document(new Rectangle(400, 400));
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
String content = "hello word!";
cb.beginText();
Font f = new Font();
f.setSize(18);
Phrase p = new Phrase(content, f);
//(100,200) 是x,y坐标蔽猛仔 45 是偏移角度 字知亏体的演示自己设宏汪置
ColumnText.showTextAligned(cb, 0, p, 100, 200, 45);
cb.endText();
document.close();
}
}