您的位置:首页 > 编程语言 > Java开发

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

2017-04-20 15:16 531 查看
最近博主接到一个新需求:用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纸,上下左右边距均为20mm
Document 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();


生成效果如下:



欢迎大家指点交流…….
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java pdf