Package com.exce;
Import Java.awt.Color;
Import Java.io.ByteArrayOutputStream;
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.lowagie.text.Document;
Import com.lowagie.text.DocumentException;
Import Com.lowagie.text.Font;
Import com.lowagie.text.PageSize;
Import Com.lowagie.text.Paragraph;
Import Com.lowagie.text.pdf.BaseFont;
Import Com.lowagie.text.pdf.PdfPCell;
Import com.lowagie.text.pdf.PdfPTable;
Import Com.lowagie.text.pdf.PdfWriter;
/** * Export pdf, pop-up download box. * Used JAR Package: Itext-2.1.7.jar/public class Exportpdfaction extends HttpServlet {private static final long serialversion
UID = 1L;
/** * @see httpservlet#httpservlet () * * * Public exportpdfaction () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doGet (HttpServletRequest request, httpservletresponse response) * * protected void doget (HttpServletRequest request, HttpS
Ervletresponse response) throws Servletexception, IOException {response.setcharacterencoding ("utf-8");
try {//1: Create com.lowagie.text.Document Object Document document = new document (pagesize.a4,20,20,20,20); 2: Create PDFWriter object to listen to the document created above and write this document to your defined output stream bytearrayoutputstream BAOs = new Bytearrayoutputstream ()
;
Pdfwriter.getinstance (document, BAOs);
3: Open the document Document.open ();
4: Add content to the document Basefont Bfchinese = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);
Font titlefont = new Font (Bfchinese, font.bold,color.red);
Font Tontchinese = new Font (Bfchinese, font.normal);
Document.add (New Paragraph ("Export PDF file data as follows", Titlefont));
Document.add (New Paragraph ("", Titlefont));
pdfptable table = new pdfptable (8); for (int i=0;i<800;i++) {if (i = = 0) {Pdfpcell cell = new Pdfpcell ();
Cell.setcolspan (8);
Cell.setbackgroundcolor (New Color (120,120,120));
Cell.addelement (New Paragraph ("Table header", New Font (Bfchinese, Font.bold,color.green));
Table.addcell (cell);
else {Pdfpcell cell = new Pdfpcell ();
Cell.setbordercolor (Color.Blue);
Cell.addelement (New Paragraph ("Content" +i, Tontchinese));
Table.addcell (cell);
} document.add (table);
5: Close the document Document.close (); Set to download the file frame, and filename encoded response.setheader ("Content-disposition", "Attachment filename=\" "+new String (" PDF file "). getBytes
(), "iso8859-1") + ". pdf\");
Set File Size Response.setcontentlength (Baos.size ());
Servletoutputstream outputstream = Response.getoutputstream ();
6: Export the PDF file to the stream. Baos.writeto (OutputStream);
Outputstream.flush ();
Outputstream.close ();
catch (Documentexception e) {e.printstacktrace (); }/** * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void Dopo
St (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {}}
In Web.xml configuration <span style= "font-family:arial, Helvetica, Sans-serif;" >exportpdfaction this servlet </span>