运用java操作PDF,生成PDF
来源:互联网 发布:手机微信监控软件 编辑:程序博客网 时间:2024/05/17 06:20
参考文档地址:
http://wenku.baidu.com/view/c3360006de80d4d8d15a4f6f.html
http://wenku.baidu.com/link?url=vwIcd482JeD1jp_lfNNX7Nc4BJ6rlOm41tX0gONl3x8lfOUW0s4t9GjPuXLuIS9b3G3Inb17MgiCqlt6wVAmZ28u6N0MIlPLtv-MSwbPNTe
http://wenku.baidu.com/link?url=vwIcd482JeD1jp_lfNNX7Nc4BJ6rlOm41tX0gONl3x8lfOUW0s4t9GjPuXLuIS9b3G3Inb17MgiCqlt6wVAmZ28u6N0MIlPLtv-MSwbPNTe
(1).第一步:建立一个document对象
Docuemnt document = new Document();
(2).第二步.创建一个Writer来监听document,并指向一个输出文件流,这里是PdfWriter
PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
(3).打开一个文档 document.open();
(4).我自己加了一个subject和title,结果发现没看见效果,郁闷!,原来他们的返回值都是boolean,所以只有在文件的文件属性中才能看到信息,还可以其他的信息
document.addTitle("title");
document.addSubject("subject"); (5).给docuemnt文档添加一个段落Paragraph内容
document.add(new Paragraph("this is a pdf text"));
(6).关闭文档
document.close();
完整程序如下:
public class PdfTest_1 {
public static void main(String[] args) {
System.out.println("this is the first process!");
Document docuemt = new Document();
try {
PdfWriter.getInstance(docuemt, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
docuemt.open();
docuemt.addTitle("title");
docuemt.addSubject("subject");
docuemt.add(new Paragraph("this is a pdf text"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println("end");
docuemt.close();
}
}
public static void main(String[] args) {
System.out.println("this is the first process!");
Document docuemt = new Document();
try {
PdfWriter.getInstance(docuemt, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
docuemt.open();
docuemt.addTitle("title");
docuemt.addSubject("subject");
docuemt.add(new Paragraph("this is a pdf text"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println("end");
docuemt.close();
}
}
2.根据文档学习内容如下:
package pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Annotation;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Header;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest {
public static void main(String[] args) throws Exception {
System.out.println("this is the PDF process!");
//设置文档大小格式
Rectangle pageSize = new Rectangle(114,720);
//设置背景颜色
pageSize.getBackgroundColor();//无效果
// pageSize.setBackgroundColor(BaseColor.RED);
//Document document = new Document(pageSize);
Document document = new Document(PageSize.A4);
//当创建一个文件时,定义上、下、左、右页边距:
//Document document = new Document(pageSize, 10, 10, 10, 10);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
//PdfWriter w = PdfWriter.getInstance(document, System.out); //在控制台输出信息---
//w.setCloseStream(false); //不能关闭
//增加文档的一些说明,返回值为boolean,在文档的文档属性中可以看到,这些摘要设置需要在文档打开之前设置
//但是为什么我在打开之后设置也可以?
// writer.CenterWindow = 1;
//// PdfWriter.NonFullScreenPageModeUseNone = 1;//既不显示大纲也不显示缩略图
// writer.NonFullScreenPageModeUseOC =1;
// writer.NonFullScreenPageModeUseOutlines = 1;//显示大纲
// writer.NonFullScreenPageModeUseThumbs = 1;//显示缩略图
// writer.FitWindow = ;
document.open();
Chunk chunk = new Chunk("this is a chunk",FontFactory.getFont(FontFactory.TIMES_BOLD, 20,Font.ITALIC ,BaseColor.RED));
Chunk chunk1 = new Chunk("this is a chunk1!",FontFactory.getFont(FontFactory.HELVETICA,20,Font.UNDEFINED,BaseColor.BLUE));
// chunk.setTextRise(2.00);
chunk.setBackground(BaseColor.BLUE);
chunk1.setBackground(BaseColor.PINK);
document.setPageCount(1);
document.getPageNumber();
document.addTitle("title");
document.addSubject("subject");
document.addAuthor("mushixing");
document.addCreationDate();
document.addCreator("mushixing");
document.addHeader("header","i am a header");
document.addKeywords("PDF,java,lixin");
document.addLanguage("chinese,english");
document.addProducer();
//创建一个锚点
Anchor anchor = new Anchor("baidu", FontFactory.getFont(FontFactory.TIMES_ROMAN, 30, Font.UNDERLINE, BaseColor.YELLOW));
anchor.setName("baidu");
anchor.setReference("www.baidu.com");
document.add(chunk);//文档中添加一个块
document.add(chunk1);//文档中添加另一个块
anchor.add(chunk);
document.add(anchor);
document.add(new Phrase(chunk1));//文档中添加一个短句
document.add(new Paragraph("this is a pdf text"));//文档中添加一个段落
document.add(new Paragraph(chunk));//文档中添加一个段落。段落中添加一个块
document.add(new Paragraph(new Phrase(chunk1)));//文档中添加一个段落,段落中添加一个短句,短句中添加一个块
//加入序列----这里有问题哦。
//List list = new List(true,20);//需要排序,以数字排序
List list = new List(false,20);//不需要排序,以"-"打头
//list.setListSymbol("*"); //设置排序标志
list.setListSymbol(new Chunk("**", FontFactory.getFont(FontFactory.SYMBOL ,15, Font.ITALIC, BaseColor.BLUE))); //设置标志为字符
list.setListSymbol(new Chunk(Image.getInstance("C:\\Users\\Administrator\\Desktop\\杂项\\star.jpg"), 0, 0));
list.add(new ListItem("this is the first list!"));
list.add(new ListItem("this is the second list!"));
list.add(new ListItem(new Chunk("this is a chunk list!")));
list.add(new ListItem(new Phrase("this is a Phrase list")));
System.out.println(list.getFirst());//getFirst---1
//System.out.println(list.getFirstItem());//getFirstItem----this is the first list!
//System.out.println(list.getChunks()); //得到了全部的内容
document.add(list);
//增加页眉,为什么没有效果!
Header header = new Header("head", "i am a header!");
document.add(header);
//增加章节和区域,为什么出现重复的section
// Chapter chapter = new Chapter("this is a paragraph", 1);
// Chapter chapter1 = new Chapter("this is title", 1);
// Chapter chapter2 = new Chapter(1);
Paragraph ptitle = new Paragraph("this is a paragraph",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
Chapter chapter3 = new Chapter(ptitle, 5);
Paragraph stitle = new Paragraph("this is a section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
Paragraph sstitle = new Paragraph("this is a stitle section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
//在章节中添加区域
//Section section = chapter3.addSection(ptitle);
Section section1 = chapter3.addSection(stitle);
Section section2 = section1.addSection(sstitle);
Section section3 = chapter3.addSection("this is a string section");
Section section4 = section3.addSection("this is a stitle stitle section");
document.add(section1);
document.add(section2);
document.add(section3);
document.add(section4);
// document.add(chapter);
// document.add(chapter1);
// document.add(chapter2);
//添加图形
//添加表格
PdfPTable table = new PdfPTable(6);
//如果不用PdfPCell的话,那么就只会有6个表格列,不会自动添加多余的值到下一行
// PdfPCell cell = new PdfPCell(new Paragraph("..........."));
PdfPCell cell = new PdfPCell();
System.out.println(cell.getBorderColor());
cell.setBackgroundColor(BaseColor.GRAY);
cell.setBorder(10);
cell.setBorderColor(BaseColor.RED);
cell.setBorderColorBottom(BaseColor.BLUE);
cell.setBorderColorLeft(BaseColor.GREEN);
cell.setBorderColorRight(BaseColor.PINK);
cell.setBorderColorTop(BaseColor.RED);
//cell.setColspan(2);//合并两列
cell.setPhrase(new Phrase("orgNo"));
table.addCell(cell);
table.addCell("string");
table.addCell(new Paragraph("paragraph"));
table.addCell(new Paragraph("1"));
table.setTotalWidth(400);
table.addCell("2");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.setHorizontalAlignment(10);
document.add(table);
//添加图片
Image image = Image.getInstance("C:\\Users\\Administrator\\Desktop\\beaute.jpg");
image.setAlignment(1);
document.add(image);
//加入注释
Annotation annotation = new Annotation("annotation", "this is a annotation!");
Annotation annotation1 = new Annotation(100f,700f,200f,800f,new URL("http://www.baidu.com"));
Annotation annotation2 = new Annotation(100f,700f,200f,800f,"http://www.baidu.com");
document.add(annotation);
document.add(annotation1);
document.add(annotation2);
//设置文档文件大小格式
/*document.setPageSize(PageSize._11X17);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize 11*17"));
document.setPageSize(PageSize.A0);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize A0"));
document.setPageSize(PageSize.A1);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize A1"));
document.setPageSize(PageSize.FLSA);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize flsa"));
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Annotation;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Header;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest {
public static void main(String[] args) throws Exception {
System.out.println("this is the PDF process!");
//设置文档大小格式
Rectangle pageSize = new Rectangle(114,720);
//设置背景颜色
pageSize.getBackgroundColor();//无效果
// pageSize.setBackgroundColor(BaseColor.RED);
//Document document = new Document(pageSize);
Document document = new Document(PageSize.A4);
//当创建一个文件时,定义上、下、左、右页边距:
//Document document = new Document(pageSize, 10, 10, 10, 10);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
//PdfWriter w = PdfWriter.getInstance(document, System.out); //在控制台输出信息---
//w.setCloseStream(false); //不能关闭
//增加文档的一些说明,返回值为boolean,在文档的文档属性中可以看到,这些摘要设置需要在文档打开之前设置
//但是为什么我在打开之后设置也可以?
// writer.CenterWindow = 1;
//// PdfWriter.NonFullScreenPageModeUseNone = 1;//既不显示大纲也不显示缩略图
// writer.NonFullScreenPageModeUseOC =1;
// writer.NonFullScreenPageModeUseOutlines = 1;//显示大纲
// writer.NonFullScreenPageModeUseThumbs = 1;//显示缩略图
// writer.FitWindow = ;
document.open();
Chunk chunk = new Chunk("this is a chunk",FontFactory.getFont(FontFactory.TIMES_BOLD, 20,Font.ITALIC ,BaseColor.RED));
Chunk chunk1 = new Chunk("this is a chunk1!",FontFactory.getFont(FontFactory.HELVETICA,20,Font.UNDEFINED,BaseColor.BLUE));
// chunk.setTextRise(2.00);
chunk.setBackground(BaseColor.BLUE);
chunk1.setBackground(BaseColor.PINK);
document.setPageCount(1);
document.getPageNumber();
document.addTitle("title");
document.addSubject("subject");
document.addAuthor("mushixing");
document.addCreationDate();
document.addCreator("mushixing");
document.addHeader("header","i am a header");
document.addKeywords("PDF,java,lixin");
document.addLanguage("chinese,english");
document.addProducer();
//创建一个锚点
Anchor anchor = new Anchor("baidu", FontFactory.getFont(FontFactory.TIMES_ROMAN, 30, Font.UNDERLINE, BaseColor.YELLOW));
anchor.setName("baidu");
anchor.setReference("www.baidu.com");
document.add(chunk);//文档中添加一个块
document.add(chunk1);//文档中添加另一个块
anchor.add(chunk);
document.add(anchor);
document.add(new Phrase(chunk1));//文档中添加一个短句
document.add(new Paragraph("this is a pdf text"));//文档中添加一个段落
document.add(new Paragraph(chunk));//文档中添加一个段落。段落中添加一个块
document.add(new Paragraph(new Phrase(chunk1)));//文档中添加一个段落,段落中添加一个短句,短句中添加一个块
//加入序列----这里有问题哦。
//List list = new List(true,20);//需要排序,以数字排序
List list = new List(false,20);//不需要排序,以"-"打头
//list.setListSymbol("*"); //设置排序标志
list.setListSymbol(new Chunk("**", FontFactory.getFont(FontFactory.SYMBOL ,15, Font.ITALIC, BaseColor.BLUE))); //设置标志为字符
list.setListSymbol(new Chunk(Image.getInstance("C:\\Users\\Administrator\\Desktop\\杂项\\star.jpg"), 0, 0));
list.add(new ListItem("this is the first list!"));
list.add(new ListItem("this is the second list!"));
list.add(new ListItem(new Chunk("this is a chunk list!")));
list.add(new ListItem(new Phrase("this is a Phrase list")));
System.out.println(list.getFirst());//getFirst---1
//System.out.println(list.getFirstItem());//getFirstItem----this is the first list!
//System.out.println(list.getChunks()); //得到了全部的内容
document.add(list);
//增加页眉,为什么没有效果!
Header header = new Header("head", "i am a header!");
document.add(header);
//增加章节和区域,为什么出现重复的section
// Chapter chapter = new Chapter("this is a paragraph", 1);
// Chapter chapter1 = new Chapter("this is title", 1);
// Chapter chapter2 = new Chapter(1);
Paragraph ptitle = new Paragraph("this is a paragraph",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
Chapter chapter3 = new Chapter(ptitle, 5);
Paragraph stitle = new Paragraph("this is a section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
Paragraph sstitle = new Paragraph("this is a stitle section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
//在章节中添加区域
//Section section = chapter3.addSection(ptitle);
Section section1 = chapter3.addSection(stitle);
Section section2 = section1.addSection(sstitle);
Section section3 = chapter3.addSection("this is a string section");
Section section4 = section3.addSection("this is a stitle stitle section");
document.add(section1);
document.add(section2);
document.add(section3);
document.add(section4);
// document.add(chapter);
// document.add(chapter1);
// document.add(chapter2);
//添加图形
//添加表格
PdfPTable table = new PdfPTable(6);
//如果不用PdfPCell的话,那么就只会有6个表格列,不会自动添加多余的值到下一行
// PdfPCell cell = new PdfPCell(new Paragraph("..........."));
PdfPCell cell = new PdfPCell();
System.out.println(cell.getBorderColor());
cell.setBackgroundColor(BaseColor.GRAY);
cell.setBorder(10);
cell.setBorderColor(BaseColor.RED);
cell.setBorderColorBottom(BaseColor.BLUE);
cell.setBorderColorLeft(BaseColor.GREEN);
cell.setBorderColorRight(BaseColor.PINK);
cell.setBorderColorTop(BaseColor.RED);
//cell.setColspan(2);//合并两列
cell.setPhrase(new Phrase("orgNo"));
table.addCell(cell);
table.addCell("string");
table.addCell(new Paragraph("paragraph"));
table.addCell(new Paragraph("1"));
table.setTotalWidth(400);
table.addCell("2");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.addCell("10");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.addCell("7");
table.addCell("8");
table.addCell("9");
table.setHorizontalAlignment(10);
document.add(table);
//添加图片
Image image = Image.getInstance("C:\\Users\\Administrator\\Desktop\\beaute.jpg");
image.setAlignment(1);
document.add(image);
//加入注释
Annotation annotation = new Annotation("annotation", "this is a annotation!");
Annotation annotation1 = new Annotation(100f,700f,200f,800f,new URL("http://www.baidu.com"));
Annotation annotation2 = new Annotation(100f,700f,200f,800f,"http://www.baidu.com");
document.add(annotation);
document.add(annotation1);
document.add(annotation2);
//设置文档文件大小格式
/*document.setPageSize(PageSize._11X17);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize 11*17"));
document.setPageSize(PageSize.A0);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize A0"));
document.setPageSize(PageSize.A1);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize A1"));
document.setPageSize(PageSize.FLSA);
document.newPage();
document.add(new Paragraph("this is a pdf pagesize flsa"));
/*//新建一个页面并设置格式和内容
//设置文档横向排列或者直接Document document = new Document(PageSize.A4.rotate());
document.setPageSize(PageSize.A4.rotate());
document.newPage();//新建一个页面
document.add(new Paragraph("this is a pdf text"));*/
/*
* 为什么出不来,请给我一个理由。。。。。。。。。。啊!啊!啊!。。。。。。。。。。
* */
// 创建中文字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UTF32-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
// 设置为中文
Paragraph pragraph=new Paragraph("你好,这是中文",FontChinese);
document.add(pragraph);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println("end");
document.close();
}
}
//这就是出来的结果
0 0
- 运用java操作PDF,生成PDF
- java读取生成PDF,iText操作PDF
- 运用Java组件itext生成pdf
- java生成和操作pdf表单
- java操作pdf生成图片水印
- 【JAVA生成PDF】利用模板生成PDF
- Itext操作pdf模板生成pdf
- java 生成 pdf excel
- java 生成pdf
- java生成PDF-jasperreport
- java PDF 生成方案
- JAVA生成PDF文件
- java 生成pdf缩略图
- java pdf生成图片
- java生成PDF
- java生成PDF文件
- java生成PDF
- java生成PDF文件
- APACHE 2.2.8+TOMCAT6.0.14配置负载均衡
- XTEMPLATE介绍
- POJ3616 Milking Time 【DP】
- 使用第三方框架的时候报“synthesize of ‘weak’ property is only allowed in ARC or GC mode”错
- Tomcat(免安装版)的安装与配置
- 运用java操作PDF,生成PDF
- weblogic.jdbc.extensions.PoolLimitSQLException
- 索尼爱立信系统越狱后的问题
- CentOS 6.5 配置IP地址的三种方法
- 链队的基本操作
- C语言Int类型和Double类型小端转大端
- 第十一周项目2调用gcd函数求四位数的最大公约数
- Oracle Applications DBA 基础(一)
- activeMQ发布订阅模式中中常用工具类