iText的showTextAligned⽅法
java使⽤itext的showTextAligned⽅法给pdf添加⽂字⽔印(watermark)
2013-07-22 17:39:59 来源:评论:点击:3582
ps透明背景java的开源pdf库-Itext可以给pdf添加⽔印,主要是使⽤showTextAligned这个⽅法.ShowTextAligned⽅法可以设置输出⽔印的⽂本和⽔印⽂本的旋转⾓度,ShowTextAligned⽅法具体的参数说明如下:
public void ShowTextAligned(int alignment, String text, float x,float y, float rotation)
参数参数说明:
alignment左、右、居中(ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT)
text要输出的⽂本
x⽂本输⼊的X坐标
y⽂本输⼊的Y坐标
手机锁屏
rotation⽂本的旋转⾓度
将itext-5.4.2.zip压缩包解压缩后得到7个⽂件:itextpdf-5.4.2.jar(核⼼组件)、itextpdf-5.4.2-javadoc.jar(API⽂档)、itextpdf-5.4.2-sources.jar(源代码)、itext-xtra-5.4.2.jar、itext-xtra-5.4.2-javadoc.jar、itext-xtra-5.4.2-sources.jar ,如果是输出中⽂pdf,需要⽤到itext-asian.jar这个jar包,这个jar包在extrajars-2.3.zip中,下载地址:
private static void addWatermark(PdfStamper pdfStamper
, String waterMarkName) {
PdfContentByte content = null;
BaFont ba = null;
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
try { // 设置字体
ba = ateFont("STSongStd-Light",
"UniGB-UCS2-H", BaFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ba == null || pdfStamper == null) { return; }
// 设置透明度为0.4
gs.tFillOpacity(0.4f);
gs.tStrokeOpacity(0.4f);
int toPage = Reader().getNumberOfPages();
for (int i = 1; i <= toPage; i++) {
pageRect = Reader(). getPageSizeWithRotation(i);
// 计算⽔印X,Y坐标
float x = Width() / 2;
float y = Height() / 2;
//获得PDF最顶层
content = OverContent(i);
content.saveState();
// t Transparency
content.tGState(gs);
简短心得大全
content.beginText();
content.tColorFill(BaColor.GRAY);
content.tFontAndSize(ba, 60);
// ⽔印⽂字成45度⾓倾斜
content.showTextAligned(Element.ALIGN_CENTER , waterMarkName, x, y, 45); dText(); }
} catch (Exception ex) {
ex.printStackTrace();
}
finally {
content = null;
ba = null;
pageRect = null;
}
}
java使⽤itext的showTextAligned⽅法给pdf添加⽂字⽔印的完整代码如下:
1 2 3 4 5 6 7 8 9package com.pdf;
import java.io.FileOutputStream; import java.io.IOException;
import BaColor; import Chunk; import Document;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74import DocumentException;
import Element;
import Font;
import PageSize;
import Paragraph;
import Rectangle;
import pdf.BaFont;
十驾
import pdf.PdfContentByte;
import pdf.PdfGState;
import pdf.PdfPCell;
import pdf.PdfPTable;
import pdf.PdfReader;
import pdf.PdfStamper;
import pdf.PdfWriter;
public class ItextHelloword {
public static void main(String[] args) throws Exception {
String pdfFilePath = "f:/itext-demo.pdf";
PdfReader pdfReader = new PdfReader("f:/itext-demo.pdf");
// Get the PdfStamper object
PdfStamper pdfStamper = new PdfStamper(pdfReader
, new FileOutputStream(
"f:/itext-demo22.pdf"));
addWatermark(pdfStamper, "");
pdfStamper.clo();
}
private static void addWatermark(PdfStamper pdfStamper , String waterMarkName) {
PdfContentByte content = null;
BaFont ba = null;
Rectangle pageRect = null;
活动设计方案PdfGState gs = new PdfGState();
try{
// 设置字体
ba = ateFont("STSongStd-Light", "UniGB-UCS2-H",
BaFont.NOT_EMBEDDED);
} catch(DocumentException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
try{
河南省考生服务平台if(ba == null|| pdfStamper == null) {
return;
}
// 设置透明度为0.4
gs.tFillOpacity(0.4f);
gs.tStrokeOpacity(0.4f);
int toPage = Reader().getNumberOfPages();
for(int i = 1; i <= toPage; i++) {
pageRect = Reader().
getPageSizeWithRotation(i);
// 计算⽔印X,Y坐标
float x = Width() / 2;
float y = Height() / 2;
//获得PDF最顶层
小学生环保作文content = OverContent(i);
content.saveState();
// t Transparency
content.tGState(gs);
民间二胡content.beginText();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
content.tColorFill(BaColor.GRAY);
content.tFontAndSize(ba, 60);
// ⽔印⽂字成45度⾓倾斜
content.showTextAligned(Element.ALIGN_CENTER
, waterMarkName, x,
y, 45);
}
} catch(Exception ex) {
ex.printStackTrace();
} finally{
content = null;
ba = null;
pageRect = null;
}
}
}
java使⽤itext的showTextAligned⽅法给pdf添加⽂字⽔印(watermark)