java程序猿的成长记录之(一)用itext创建并生成pdf

来源:互联网 发布:查询域名的二级域名 编辑:程序博客网 时间:2024/06/05 14:22
  最近博主接到一个新需求:用java代码生成PDF,最后选择市面上比较流行的itext进行编码操作。  需要引入第三方jar:(itext官网链接 http://itextpdf.com/)
        <dependency>            <groupId>com.itextpdf</groupId>            <artifactId>itextpdf</artifactId>            <version>5.5.10</version>        </dependency>        <dependency>            <groupId>com.itextpdf</groupId>            <artifactId>itext-asian</artifactId>            <version>5.2.0</version>        </dependency>

开始编码ing:
创建一个document对象来对PDF进行操作

//博主为了测试方便,直接将PDF生成到桌面public static final String PATH = "C:/Users/liuxi/Desktop/"+ new Date().getTime() + ".pdf";// 创建PDF输出路径OutputStream os = new FileOutputStream(PATH);// 大小为A4纸,上下左右边距均为20mmDocument document = new Document();     // 设置文档大小document.setPageSize(PageSize.A4);// 设置边距,单位都是像素,换算大约1厘米=28.33像素document.setMargins(56, 56, 56, 0);PdfWriter writer=PdfWriter.getInstance(document, os);// 打开文档document.open();

设置字体

//添加对中文支持BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font font_title = new Font(bf, 16);Font font_pro = new Font(bf, 8);

itext设置字体的三种方式(建议使用第一种)

//使用iTextAsian.jar中的字体BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//使用Windows系统字体(TrueType)BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);//使用资源字体(ClassPath)BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);     

添加水印图片

//添加水印PdfContentByte under = writer.getDirectContentUnder();//获取图片实例Image waterImage=Image.getInstance();//对图片进行定位(博主实践证明,是相对PDF左下角进行定位)waterImage.setAbsolutePosition(); //设置图片大小waterImage.scaleAbsolute();under.addImage(waterImage);

添加图片

Paragraph img_box = new Paragraph();//这个image对象是itext的,getInstance(图片url) 可以获得图片的宽高等属性Image image = Image.getInstance();float height = image.getScaledHeight();float width = image.getScaledWidth();//由于图片或大或小,可以对图片进行等比压缩后在进行布局等比压缩定位代码略......//图片位置居中image.setAlignment(Image.MIDDLE);img_box.add(image);// PDF写入图片document.add(img_box);

设置直线

//创建一个直线的实例LineSeparator line = new LineSeparator();//设置位置line.setAlignment(Element.ALIGN_CENTER);//长度(占据PDF百分比)line.setPercentage(80);// 线的长度(百分比)//宽度line.setOffset(0.2f);//颜色line.setLineColor(new BaseColor(204, 204,204));Paragraph topline = new Paragraph();topline.add(line);//直线距离上面大小topline.setSpacingBefore(33);document.add(topline);

设置文字

Paragraph title = new Paragraph("吴冠中—江南水乡", font_title);title .setAlignment(Element.ALIGN_CENTER);title .setSpacingBefore(20);document.add(title);

添加表格

// 设置4列的表格PdfPTable table = new PdfPTable(4);//表格宽度table.setTotalWidth(PageSize.A4.getWidth() - 200);table.setSpacingBefore(20);//固定宽度table.setLockedWidth(true);// 无边框table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);// 添加内容table.addCell(new Paragraph("开本:600×400mm", font_pro));table.addCell(new Paragraph("材质:纸本", font_pro));table.addCell(new Paragraph("仓库:上海博物馆", font_pro));table.addCell(new Paragraph("时间:2017年3月16日", font_pro));document.add(table);

关闭文档,大功告成

// 关闭文档document.close();

生成效果如下:
这里写图片描述

欢迎大家指点交流…….

0 0
原创粉丝点击