1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JAVA使用itext来生成PDF表格实例和使用说明

JAVA使用itext来生成PDF表格实例和使用说明

时间:2019-01-01 09:00:42

相关推荐

JAVA使用itext来生成PDF表格实例和使用说明

在对接某第三方XX平台时需要对表格传入PDF文件类型。于是自造了一个。

Maven使用版本如下:

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.1</version></dependency>

支持中文的一个jar包

<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

最终效果:

以下是全部的代码实现以及说明:

package com.qingzu.applet.util;import com.itextpdf.text.*;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import org.ponent;import java.io.FileOutputStream;public class CreatePDFUtils1 {/***新建以下两个方法,创建表格内的字体和样式的方法* @param str 内容* @param font 字体对象* @param high 表格高度* @Param alignCenter 是否水平居中* @Param alignMidde 是否垂直居中* @return*/private static PdfPCell mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde){PdfPCell pdfPCell = new PdfPCell(new Phrase(str,font));pdfPCell.setMinimumHeight(high);pdfPCell.setUseAscender(true); // 设置可以居中if (alignCenter){pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中}if (alignMidde){pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中}return pdfPCell;}/**** @param str 字符串* @param font 字体* @param high 表格高度* @Param alignCenter 是否水平居中* @Param alignMidde 是否垂直居中* @Param haveColor 是否有背景色(灰色)* @return*/private static PdfPCell mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde,boolean haveColor){PdfPCell pdfPCell = new PdfPCell(new Phrase(str,font));pdfPCell.setMinimumHeight(high);pdfPCell.setUseAscender(true); // 设置可以居中if (alignCenter){pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中}if (alignMidde){pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中}if (haveColor){//颜色代码 RGBpdfPCell.setBackgroundColor(new BaseColor(217,217,217));}return pdfPCell;}public static void createHardwarePDF( String outputPath)throws Exception{//新建文档对象,页大小为A4纸,然后设置4个边距Document document = new Document(PageSize.A4,20,20,30,30);PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outputPath));document.open();//创建字体BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//字体对象Font size14font = new Font(baseFont,14,Font.NORMAL); //大小为14的正常字体Font size10font = new Font(baseFont,10,Font.BOLD); //大小为10的粗体//添加标题PdfPTable tableName = new PdfPTable(1);tableName.setWidthPercentage(90); //设置标题长度占纸张比例tableName.addCell(mircoSoftFont("个人信息",size14font,50,true,true));document.add(tableName);//添加第二行的数据PdfPTable secondRowTable = new PdfPTable(3); //三列的意思secondRowTable.setWidthPercentage(90);//这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思secondRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});secondRowTable.addCell(mircoSoftFont(" 姓名: ",size10font,50,false,true));secondRowTable.addCell(mircoSoftFont("李晓明",size10font,50,false,true));secondRowTable.addCell(mircoSoftFont(" 出生日期: 1994年3月14日",size10font,50,false,true));document.add(secondRowTable);//第三行数据PdfPTable thirdRowTable = new PdfPTable(3);thirdRowTable.setWidthPercentage(90);thirdRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});thirdRowTable.addCell(mircoSoftFont(" 名族:",size10font,50,false,true));thirdRowTable.addCell(mircoSoftFont("汉族",size10font,50,false,true));thirdRowTable.addCell(mircoSoftFont(" 联系电话: 13888880000",size10font,50,false,true));document.add(thirdRowTable);//第四行数据PdfPTable fourthRowTable = new PdfPTable(2);fourthRowTable.setWidthPercentage(90);fourthRowTable.setTotalWidth(new float[]{0.66f,0.34f});fourthRowTable.addCell(mircoSoftFont(" 个人描述 :",size10font,175,false,false));fourthRowTable.addCell(mircoSoftFont("个人特长 :",size10font,175,false,false));document.add(fourthRowTable);//第五行PdfPTable fifthDetailName = new PdfPTable(1);fifthDetailName.setWidthPercentage(90);fifthDetailName.addCell(mircoSoftFont("获奖记录 :",size14font,50,true,true));document.add(fifthDetailName);//第六行PdfPTable sisthRowTalbe= new PdfPTable(1);sisthRowTalbe.setWidthPercentage(90);sisthRowTalbe.addCell(mircoSoftFont(" 联系地址: "+"广东省广州市天河区XXXXXXXXXXXXXXXXXX",size10font,50,false,true));document.add(sisthRowTalbe);PdfPTable seventhRowTalbe = new PdfPTable(1);seventhRowTalbe.setWidthPercentage(90);seventhRowTalbe.addCell(mircoSoftFont(" 毕业院校 ",size14font,60,true,true));document.add(seventhRowTalbe);//第八行PdfPTable eiththRowTalbe = new PdfPTable(3);eiththRowTalbe.setWidthPercentage(90);eiththRowTalbe.setTotalWidth(new float[]{0.3f,0.5f,0.2f});eiththRowTalbe.addCell(mircoSoftFont(" 毕业学校",size10font,50,true,true,true));eiththRowTalbe.addCell(mircoSoftFont(" 就读日期",size10font,50,true,true,true));eiththRowTalbe.addCell(mircoSoftFont(" 联系人",size10font,50,true,true,true));document.add(eiththRowTalbe);//接下来加ListString school = "XXX学校";String time = "09 - -06";String name = "陈某";for (int i = 0 ;i<4 ;i++){PdfPTable tempTable = new PdfPTable(3);tempTable.setWidthPercentage(90);tempTable.setTotalWidth(new float[]{0.3f,0.5f,0.2f});tempTable.addCell(mircoSoftFont(school,size10font,50,true,true));tempTable.addCell(mircoSoftFont(time,size10font,50,true,true));tempTable.addCell(mircoSoftFont(name,size10font,50,true,true));document.add(tempTable);}document.close();writer.close();}//使用例子public static void main(String[] args)throws Exception{createHardwarePDF("test.pdf");}}

完成。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。