<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.7</version>
</dependency>
public void export( String year,String month,HttpServletRequest request) throws Exception{
String path = request.getSession().getServletContext().getRealPath("/fileDisk/report/pdf");//获取存放生成的PDF的项目相对路径
String fileName="/月度工作总结.pdf";
File filedir = new File(path);
if (!filedir.exists()) {
filedir.mkdirs();
}
File file = new File(path+fileName);
if(file.exists()&& file.isFile()){
file.delete();
}
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//字体基本样式
Font font = new Font(bfChinese, 12, Font.NORMAL); //1
Font font3 = new Font(bfChinese, 14, Font.BOLD);//一
Font font2 = new Font(bfChinese, 21, Font.BOLD);//大标题
Font font4 = new Font(bfChinese, 12, Font.BOLD);//1.1 正文
Document document = new Document(PageSize.A4,80,78,90,80); //设置纸张大小,页边距
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path +fileName));
//设置页眉页脚,在页面打开之前或关闭之后
HeaderFooter hf=new HeaderFooter();
writer.setPageEvent(hf);
document.open();
//标题
Paragraph p1 = new Paragraph("技术市场部月度工作总结", font2);
p1.setSpacingAfter(15);
p1.setSpacingBefore(15);
p1.setAlignment(1);
document.add(p1);
Paragraph p2 = new Paragraph("一、 本月工作进展", font3);
p1.setSpacingAfter(10);
p1.setSpacingBefore(10);
p1.setAlignment(1);
document.add(p2);
Paragraph p3 = new Paragraph("1. 售后服务保障", font4);
p1.setSpacingAfter(10);
p1.setSpacingBefore(10);
p1.setAlignment(1);
document.add(p3);
Paragraph p4 = new Paragraph(“想要显示的段落内容。。。”, font);
p1.setSpacingAfter(10);
p1.setSpacingBefore(10);
p1.setAlignment(1);
document.add(p4);
//插入图片
String pic1Path = request.getSession().getServletContext().getRealPath("/fileDisk/report/img/pic.png");
Image image = Image.getInstance(pic1Path);
image.scaleAbsolute(400,270);//控制图片大小
document.add(image);
//机车故障地理位置表
PdfPTable table1 = new PdfPTable(13);
table1.setHorizontalAlignment(1);
table1.setWidths(new int[] { 16,7,7,7,7,7,7,7,7,7,7,7,7 });//百分比
table1.addCell(new Paragraph("",font));
table1.addCell(new Paragraph("1月",font));
table1.addCell(new Paragraph("2月",font));
table1.addCell(new Paragraph("3月",font));
table1.addCell(new Paragraph("4月",font));
table1.addCell(new Paragraph("5月",font));
table1.addCell(new Paragraph("6月",font));
table1.addCell(new Paragraph("7月",font));
table1.addCell(new Paragraph("8月",font));
table1.addCell(new Paragraph("9月",font));
table1.addCell(new Paragraph("10月",font));
table1.addCell(new Paragraph("11月",font));
table1.addCell(new Paragraph("12月",font));
//添加到第14单元格自动换列
document.add(table1);
//关闭文件
document.close();
}
/**
* 页眉 页脚,通过页面坐标定位写入段落实现
* 此页眉页脚方式适用于itext5.0以上版本
*/
class HeaderFooter extends PdfPageEventHelper{
public void onEndPage (PdfWriter writer, Document document) {
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
} catch (DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Font font5 = new Font(bfChinese, 17, Font.BOLD);
int pageS = writer.getPageNumber();
String foot1 = "- " + pageS + " -";
ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_LEFT, new Phrase("上海****科技有限公司",font5),80, 785, 0);//页眉 左边距80,下边距785
Font font7 = new Font(bfChinese, 12, Font.NORMAL);
ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_LEFT, new Phrase("SHANGHAI **** TECHNOLOGY CO.,LTD.",font7),80, 772, 0);//页眉
Font font6 = new Font(bfChinese, 10, Font.NORMAL);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(foot1, font6), 300,50,0);//页脚 左边距300,下边距50
}
}
插件缺陷:1. 在IE8下js生成段落放入HTML后生成PDF不会自行换行。
2. 生成PDF中英文句子格式“间距自由”。
3. 不能设置行间距。