1.Tomcat的安裝不描述.現在假如Tomcat 安裝目錄為C:Tomcat 5.0,新建C: /Tomcat 5.0 /webapps /pdfservlet目錄.
安裝JDK 安裝目錄C:j2sdk1.4.2_09 去http://www.lowagie.com/iText/download.html下載itext1.3.0.jar,後解壓 縮,將itext-1.3.0.jar 放到C: Tomcat 5.0 webapps myServlets WEB-INF lib下(當然還有一些必須的包如servlet.jar) 和 C: j2sdk1.4.2_09 lib下.並將C:/j2sdk1.4.2_09/ lib/ itext-1.3.0.jar加到CLASSPATH中.確保能正確編譯.(我也是一名JAVA初學者,我想起碼的JAVA環境配置要會)
2.下面就是itext 的例子,我在網上查過關於ITEXT的貼子,大部份都是轉貼,一個貼子被一幫所謂的JAVA愛好者轉了又轉,難得出一份原創貼,可能那些JAVA高手有點低估我們初學者的水平,來個Helloworld字符串加到PDF中就算過了,TMD那個在實際中頂個叉用.我說這些話也只是希望那些精通JAVA的人能對我們這些初學負一點責任,寫一點好貼,給我們參考一下.廢話不多講,下面是代碼.:
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfPTable;
/**
* Hello World example as a Servlet.
*
* @author blowagie
*/
public class HelloWorldServlet extends HttpServlet {
/**
* Returns a PDF, RTF or HTML document.
*
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("document.add(BigTable)");
// step1
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10); //定義紙張類型及方向,頁邊距
// step 1
try {
// step 2: we set the ContentType and create an instance of the corresponding Writer
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step3
document.open();
// step4 定義表格填充內容
String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD",
"119000", "96 06", "2001-08-13", "4350", "6011648299",
"FLFLMTGP", "153", "119000.00" };
int NumColumns = 12; //定義表格列數
PdfPTable datatable = new PdfPTable(NumColumns); //創建新表.
int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage 定義表格頭寬度
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100); // percentage
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
//以下是填充表頭
datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");
datatable.setHeaderRows(1); // this is the end of the table header
datatable.getDefaultCell().setBorderWidth(1);
for (int i = 1; i < 750; i++) {
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(0.9f);
}
for (int x = 0; x < NumColumns; x++) {
datatable.addCell(bogusData[x]);
}
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(0.0f);
}
}
document.add(datatable); //加載新表
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
正確編譯它,我試過了沒問題,我itext REV.為1.3.0 放到C: Tomcat 5.0 webapps pdfservlet WEB-INF classes
2.由於是Servlet 我們創建一個WEB.XML 放到C: Tomcat 5.0 webapps pdfservlet WEB-INF下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>
HelloWorldServlet
</servlet-name>
<servlet-class>
HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>
3.重新啟動TOMCAT
http://IP地址:8080/pdfservlet/HelloWorldServlet
如果順利的話你可以看到很漂亮的PDF報表