java用itext生成pdf.docx
- 文档编号:24470393
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:20
- 大小:18.38KB
java用itext生成pdf.docx
《java用itext生成pdf.docx》由会员分享,可在线阅读,更多相关《java用itext生成pdf.docx(20页珍藏版)》请在冰豆网上搜索。
java用itext生成pdf
java用itext生成pdf
一、初探itext生成pdf
用itext生成pdf文档,主要需要以下几步
1.创建document实例
Documentdocument=newDocument();
2.配置pdf的输入流,这时指明了pdf输入文件的位置。
PdfWriter.getInstance(document,newFileOutputStream("c:
\A.pdf"));
3.打开document
document.open();
4.把pdf需要显示的内容添加到pdf中,可以是文字,表格,图片等
document.add(newParagraph("HelloWorld"));
5.最后不要忘记close掉document
document.close();
具体代码如下:
importjava.io.FileOutputStream;importjava.io.IOException;importcom.lowagie.text.*;importcom.lowagie.text.pdf.PdfWriter;/***生成pdf*/publicclassHelloWorld{publicstaticvoidmain(String[]args){try{
//创建document实例 Documentdocument=newDocument();//配置pdf的输出流
PdfWriter.getInstance(document,newFileOutputStream("c:
\A.pdf"));//打开document document.open();//添加数据入pdf document.add(newParagraph("HelloWorld"));}catch(DocumentExceptionde){System.err.println(de.getMessage());}catch(IOExceptionioe){System.err.println(ioe.getMessage());}//关闭document document.close();}}
二、深入itext生成pdf
pdf显示的内容是丰富的,有文字,表格,图片等,如何做到这点,下面将一步步进行介绍:
1.页面的设置
通过Document对象的构造方法可以进行设置
Documentdocument=newDocument(RectanglepageSize,floatarg1,floatarg2,floatarg3,floatarg4);
pageSize设置纸张大小,背景等,具体查看Rectangle类
arg1左边距
arg2右边距
arg3上边距
arg4下边距
2.在pdf中添加丰富的文字。
com.lowagie.text.Paragraph类主要是对pdf的文本进行处理
这里主要介绍几个常用方法
com.lowagie.text.Font主要是处理字体的大小,加粗,斜体,颜色等。
3.在pdf中的表格处理
com.lowagie.text.Table类主要是对pdf的表格进行处理
4.在pdf中的图片的处理
com.lowagie.text.Image类主要是对pdf的图片进行处理
三、pdf的公共类
下面是生成pdf的公共类,希望能给大家一个参考
importjava.awt.Color;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importcom.lowagie.text.Cell;
importcom.lowagie.text.Document;
importcom.lowagie.text.DocumentException;
importcom.lowagie.text.Element;
importcom.lowagie.text.Font;
importcom.lowagie.text.FontFactory;
importcom.lowagie.text.PageSize;
importcom.lowagie.text.Paragraph;
importcom.lowagie.text.Phrase;
importcom.lowagie.text.Rectangle;
importcom.lowagie.text.Table;
importcom.lowagie.text.pdf.BaseFont;
importcom.lowagie.text.pdf.PdfWriter;
publicclassFeesPDFUtil{
/**
*PDF报表对象
*/
privateDocumentdocument;
/**
*定义段落Paragraph后的间距
*/
privatefloatspacingAfter=10f;
/**
*是否设置表格Cell边框
*/
privatebooleanisSetBorder=true;
/**
*定义段落Paragraph前的间距
*/
privatefloatspacingBefore=0f;
/**
*PDF报表中表格对象
*/
privateTabletable;
/**
*是否打开PDF,默认false
*/
privatebooleanisInitPdf=false;
/**
*报表的纸张大小,默认为A4
*/
privateRectanglepageSize=PageSize.A4;
publicFeesPDFUtil(StringpdfFileName,float[]margins)throwsFileNotFoundException,DocumentException{
document=newDocument(pageSize,margins[0],margins[1],margins[2],margins[3]);
PdfWriter.getInstance(document,newFileOutputStream(pdfFileName));
document.open();
isInitPdf=true;
}
/**
*设置table
*@paramcolWithstable每个列的宽度数组(百分比)
*@paramborderSize边框大小
*@paramwidth表格占可显示宽度的百分比
*@throwsException
*/
publicTableinitTable(int[]colWiths,intborderSize,intwidth)throwsException{
table=newTable(colWiths.length);
table.setWidths(colWiths);
table.setBorder(borderSize);
table.setWidth(width);
returntable;
}
/**
*中文字体
*@paramsize字体大小
*@return
*@throwsException
*/
publicFontgetChineseFont(Floatsize)throwsException{
BaseFontbfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.EMBEDDED);
FontfontChinese=newFont(bfChinese,size,Font.NORMAL);
returnfontChinese;
}
/**
*标题中文字体(加粗)
*@paramsize字体大小
*@return
*@throwsException
*/
publicFontgetTitleChineseFont(Floatsize)throwsException{
BaseFontbfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.EMBEDDED);
FontfontChinese=newFont(bfChinese,size,Font.NORMAL);
fontChinese.setStyle(Font.BOLD);
returnfontChinese;
}
/**
*功能描述:
获取普通字体(缺省字体为COURIER)
*@paramnormalFontName普通字体名,可以使用以下参数
*COURIER,COURIER_BOLD,COURIER_OBLIQUE,COURIER_BOLDOBLIQUE,
*HELVETICA,HELVETICA_BOLD,HELVETICA_OBLIQUE,HELVETICA_BOLDOBLIQUE,
*SYMBOL,
*TIMES,TIMES_ROMAN,TIMES_BOLD,TIMES_ITALIC,TIMES_BOLDITALIC,
*ZAPFDINGBATS
*@paramfontSize字体大小
*@returnFont
*/
publicFontgetBaseFont(StringfontName,floatfontSize){
if(fontName==null)
fontName=FontFactory.COURIER;
elseif("".equals(fontName))
fontName=FontFactory.COURIER;
Fontfont=FontFactory.getFont(fontName);
font.setSize(fontSize);
returnfont;
}
/**
*插入段落
*@paramcontent段落内容
*@paramfont字体
*@paramalign对齐方式
*@return
*@throwsException
*/
publicvoidinsertParagraph(Stringcontent,Fontfont,Stringalign)throwsException
{
Phrasephrase=newPhrase(content,font);
Paragraphpg=newParagraph(phrase);
if(align!
=null&&align.equalsIgnoreCase("left"))
pg.setAlignment(Paragraph.ALIGN_LEFT);
elseif(align!
=null&&align.equalsIgnoreCase("right"))
pg.setAlignment(Paragraph.ALIGN_RIGHT);
else
pg.setAlignment(Paragraph.ALIGN_CENTER);
//设置段落后的间距
pg.setSpacingAfter(spacingAfter);
//设置段落前的间距
pg.setSpacingBefore(spacingBefore);
document.add(pg);
}
publicvoidinsertParagraph(Phrasephrase,Stringalign)throwsDocumentException{
Paragraphpg=newParagraph();
pg.add(phrase);
if(align!
=null&&align.equalsIgnoreCase("left"))
pg.setAlignment(Paragraph.ALIGN_LEFT);
elseif(align!
=null&&align.equalsIgnoreCase("right"))
pg.setAlignment(Paragraph.ALIGN_RIGHT);
else
pg.setAlignment(Paragraph.ALIGN_CENTER);
//设置段落后的间距
pg.setSpacingAfter(spacingAfter);
//设置段落前的间距
pg.setSpacingBefore(spacingBefore);
document.add(pg);
}
publicvoidaddElement(Elementelement)throwsDocumentException{
document.add(element);
}
/**
*向PDF添加table对象,需在initTable后使用
*@throwsDocumentException
*/
publicvoidaddTable()throwsDocumentException{
if(isInitPdf&&table!
=null)
document.add(table);
}
/**
*关闭,结束操作,必须调用
*/
publicvoidclose(){
if(isInitPdf)
document.close();
}
/**
*插入表格Cell
*@paramcontent表格内容
*@paramfont字体
*@paramalign对齐方式
*@returncell
*@throwsException
*/
publicvoidaddCell(Stringcontent,Fontfont,Stringalign)throwsException{
if(table!
=null){
if(content==null){
content="";
}
Cellcell=newCell(newPhrase(content,font));
cell.setBorder(0);
if("left".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
}elseif("center".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
}else{
cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
}
if(isSetBorder==true){
cell.setBorder(15);
cell.setBorderColor(Color.black);
}
table.setPadding(1f);
table.addCell(cell);
}
}
/**
*插入表格Cell,允许跨行跨列
*@paramcontent表格内容
*@paramfont字体
*@paramalign对齐方式
*@paramrowspan跨行数
*@paramcolspan跨列数
*@return
*@throwsException
*/
publicvoidaddCell(Stringcontent,Fontfont,Stringalign,introwspan,intcolspan)throwsException{
if(table!
=null){
if(content==null){
content="";
}
Cellcell=newCell(newPhrase(content,font));
cell.setBorder(0);
if("left".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
}elseif("center".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
}else{
cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
}
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
if(isSetBorder==true){
cell.setBorder(15);
cell.setBorderColor(Color.black);
}
if(rowspan>1)
cell.setRowspan(rowspan);
if(colspan>1)
cell.setColspan(colspan);
table.setPadding(1f);
table.addCell(cell);
}
}
/**
*插入表格Cell,允许跨行跨列
*@paramcontent表格内容
*@paramfont字体
*@paramalign对齐方式
*@paramrowspan跨行数
*@paramcolspan跨列数
*@paramheightCell高度
*@return
*@throwsException
*/
publicvoidaddCell(Stringcontent,Fontfont,Stringalign,introwspan,intcolspan,floatheight)throwsException{
if(table!
=null){
if(content==null){
content="";
}
Cellcell=newCell(newPhrase(content,font));
cell.setBorder(0);
cell.setLeading(height);//设置Cell的高度
if("left".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
}elseif("center".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
}else{
cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
}
if(isSetBorder==true){
cell.setBorder(15);
cell.setBorderColor(Color.black);
}
if(rowspan>1)
cell.setRowspan(rowspan);
if(colspan>1)
cell.setColspan(colspan);
table.setPadding(1f);
table.addCell(cell);
}
}
/**
*插入表格Cell
*@paramcontent表格内容
*@paramfont字体
*@paramalign对齐方式
*@paramisHeader是否表头
*@return
*@throwsException
*/
publicvoidaddCell(Stringcontent,Fontfont,Stringalign,BooleanisHeader)throwsException{
if(table!
=null){
if(content==null){
content="";
}
Cellcell=newCell(newPhrase(content,font));
cell.setHeader(isHeader);//设置为表头
cell.setBorder(0);
if("left".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_LEFT);
}elseif("center".equalsIgnoreCase(align)){
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
}else{
cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
}
if(isSetBorder==true){
cell.setBorder(15);
cell.setBorderColor(Color.black);
}
table.setPadding(1f);
table.addCell(cell);
}
}
/**
*插入表格Cell,允许跨行跨列
*@paramcontent表格内容
*@paramfont字体
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java itext 生成 pdf