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