iText是一个非常著名的能够快速产生PDF文件的Java类库。支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。
授权协议: AGPLv3下载地址: http://sourceforge.net/projects/itext/files/
案例:此案例主要说明往pdf中写入文字和图片,另外生成表格
import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class Prin { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String filepath="E:\\pp\\招聘通知.pdf"; String imagepath="E:\\pp\\df.png"; int ColumnNums=6; int CellNums=24; Document document=new Document(); try { /*PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filepath)); document.addAuthor("王胜利"); document.addSubject("text"); document.setPageSize(PageSize.A5); document.open(); document.add(new Paragraph("Just tets....")); document.add(new Paragraph("=================")); Image iamImage=Image.getInstance(imagepath); document.add(iamImage); document.addCreationDate(); document.addTitle("网页"); document.addKeywords("1234");*/ PdfWriter.getInstance(document, new FileOutputStream("E:\\pp\\通知.pdf")); document.open(); PdfPTable table=new PdfPTable(ColumnNums); PdfPCell cell=new PdfPCell(new Paragraph("text")); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(ColumnNums); table.addCell(cell); for (int i = 0; i < CellNums; i++) { cell = new PdfPCell(new Paragraph(i + " ")); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平Alignment cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直Alignment table.addCell(cell); } document.add(table); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } document.close(); System.out.println("执行完毕"); } }
参考案例:
http://www.cnblogs.com/jinsqy/archive/2013/05/08/3066886.html
http://www.cnblogs.com/kakafra/archive/2013/03/05/2944747.html
jar包下载:
jar包下载