{oapdf_gg}
Office software is closely linked to the PDF,the PDF is also must have to control!      Set home Page  Add to Favorites  
You are here:Home > PDF Application > PDF Development
PDF Development
In the JSP use iText to generate PDF reports
Finishing by: Date:2009-04-17 21:05:08 Popularity: Tags:

2) to PDF files through the form of transport stream to the client's cache. The benefits of doing so will not be any left on the server "relics."

i) directly through generate JSP pages

<%@

page import = "java.io. *, java.awt.Color, com.lowagie.text .*, com.lowagie.text.pdf .*"%>

<%

response.setContentType ( "application/ pdf");

Document document = new Document ();

ByteArrayOutputStream buffer = new ByteArrayOutputStream ();

PdfWriter writer = PdfWriter.getInstance (document, buffer);

document.open ();

document.add (new Paragraph ( "Hello World "));

document.close ();

DataOutput output = new DataOutputStream (response.getOutputStream ());

byte [] bytes = buffer.toByteArray ();

response.setContentLength (bytes.length);

for (int i = 0; i

(

output.writeByte (bytes [i]);

)

%>

ii) through the Servlet to generate

import java.io. *;

import javax.servlet .*;

import javax.servlet.http .*;

import com.lowagie.text .*;

import com.lowagie.text.pdf .*;

public void doGet (HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

(

Document document = new Document (PageSize.A4, 36,36,36,36);

ByteArrayOutputStream ba = new ByteArrayOutputStream ();

try

(

PdfWriter writer = PdfWriter.getInstance (document, ba);

document.open ();

document.add (new Paragraph ( "Hello World "));

)

catch (DocumentException de)

(

de.printStackTrace ();

System.err.println ( "A Document error:" + de.getMessage ());

)

document.close ();

response.setContentType ( "application/ pdf ");

response.setContentLength (ba.size ());

ServletOutputStream out = response.getOutputStream ();

ba.writeTo (out);

out.flush ();

)


End

I used in the project is the second method. In this paper, the source code in my debugger tomcat4 above are adopted. Hope that we can bring convenience to everyone.

there isArticle InformationsComment Information
Category column

Site Help | Site Map | Feedback |
OAPDF.COM Copyright 2009 V1.1