引入 iText-5.0.2.jar iTextAsian.jar
public static void main(String[] args) throws Exception {
Document doc = new Document (PageSize.A4);
PdfWriter.getInstance (doc, new FileOutputStream("D:/test.pdf"));
doc.open ();
//添加一张图片
// Image image1 = Image.getInstance ("D:\\images\\aa.jpg");
// image1.setAlignment(image1.ALIGN_CENTER);
// image1.scaleAbsolute(500f, 300f);
// //image1.scaleToFit( PageSize.A4.getHeight(),PageSize.A4.getWidth());
// doc.add (image1);
//标题字体
BaseFont bfTitle = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfTitle, 18, Font.NORMAL);
//内容字体
BaseFont bfComic = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 9, Font.NORMAL);
//生成4列的表格
PdfPTable table = new PdfPTable (4);
table.setWidthPercentage(100);
//添加第一行
PdfPCell cell = new PdfPCell (new Paragraph ("订单号:",font));
cell.disableBorderSide(1);//隐藏边框
cell.setBorder(2);//隐藏竖边框
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("500382100101012"));
cell.setColspan (3);
cell.disableBorderSide(2);//隐藏横边框
cell.setBorder(2);//隐藏竖边框
table.addCell (cell);
//添加第二行
table.addCell (new Paragraph ("姓名:",font));
table.addCell (new Paragraph ("支付金额",font));
table.addCell (new Paragraph ("手机号",font));
table.addCell (new Paragraph ("入园时间",font));
//添加第三行
table.addCell (new Paragraph ("张三",font));
table.addCell (new Paragraph ("500"));
table.addCell (new Paragraph ("15023937777"));
table.addCell (new Paragraph ("2013-01-02"));
//添加第四行
table.addCell (new Paragraph ("备注",font));
cell = new PdfPCell (new Paragraph ("武隆喀斯特旅游集团",font));
cell.setColspan (3);
table.addCell (cell);
//添加第五行
table.addCell (new Paragraph ("一维码",font));
Image photo=Image.getInstance("D:\\online\\images\\JJB0FB4140007.jpg");
//设置照片大小
photo.scaleAbsolute(150f, 60f);
cell = new PdfPCell (photo);
cell.setColspan (3);
table.addCell (cell);
for(PdfPRow row:(ArrayList<PdfPRow>)table.getRows()){
for(PdfPCell cells:row.getCells()){
if(cells!=null){
cells.setPadding(10.0f);
}
}
}
doc.add (table);
//插入图片
// doc.newPage();//在下一页开始
// Image image1 = Image.getInstance ("D:\\images\\CCCDE0123456789.jpg");
// image1.setAlignment(image1.ALIGN_CENTER);
// image1.scaleAbsolute(40f, 40f);
// image1.scaleToFit( PageSize.A4.getHeight(),PageSize.A4.getWidth());
// doc.add (image1);
PdfPTable table2 = new PdfPTable (1);
table2.setWidthPercentage(100);
PdfPCell cell2 = new PdfPCell (new Paragraph ("1.少的地方进口商的房间和史蒂夫速",font));
cell2.disableBorderSide(1);//隐藏边框
cell2.setBorder(2);//隐藏竖边框
table2.addCell (cell2);
doc.add(table2);
doc.close ();
}