Java开源工具iText生成PDF简单实例
iText下载页面:http://sourceforge.net/projects/itext/files/
1.创建简单的PDF文件
packageconsole.pdf;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.DocumentException;
importcom.itextpdf.text.PageSize;
importcom.itextpdf.text.Paragraph;
importcom.itextpdf.text.pdf.PdfWriter;
/**
*使用iText生成PDF文件
*/
publicclassCreatePDF{
publicstaticvoidmain(String[]args){
CreatePDFp001=newCreatePDF();
Stringfilename="P001.pdf";
p001.createPDF(filename);
}
publicvoidcreatePDF(Stringfilename){
//step1
Documentdocument=newDocument(PageSize.A4);
//step2
try{
PdfWriter.getInstance(document,newFileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("ThisisthesubjectofthePDFfile.");
document.addKeywords("ThisisthekeywordofthePDFfile.");
//step3
document.open();
//step4
document.add(newParagraph("HelloWorld!"));
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(DocumentExceptione){
e.printStackTrace();
}finally{
//step5
document.close();
}
}
}
2.在PDF文件中添加Table
packageconsole.pdf;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importcom.itextpdf.text.BaseColor;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.DocumentException;
importcom.itextpdf.text.PageSize;
importcom.itextpdf.text.Phrase;
importcom.itextpdf.text.Rectangle;
importcom.itextpdf.text.pdf.PdfPCell;
importcom.itextpdf.text.pdf.PdfPTable;
importcom.itextpdf.text.pdf.PdfWriter;
/**
*使用iText生成PDF文件
*在PDF文件中创建表格
*/
publicclassTableOfPDF{
publicstaticvoidmain(String[]args){
TableOfPDFp001=newTableOfPDF();
Stringfilename="P002.pdf";
p001.createPDF(filename);
}
publicvoidcreatePDF(Stringfilename){
//step1
Documentdocument=newDocument(PageSize.A4);
//step2
try{
PdfWriter.getInstance(document,newFileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("ThisisthesubjectofthePDFfile.");
document.addKeywords("ThisisthekeywordofthePDFfile.");
//step3
document.open();
//step4
PdfPTabletable=createTable1();
document.add(table);
table=createTable2();
table.setSpacingBefore(5);
table.setSpacingAfter(5);
document.add(table);
table=createTable3();
document.add(table);
table=createTable4();
table.setSpacingBefore(5);
table.setSpacingAfter(5);
document.add(table);
table=createTable5();
document.add(table);
table=createTable6();
document.add(table);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(DocumentExceptione){
e.printStackTrace();
}finally{
//step5
document.close();
}
}
/**
*Createsatable;widthsaresetwithsetWidths().
*
*@returnaPdfPTable
*@throwsDocumentException
*/
publicstaticPdfPTablecreateTable1()throwsDocumentException{
PdfPTabletable=newPdfPTable(3);
table.setWidthPercentage(288/5.23f);
table.setWidths(newint[]{2,1,1});
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table1"));
cell.setColspan(3);
table.addCell(cell);
cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row1;cell1");
table.addCell("row1;cell2");
table.addCell("row2;cell1");
table.addCell("row2;cell2");
returntable;
}
/**
*Createsatable;widthsaresetwithsetWidths().
*
*@returnaPdfPTable
*@throwsDocumentException
*/
publicstaticPdfPTablecreateTable2()throwsDocumentException{
PdfPTabletable=newPdfPTable(3);
table.setTotalWidth(288);
table.setLockedWidth(true);
table.setWidths(newfloat[]{2,1,1});
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table2"));
cell.setColspan(3);
table.addCell(cell);
cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row1;cell1");
table.addCell("row1;cell2");
table.addCell("row2;cell1");
table.addCell("row2;cell2");
returntable;
}
/**
*Createsatable;widthsaresetintheconstructor.
*
*@returnaPdfPTable
*@throwsDocumentException
*/
publicstaticPdfPTablecreateTable3()throwsDocumentException{
PdfPTabletable=newPdfPTable(newfloat[]{2,1,1});
table.setWidthPercentage(55.067f);
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table3"));
cell.setColspan(3);
table.addCell(cell);
cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row1;cell1");
table.addCell("row1;cell2");
table.addCell("row2;cell1");
table.addCell("row2;cell2");
returntable;
}
/**
*Createsatable;widthsaresetwithspecialsetWidthPercentage()method.
*
*@returnaPdfPTable
*@throwsDocumentException
*/
publicstaticPdfPTablecreateTable4()throwsDocumentException{
PdfPTabletable=newPdfPTable(3);
Rectanglerect=newRectangle(523,770);
table.setWidthPercentage(newfloat[]{144,72,72},rect);
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table4"));
cell.setColspan(3);
table.addCell(cell);
cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row1;cell1");
table.addCell("row1;cell2");
table.addCell("row2;cell1");
table.addCell("row2;cell2");
returntable;
}
/**
*Createsatable;widthsaresetwithsetTotalWidth().
*
*@returnaPdfPTable
*@throwsDocumentException
*/
publicstaticPdfPTablecreateTable5()throwsDocumentException{
PdfPTabletable=newPdfPTable(3);
table.setTotalWidth(newfloat[]{144,72,72});
table.setLockedWidth(true);
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table5"));
cell.setColspan(3);
table.addCell(cell);
cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row1;cell1");
table.addCell("row1;cell2");
table.addCell("row2;cell1");
table.addCell("row2;cell2");
returntable;
}
publicstaticPdfPTablecreateTable6()throwsDocumentException{
PdfPTabletable=newPdfPTable(10);
table.setTotalWidth(595);
//table.setLockedWidth(true);
PdfPCellcell;
cell=newPdfPCell(newPhrase("Table6"));
cell.setColspan(10);
table.addCell(cell);
for(inti=1;i<100;i++){
cell=newPdfPCell(newPhrase(String.valueOf(i)));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
}
//cell=newPdfPCell(newPhrase("Cellwithrowspan2"));
//cell.setRowspan(2);
//table.addCell(cell);
//table.addCell("row1;cell1");
//table.addCell("row1;cell2");
//table.addCell("row2;cell1");
//table.addCell("row2;cell2");
returntable;
}
}
3.在PDF文件中添加图片,并且指定文本位置
packageconsole.pdf;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.DocumentException;
importcom.itextpdf.text.Image;
importcom.itextpdf.text.PageSize;
importcom.itextpdf.text.pdf.BaseFont;
importcom.itextpdf.text.pdf.PdfContentByte;
importcom.itextpdf.text.pdf.PdfWriter;
/**
*使用iText生成PDF文件
*在PDF文件中添加背景图片,并指定文本在PDF文件中的位置
*/
publicclassBackgroundImageOfPDF{
publicstaticvoidmain(String[]args){
BackgroundImageOfPDFp001=newBackgroundImageOfPDF();
Stringfilename="P003.pdf";
p001.createPDF(filename);
}
publicvoidcreatePDF(Stringfilename){
//step1
Documentdocument=newDocument(PageSize.A4.rotate(),0,0,0,0);
//step2
try{
PdfWriterpdfwriter=PdfWriter.getInstance(document,newFileOutputStream(filename));
document.addTitle("ID.NET");
document.addAuthor("dotuian");
document.addSubject("ThisisthesubjectofthePDFfile.");
document.addKeywords("ThisisthekeywordofthePDFfile.");
//step3
document.open();
//step4
Imageimage=Image.getInstance("bg.jpg");
document.add(image);
PdfContentBytepdfContentByte=pdfwriter.getDirectContent();
pdfContentByte.beginText();
BaseFontbf=BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.WINANSI,BaseFont.EMBEDDED);
pdfContentByte.setFontAndSize(bf,12);
for(inti=0;i<=842;i=i+50){
for(intj=0;j<=595;j=j+20){
pdfContentByte.setTextMatrix(i,j);
pdfContentByte.showText("("+i+":"+j+")");
}
}
pdfContentByte.endText();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(DocumentExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
//step5
document.close();
}
}
}