作項目時要用到itext,從網上去查,好多都無法用了,我就把本身作的有關itext的整理了一下,但願能給你們一些幫助php
1 首先如今好多上面的資料顯示的有關itext的導入的架包的版本是itextpdf2的jar包,因此你去查資料的時候會發現不少像這樣的,只是版本的問題,java
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font; windows
如今itext的jar包的版本是itextpdf-5.2.0.jar,你們能夠去http://www.itextpdf.com/進行下載app
2 典型的五步建立一個Helloword.pdfjsp
import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class PdfTest { public static void main(String[] args) throws FileNotFoundException, DocumentException { Document doc=new Document();//第一步建立文檔 PdfWriter.getInstance(doc, new FileOutputStream("WebRoot/Helloword.pdf"))//;第二步建立實例,在WebRoot 裏面建立一個Helloword.pdf doc.open();//第三步開啓文檔 doc.add(new Paragraph("hello"));//在文檔內添加信息 doc.close();//關閉文檔 } }
這裏有個實例能夠幫助你們更深刻的瞭解:ide
在index.jsp中咱們寫了這樣的一個jsp:post
<body> <form action="pdf.php" method="post">//這裏的pdf.php實際上是servlet,也就在配置路徑的時候將其改成pdf.php 標題: <input type="text" name="title"/><br> 做者:<input type="text" name="author"/><br> 內容:<textarea name="content"></textarea><br> <input type="submit" value="生成pdf"/> </form> </body>
咱們建立一個servlet名爲PdfServlet:字體
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; public class PdfServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); // 定義中文字體使其以中文的格式顯示 BaseFont bfChinese = null; try { bfChinese = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } catch (Exception e) { e.printStackTrace(); } Font fontCN = new Font(bfChinese, 12, Font.NORMAL,BaseColor.BLUE); //接收jsp頁面的信息 String title=request.getParameter("title"); String author=request.getParameter("author"); String center=request.getParameter("content"); //設置格式爲pdf類型的 response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment;filename=\"data.pdf\"");//filename是將其以pdf導出時的名字 ServletOutputStream out = response.getOutputStream(); Document doc=new Document();//建立文檔 try { PdfWriter.getInstance(doc, out);//建立實例 doc.open();//開啓文檔 doc.addAuthor(author);//document裏面自帶的有添加Author doc.addTitle(title);//document裏面自帶的有添加Title Paragraph pa=new Paragraph(author,fontCN);//設置以段落的格式輸出 pa.setAlignment(Element.ALIGN_RIGHT);//設置其排版的格式靠右 Paragraph pt=new Paragraph(title,fontCN);//設置以段落的格式輸出 pt.setAlignment(Element.ALIGN_CENTER);//設置其排版的格式居中 Paragraph pc=new Paragraph(center,fontCN); //將段落添加進文檔 doc.add(pa); doc.add(pt); doc.add(pc); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } doc.close();//關閉文檔 } } 這樣就能夠保存爲pdf 有關itext的表格: import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class MyFirstTable { /** The resulting PDF file. */ public static final String RESULT = "WebRoot/first_table.pdf"; /** * Main method. * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws IOException, DocumentException { new MyFirstTable().createPdf(RESULT); } /** * Creates a PDF with information about the movies * @param filename the name of the PDF file that will be created. * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 document.add(createFirstTable()); // step 5 document.close(); } /** * Creates our first table * @return our first table */ public static PdfPTable createFirstTable() { // a table with three columns PdfPTable table = new PdfPTable(3); // the cell object PdfPCell cell; // we add a cell with colspan 3 cell = new PdfPCell(new Phrase("Cell with colspan 3")); cell.setColspan(3); table.addCell(cell); // now we add a cell with rowspan 2 cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); // we add the four remaining cells with addCell() table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } }