iText 套件下載位址:
http://sourceforge.net/projects/itext/files/
<< 範例程式 >>
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class PdfWatermarkUtil {
/**
* Add text water mark
*
* @param inputStream - Input file path of the original pdf file.
* @param outputStream - Output file path of the target pdf file that water
* mark is added.
* @param watermark - Text of water mark.
* @throws DocumentException
* @throws IOException
*/
public void addITextWatermark(String inputFilePath, String outputFilePath, String watermark) throws Exception {
FileInputStream inputStream = new FileInputStream(inputFilePath);
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
this.addITextWatermark(inputStream, outputStream, watermark);
}
/**
* Add text water mark
*
* @param inputStream - InputStream of the original pdf file.
* @param outputStream - OutputStream of the target pdf file that water mark
* is added.
* @param watermark - Text of water mark.
* @throws DocumentException
* @throws IOException
*/
public void addITextWatermark(InputStream inputStream, OutputStream outputStream, String watermark)
throws Exception {
Document document = new Document(PageSize.A4);
//Read the existing PDF document
PdfReader pdfReader = new PdfReader(inputStream);
//Get the PdfStamper object
PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
//Get the PdfContentByte type by pdfStamper.
for (int i = 1, pdfPageSize = pdfReader.getNumberOfPages() + 1; i < pdfPageSize; i++) {
PdfContentByte pageContent = pdfStamper.getOverContent(i);
pageContent.setGState(this.getPdfGState());
pageContent.beginText();
pageContent.setFontAndSize(this.getBaseFont(), 20);
pageContent.setColorFill(BaseColor.LIGHT_GRAY);
pageContent.showTextAligned(Element.ALIGN_CENTER, watermark, document.getPageSize().getWidth() / 2,
document.getPageSize().getHeight() / 2, 0);
pageContent.endText();
}
pdfStamper.close();
}
/**
* Get BaseFont
*
* @return
* @throws Exception
*/
private BaseFont getBaseFont() throws Exception {
return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
}
/**
* Get PdfGState
*
* @return
*/
private PdfGState getPdfGState() {
PdfGState graphicState = new PdfGState();
graphicState.setFillOpacity(0.7f);
graphicState.setStrokeOpacity(1f);
return graphicState;
}
}
<<注意事項>>
PdfContentByte pageContent = pdfStamper.getOverContent(i);
代表變數 pageContent 所加入的浮水印會在 PDF 內容最上層
若改變為 PdfContentByte pageContent = pdfStamper.getUnderContent(i);
則代表變數 pageContent 所加入的浮水印會在 PDF 內容最下層
此差別在於,當我們針對由 Power Point (*.ppt) 轉出來的 PDF 加浮水印時,若使用 pdfStamper.getUnderContent(i) 會導致加入的浮水印被內容蓋掉而看不到浮水印,因此要改用 pdfStamper.getOverContent(i)。
然而,對於*.doc 及*.xls 目前樣本數不夠多,尚未發現這樣的問題。
沒有留言:
張貼留言