在项目中涉及到合同下载和说明书下载,合同下载是其他的同事交接到我手里面的活,之前也没做过合同相关的东西,光听就是多么高大上的东西,但是接触后你就知道 什么他么的玩意啊 ,这么简单。但是简单也是在使用外部jar包的情况下,那就是itext了。需要使用到的jar请见附件。
itext在使用方面确实挺好用的,不管是生成pdf或者是word文档,全能够实现(最起码我在项目中实现功能是完全可以了,还有就是我的这些个东西也是在参考其他文件的基础上完成的)
itext在生成pdf文件中,有两种实现机制:第一、可以通过模板类来完善pdf文档
第二、可以不使用模板自己生成pdf文档
第一,使用模板类完善pdf文档
PdfReader pdfTemplate = new PdfReader(inFile);--------------读取模板类流
FileOutputStream fileOutputStream = new FileOutputStream(outFile);----------输出指定文件的输出流
PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream); -------模板和最终结果结合
stamper.setFormFlattening(true);
//通过使用pdf阅读编辑软件操作pdf文件,给pdf文件赋予表单域,并给出属性名称,然后通过itext来找到相对应的属性名称,并且给这个属性名称代表的表单域进行赋值(推荐使用Adobe Acrobat软件来编辑pdf文件......)
stamper.getAcroFields().setField("contract_series","" );
stamper.getAcroFields().setField("bid_real_name", "");
stamper.getAcroFields().setField("bid_money", "");
stamper.close();
pdfTemplate.close();
第二、不使用模板生成pdf文件,直接生成
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("d:\\createSamplePDF.pdf"));
} catch (Exception e1) {
e1.printStackTrace();
}
doc.open();
RtfFont titleFont = new RtfFont("仿宋_GB2312", 20, Font.NORMAL,
Color.BLACK);
RtfFont contextFont = new RtfFont("仿宋_GB2312", 15, Font.NORMAL,
Color.BLACK);
try {
Table table = new Table(12, 16);
int[] withs = { 3, 9, 5, 4, 4, 3, 3, 14, 14, 14, 14, 14 };
/** 设置每列所占比例 author:yyli Sep 15, 2010 */
table.setWidths(withs);
/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
table.setWidth(100);
/** 居中显示 author:yyli Sep 15, 2010 */
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 author:yyli Sep 15, 2010 */
table.setAutoFillEmptyCells(true);
String titleString = "债权说明书";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
String contextString ="尊敬的冯长辉先生 ,您好!";
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
String contextString2 =" 货币单位:人民币(元)";
Paragraph context2 = new Paragraph(contextString2);
// 正文格式对齐方式
context2.setAlignment(Element.ALIGN_LEFT);
context2.setFont(contextFont);
// 与上一段落(标题)的行距
context2.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context2);
Cell[] cellHeaders = new Cell[11];
cellHeaders[0] = new Cell(new Phrase("序号", contextFont));
cellHeaders[1] = new Cell(new Phrase("课程名称", contextFont));
cellHeaders[2] = new Cell(new Phrase("教师", contextFont));
cellHeaders[3] = new Cell(new Phrase("学分", contextFont));
cellHeaders[4] = new Cell(new Phrase("上课周次", contextFont));
cellHeaders[5] = new Cell(new Phrase(" ", contextFont));
cellHeaders[5].setColspan(2);
cellHeaders[6] = new Cell(new Phrase("星期一", contextFont));
cellHeaders[7] = new Cell(new Phrase("星期二", contextFont));
cellHeaders[8] = new Cell(new Phrase("星期三", contextFont));
cellHeaders[9] = new Cell(new Phrase("星期四", contextFont));
cellHeaders[10] = new Cell(new Phrase("星期五", contextFont));
table.addCell(cellHeaders[0]);
table.addCell(cellHeaders[1]);
table.addCell(cellHeaders[2]);
table.addCell(cellHeaders[3]);
table.addCell(cellHeaders[4]);
table.addCell(cellHeaders[5]);
table.addCell(cellHeaders[6]);
table.addCell(cellHeaders[7]);
table.addCell(cellHeaders[8]);
table.addCell(cellHeaders[9]);
table.addCell(cellHeaders[10]);
doc.add(table);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------------------------使用itext生成word文档,请参考http://outofmemory.cn/code-snippet/1892/usage-iText-word-document-insert-complex-form
/******************************************word*********************************************************/
Document doc = new Document();
try {
RtfWriter2.getInstance(doc, new FileOutputStream("d:\\createSamplePDF.doc"));
} catch (Exception e1) {
e1.printStackTrace();
}
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// RtfWriter2.getInstance(doc, baos);
doc.open();
RtfFont titleFont = new RtfFont("仿宋_GB2312", 20, Font.NORMAL,
Color.BLACK);
RtfFont contextFont = new RtfFont("仿宋_GB2312", 15, Font.NORMAL,
Color.BLACK);
try {
Table table = new Table(12, 16);
int[] withs = { 3, 9, 5, 4, 4, 3, 3, 14, 14, 14, 14, 14 };
/** 设置每列所占比例 author:yyli Sep 15, 2010 */
table.setWidths(withs);
/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
table.setWidth(100);
/** 居中显示 author:yyli Sep 15, 2010 */
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 author:yyli Sep 15, 2010 */
table.setAutoFillEmptyCells(true);
String titleString = "债权说明书";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
String contextString ="尊敬的冯长辉先生 ,您好!";
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
String contextString2 =" 货币单位:人民币(元)";
Paragraph context2 = new Paragraph(contextString2);
// 正文格式对齐方式
context2.setAlignment(Element.ALIGN_LEFT);
context2.setFont(contextFont);
// 与上一段落(标题)的行距
context2.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context2);
Cell[] cellHeaders = new Cell[11];
cellHeaders[0] = new Cell(new Phrase("序号", contextFont));
cellHeaders[1] = new Cell(new Phrase("课程名称", contextFont));
cellHeaders[2] = new Cell(new Phrase("教师", contextFont));
cellHeaders[3] = new Cell(new Phrase("学分", contextFont));
cellHeaders[4] = new Cell(new Phrase("上课周次", contextFont));
cellHeaders[5] = new Cell(new Phrase(" ", contextFont));
cellHeaders[5].setColspan(2);
cellHeaders[6] = new Cell(new Phrase("星期一", contextFont));
cellHeaders[7] = new Cell(new Phrase("星期二", contextFont));
cellHeaders[8] = new Cell(new Phrase("星期三", contextFont));
cellHeaders[9] = new Cell(new Phrase("星期四", contextFont));
cellHeaders[10] = new Cell(new Phrase("星期五", contextFont));
table.addCell(cellHeaders[0]);
table.addCell(cellHeaders[1]);
table.addCell(cellHeaders[2]);
table.addCell(cellHeaders[3]);
table.addCell(cellHeaders[4]);
table.addCell(cellHeaders[5]);
table.addCell(cellHeaders[6]);
table.addCell(cellHeaders[7]);
table.addCell(cellHeaders[8]);
table.addCell(cellHeaders[9]);
table.addCell(cellHeaders[10]);
doc.add(table);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
参考文档:http://www.360doc.com/content/10/0424/17/633992_24681953.shtml