*
The moose likes Other Open Source Projects and the fly likes Problem for writing result set of data into pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Problem for writing result set of data into pdf " Watch "Problem for writing result set of data into pdf " New topic
Author

Problem for writing result set of data into pdf

vishal shiramshetty
Greenhorn

Joined: Jul 08, 2008
Posts: 4
Hello Friends,

I got a problem i.e i am able to create a pdf file with some text using iText but i want to write a query output(result set) into that pdf file

can any one tell me how to solve this one.

waiting for your replies.

Here is the sample code for it
public class VishalPdf extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {

Document document = new Document();


String filePath =this.getServletContext().getRealPath("/");
PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(filePath+"/sat.pdf"));
document.open();
document.add(new Paragraph("Sathish Reddy"));//text
document.add(new Paragraph("Swe"));//Text


Vector v=new Vector();
Collection al = new ArrayList();

Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection ("jdbc racle:thin:@10.10.108.199:1521:TRIPURA", "beneficiary", "beneficiary");

} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
}


PreparedStatement ps2 = con.prepareStatement("select * from Master.MS001_SCHM_MASTER ");
ResultSet rs2=ps2.executeQuery();
//how to add this result set in to pdf
while(rs2.next())
{
//System.out.println("inside selectAllScheme");

v.addElement(rs2.getString(1));
v.addElement(rs2.getString(2));
v.addElement(rs2.getString(3));

//return v;
}


Enumeration e=v.elements();
while(e.hasMoreElements())
{

document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());

}

document.close();

ServletOutputStream so = response.getOutputStream();
int read=0;
byte[] bytes=new byte[1024];
System.out.println("sathish");

String tempFilePath = filePath + "/"+"sat.pdf";
String temp_1="sat.pdf";

//String contentType = URLConnection.guessContentTypeFromName("sat.pdf");
response.setContentType("application/pdf");
response.setHeader("pragma", "public");
response.setHeader("Cache-control", "public");
//response.setHeader("Content-Type",contentType);
response.setHeader("Content-Disposition", "attachment; filename=/"+temp_1+"/");
ServletContext ctx=getServletContext();
InputStream is =ctx.getResourceAsStream("/sat.pdf");
OutputStream os =response.getOutputStream();

while((read=is.read(bytes))!=-1)
{
System.out.println("sathish");
so.write(bytes, 0, read);
// os.write(bytes,0,read);
}
//os.flush();
//os.close();
so.flush();
so.close();
//RequestDispatcher rd = request.getRequestDispatcher("Sat");
//rd.forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(VishalPdf.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

finally {
// out.close();

}
}

}
[ July 15, 2008: Message edited by: vishal shiramshetty ]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi welcome to Javaranch,
Firstly please UseCodeTags

<blockquote>code:
<pre name="code" class="core">v.addElement(rs2.getString(1));
v.addElement(rs2.getString(2));
v.addElement(rs2.getString(3));

//return v;
}


Enumeration e=v.elements();
while(e.hasMoreElements())
{

document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());

}
</pre>

</blockquote>

looks like here you are putting the resultset in a vector and tehn into the PDF document.
So what is the problem.
Are you not getting proper data in the document.

Please take some time and read the ask good questions link below.

Hope this helps .
[ July 15, 2008: Message edited by: Amit Ghorpade ]

SCJP, SCWCD.
|Asking Good Questions|
vishal shiramshetty
Greenhorn

Joined: Jul 08, 2008
Posts: 4
thanks for reply

the result set of data is not reflecting in to a PDF file

can you tell me how to write result set of data into PDF
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
The method you're calling repeatedly is "addSubject", which doesn't seem to make much sense. Did you mean to add the results as paragraphs instead?


Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem for writing result set of data into pdf