package com.jll.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
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;
import com.itextpdf.text.pdf.PdfWriter;
/**
* 第一个PDF文件只能输入英文
* @author jiang.lili
*给文档设置背景颜色,边框,外边距
*/
public class MyFirstPDF {
public void create1(){
//第一步创建文档实例
Document document = new Document(PageSize.A4);
try {
//第二步新建一个输出流的文档
PdfWriter.getInstance(document, new FileOutputStream("d:/create1.pdf"));
//打开文档
document.open();
//向文档添加Meta信息
document.addAuthor("jiang.lili Sun");
document.addCreator("jiang.lili Sun");
document.addTitle("jiang.lili的技术博客");
document.addSubject("技术博客");
document.addCreationDate();
document.addKeywords("开源技术,企业架构,集群,负载均衡,分布式,J2EE,Java,SSH");
// 添加Header信息
document.addHeader("blog", "http://www.micmiu.com");
document.addHeader("twitter", "@suncto");
document.addHeader("weibo", "http://weibo.com/ctosun");
document.addHeader("mail", "sjsky007@gmail.com");
// 第四步:添加内容
document.add(new Paragraph("Hello this is my first iText"));
document.add(Chunk.NEWLINE);
// 添加 中文信息
BaseFont bf = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
Font f = new Font (bf,12,Font.BOLD,BaseColor.BLUE);
document.add(new Paragraph("这是中文,欢迎来到IText",f));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
// 第五步:关闭文档
document.close();
}
}
public void create2(){
System.out.println("My first PdfTable");
//定义一个A4大小的矩形组件
Rectangle rect = new Rectangle(PageSize.A4);
//设置背景颜色为浅灰色
rect.setBackgroundColor(BaseColor.LIGHT_GRAY);
//设置border类型为box(四周都有)
rect.setBorder(Rectangle.BOX);
//设置border颜色为深灰色
rect.setBorderColor(BaseColor.DARK_GRAY);
//设置border的宽度为5
rect.setBorderWidth(5);
//生成一个Document 对象的实例创建一个文档,将rect作为预设的样式传入后面的10,10,10,10是文档的外边距
Document document = new Document(rect,10,10,10,10);
try {
//新建一个输出流到文档
PdfWriter.getInstance(document, new FileOutputStream("d:/create2.pdf"));
//打开文档
document.open();
//向文档输入内容
document.add(new Paragraph("Hello,world!!!"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
finally{
//一定要加上,不然文档不会生成.
document.close();
}
}
public void create3(){
//页面大小
Rectangle rect = new Rectangle(PageSize.B5.rotate());
//页面背景色
rect.setBackgroundColor(BaseColor.ORANGE);
Document doc = new Document(rect);
try {
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("d:/create3.pdf"));
//pdf版本(默认1.4)
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
//文档属性
doc.addTitle("Title@sample");
doc.addAuthor("Author@rensanning");
doc.addSubject("Subject@iText sample");
doc.addKeywords("Keywords@iText");
doc.addCreator("Creator@iText");
//页边的空白
doc.setMargins(10, 20, 30, 40);
doc.open();
doc.add(new Paragraph("jiang.lili"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}finally{
//一定要加上,不然文档不会生成.
doc.close();
}
}
public void create4(){
Document document = new Document();
BaseFont bf =null;
Font fontChinese=null;
//使用宋休
try {
//创建一个简体中文的基本字体,UniGB-UCS2-H简体中文
bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
fontChinese= new Font(bf,12,Font.NORMAL);
//文字大小,与显示方式
PdfWriter pdf = PdfWriter.getInstance(document, new FileOutputStream("d:/create4.pdf"));
/**
* 设置阅读器的参数
* PdfWriter.PageModeUseThumbs:显示缩略图
* PdfWriter.PageLayoutTwoColumnLeft:双列显示,奇数页在左
* PdfWriter.HideMenubar:隐藏阅读程序的菜单
*/
pdf.setViewerPreferences(PdfWriter.PageModeUseThumbs|
PdfWriter.PageLayoutTwoColumnLeft|
PdfWriter.HideMenubar);
/**
* 设置文档的密码和权限
* 第一个参数设置userPassword为123
* 第二个参数设置ownerPassword为1234
* 第三个参数设置user的权限可以复制,可以打印
* 第四个参数设置加密类型
*
*/
/*pdf.setEncryption(new byte[]{'1','2','3'}, new byte[]{'1','2','3','4'},
PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_PRINTING,PdfWriter.STANDARD_ENCRYPTION_40);
//设置密码不能运行
pdf.setEncryption("hello".getBytes(), "world".getBytes(), PdfWriter.ALLOW_SCREENREADERS,PdfWriter.STANDARD_ENCRYPTION_128);
*/
document.open();
//添加page
document.add(new Paragraph("第一页",fontChinese));
//显示版本号
document.add(new Paragraph(Document.getVersion()));
document.newPage();
document.add(new Paragraph("第二页",fontChinese));
pdf.setPageEmpty(false);
document.newPage();
document.add(new Paragraph("第三页",fontChinese));
document.add(new Paragraph(Document.getProduct()));
document.newPage();
document.add(new Paragraph("第四页",fontChinese));
document.add(new Paragraph(Document.getRelease()));
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
document.close();
}
}
/**
* 添加水印
* 给pdf文件添加水印
* @param InPdfFile 要加水印的原pdf文件路径
* @param outPdfFile 加了水印后要输出的路径
* @param markImagePath 水印图片路径
* @param pageSize 原pdf文件的总页数
* (该方法是我当初将数据导入excel中然后再转换成pdf所以我这里的值是用excel的行数计算出来的,
* 如果不是我这种可以 直接用reader.getNumberOfPages()获取pdf的总页数)
*/
public void create5(String InPdfFile, String outPdfFile, String markImagePath){
PdfStamper stamp = null;
PdfReader reader = null;
try {
reader = new PdfReader( InPdfFile, "PDF".getBytes());
stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));
//给所有的页面都添加相同的图片
for(int i = 1; i <= reader.getNumberOfPages(); i++) {
//背景图
Image imgs = Image.getInstance("WebContent/image/8.jpg");
imgs.setAbsolutePosition(0, 0);
PdfContentByte under2 = stamp.getUnderContent(i);//只给 第三章加背景图
under2.addImage(imgs);
//文字水印
PdfContentByte over = stamp.getOverContent(i);//给第二章显示文字水印
over.beginText();
//BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,BaseFont.WINANSI,BaseFont.EMBEDDED);
BaseFont bf = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
over.setFontAndSize(bf, 18);
over.setTextMatrix(30,30);
//230:X轴 ,650:Y轴, 0:倾斜角度
over.showTextAligned(Element.ALIGN_LEFT, "阳光", 230, 650, 0);
over.setColorFill(BaseColor.CYAN);
over.endText();
//图片水印
Image img = Image.getInstance(markImagePath);
img.scaleAbsolute(1440, 900); //图片的大小
img.setAbsolutePosition(100, 200);//坐标
img.setRotation(-20);//旋转 弧度
img.setRotationDegrees(-45);//旋转 角度
img.scalePercent(10);//依照比例缩放
PdfContentByte under = stamp.getUnderContent(i);
under.addImage(img);
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.1f);// 设置透明度为0.1
under.setGState(gs);
under.beginText();
}
File tempfile = new File(outPdfFile);
if(tempfile.exists()) {
tempfile.delete();
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally{
try {
stamp.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyFirstPDF pdf = new MyFirstPDF();
pdf.create1();
pdf.create2();
pdf.create3();
pdf.create4();
pdf.create5("d:/create4.pdf","d:/create5.pdf","WebContent/image/girl.jpg");
}
}
最近下载更多
最近浏览更多
1358849392 LV21
2022年11月23日
akbar2020 LV9
2022年9月4日
是你爸爸啊100 LV5
2022年8月29日
crosa_Don LV18
2022年6月7日
heifenglei LV7
2022年4月7日
nickshen111 LV8
2021年12月13日
来恬爸爸晋亚阳 LV3
2021年10月29日
疯狂的巨兔12138 LV4
2021年5月14日
阿玉之父 LV1
2021年4月22日
一字清华 LV8
2021年2月21日

