谁能帮我注解的再详细点 求高手
@SuppressWarnings("static-access")
public static void addStamp(String pdfPath,String sPdfPath, String projectID, String type, String abs,String stampStr) {
PdfReader reader = null;
PdfStamper stamp = null;
try {
reader = new PdfReader(pdfPath); // ("h:\\ITextTest.pdf",
// "World".getBytes());,后面的表示含有密码
stamp = new PdfStamper(reader, new FileOutputStream(sPdfPath));
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// Font fontChinese=new Font(bfChinese,12,Font.NORMAL,BaseColor.RED);
int total = reader.getNumberOfPages();
float pageWidth =0;
float pageHeight=0;
PdfContentByte under=null;
int fontSize=11;
itext给pdf文件添加居中水印
答案:2 悬赏:30 手机版
解决时间 2021-12-22 20:46
- 提问者网友:孤凫
- 2021-12-22 11:25
最佳答案
- 五星知识达人网友:渊鱼
- 2021-12-22 13:00
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();
}
}
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();
}
}
全部回答
- 1楼网友:痴妹与他
- 2021-12-22 14:37
首先需要的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();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯