效果图
package pdftest;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfOutline;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
public class Pdftabletest {
static Font fontChinese ;
static{
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
Pdftabletest.fontChinese=fontChinese;
} catch ( Exception e) {
e.printStackTrace();
}
}
public static class PdfAutopageNumHelper extends PdfPageEventHelper {
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.beginText();
cb.setFontAndSize(fontChinese.getBaseFont(), 10);
//Footer
float y = document.bottom(-20);
String text = "第" + writer.getPageNumber() + "页";
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text,
(document.right() + document.left())/2,
y, 0);
cb.endText();
cb.restoreState();
}
}
/**
* 给PDF添加目录
* @param currentnode
* @param cataname
* @param flag 0 是表示和当前节点平级 1作为当前节点的子节点
* @return
* @throws DocumentException
*/
public static PdfOutline
addCatalog(Document document, PdfOutline currentnode,String cataname,int kidflag) throws DocumentException{
String catakey=cataname+(int)(Math.random()*10000000);
document.add(new Paragraph(new Chunk(cataname,fontChinese).setLocalDestination(catakey)));
PdfOutline oline1;
if(kidflag==1){
oline1 = new PdfOutline(currentnode, PdfAction.gotoLocalPage(catakey, false), cataname);
}else{
oline1 = new PdfOutline(currentnode.parent(), PdfAction.gotoLocalPage(catakey, false), cataname);
}
return oline1;
}
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("f:/test/test.pdf"));
writer.setPageEvent(new PdfAutopageNumHelper());
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfOutline oline= addCatalog(document, cb.getRootOutline(), "产品设计开发", 1);
oline= addCatalog(document, oline, "1.1 策划内容", 1);
PdfPTable table=createTabledata();
document.add(table);
document.newPage();//强制下一页
oline= addCatalog(document, oline, "1.2 进度内容", 0);
document.add(table);
document.newPage();
document.add( createbartestdata(cb)) ;//条码测试;
document.add( createQrtestdata()) ;//二维码生成测试;
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static PdfPTable createTabledata() throws Exception{
PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(10f); // Space before table
table.setSpacingAfter(10f); // Space after table
table.setSplitLate(true);
// Set Column widths
float[] columnWidths = { 1f, 1f, 1f };
table.setWidths(columnWidths);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell1.setPadding(10);
cell2.setPadding(10);
cell3.setPadding(10);
PdfPCell cell11 = new PdfPCell(new Paragraph("Cell1",fontChinese));
PdfPCell cell21 = new PdfPCell(new Paragraph("Cell2中文测试",fontChinese));
PdfPCell cell31 = new PdfPCell(new Paragraph("Cell3",fontChinese));
cell11.setBackgroundColor(BaseColor.GRAY);
cell21.setBackgroundColor(BaseColor.GRAY);
cell31.setBackgroundColor(BaseColor.GRAY);
cell11.setPadding(10);
cell21.setPadding(10);
cell31.setPadding(10);
table.addCell(cell11);
table.addCell(cell21);
table.addCell(cell31);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
return table;
}
public static Image createbartestdata(PdfContentByte cb){
String myString = "http://www.baidu.com";
Barcode128 code128 = new Barcode128();
code128.setCode(myString.trim());
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
code128Image.setAbsolutePosition(10,700);
code128Image.scalePercent(125);
return code128Image;
}
public static Image createQrtestdata() throws Exception{
String myString = "http://www.baidu.com";
BarcodeQRCode qrcode = new BarcodeQRCode(myString.trim(), 1, 1, null);
Image qrcodeImage = qrcode.getImage();
qrcodeImage.setAbsolutePosition(10,600);
qrcodeImage.scalePercent(200);
return qrcodeImage;
}
}
下载地址:http://download.csdn.net/download/qq_25033003/9940258