全栈工程师开发手册 (作者:栾鹏)
java中生成pdf,插入图片,页眉、页脚、表格
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
//Description: 本实例通过使用iText包生成一个表格的PDF文件
public class MyPdf{
public static void main(String[] arg){
write_pdf("table.pdf");
}
//写PDF文件
public static void write_pdf(String filepath)
{
try{
//设置pdf文件输出流
Document document=new Document(PageSize.A4);
document.setMargins(50, 50, 100, 50); //pdf的4个页边距
Rectangle pageRect=document.getPageSize();
PdfWriter.getInstance(document, new FileOutputStream(filepath));
//创建汉字字体
BaseFont bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
Font fontSong = new Font(bfSong, 10, Font.NORMAL);
//添加一个图片,设置图片的位置,大小
try {
Watermark watermark = new Watermark(Image.getInstance("test.jpg"), pageRect.left()+50,pageRect.top()-85);
watermark.scalePercent(50);
document.add(watermark);
}catch(Exception e) {
System.err.println("找不到图片");
}
//增加页头信息
HeaderFooter header = new HeaderFooter(new Phrase("Java实例一百例",fontSong), false); //设置页眉文字
header.setBorder(2); //设置边框
header.setAlignment(Element.ALIGN_RIGHT); //设置对齐方式
document.setHeader(header);
//增加页脚信息
HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 页",fontSong)); //设置页脚文字
footer.setAlignment(Element.ALIGN_CENTER); //设置对齐方式
footer.setBorder(1); //设置边框
document.setFooter(footer);
// 打开文档
document.open();
//添加表格
Table table = new Table(4); //设置Table列数
table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE); //表格垂直对齐方式
table.setBorder(Rectangle.NO_BORDER); //设置边框
int hws[] = {10, 20, 10, 20}; //按比例分配单元格宽度
table.setWidths(hws);
table.setWidth(100); //设置表格占据的宽度比例
//表头信息
Cell cellmain = new Cell(new Phrase("用户信息",new Font(bfSong, 10, Font.BOLD,new Color(0,0,255)))); //创建单元格
cellmain.setHorizontalAlignment(Element.ALIGN_CENTER); //设置水平对齐方式
cellmain.setColspan(4); //设置占据列数
cellmain.setBorder(Rectangle.NO_BORDER); //设置边框
cellmain.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); //设置背景色
table.addCell(cellmain); //添加单元格
//分表头信息
Cell cellleft= new Cell(new Phrase("收货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));//创建单元格
cellleft.setColspan(2); //设置占据列数
cellleft.setHorizontalAlignment(Element.ALIGN_CENTER); //设置水平对齐方式
table.addCell(cellleft);
Cell cellright= new Cell(new Phrase("订货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
cellright.setColspan(2);//设置占据列数
cellright.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平对齐方式
table.addCell(cellright);
//收货和订货人信息,表体内容
table.addCell(new Phrase("姓名",fontSong)); //添加一个单元格
table.addCell(new Phrase("张三",fontSong)); //添加一个单元格
table.addCell(new Phrase("姓名",fontSong)); //添加一个单元格
table.addCell(new Phrase("李四",fontSong)); //添加一个单元格
table.addCell(new Phrase("电话",fontSong)); //添加一个单元格
table.addCell(new Phrase("23456789",fontSong)); //添加一个单元格
table.addCell(new Phrase("电话",fontSong)); //添加一个单元格
table.addCell(new Phrase("9876543",fontSong)); //添加一个单元格
//将表格添加到文本中
document.add(table);
//关闭文本,释放资源
document.close();
}catch(Exception e){
System.out.println(e);
}
}
}