Below is the code I have used to diaplay/open pdf file
String sFileName =
strCampCodeVal+"_"+nReqIdVal+".pdf";
String sFile =
"C:\\SBLI\\BCP\\barcode_files\\"+sFileName;
File fFile = new File(sFile);
if ( fFile == null )
{
System.out.println("System can not download the
file at this time. Please try again later.");
return;
}
if ( !fFile.exists() || !fFile.canRead() )
{
System.out.println("You have specified an
invalid file to download. Please check and try
again.");
return;
}
res.setContentType("application/force-download");
res.setHeader("Content-disposition",
"attachment;filename=" + sFileName);
res.setHeader("Cache-control", "must-revalidate");
ServletOutputStream sosCur = res.getOutputStream();;
BufferedInputStream bisFile = null;
try
{
byte [] bBuffer = new byte[4096];
bisFile = new BufferedInputStream(new
FileInputStream(fFile));
int nBytes = -1;
while( (nBytes = bisFile.read(bBuffer, 0, 4096)) !=
-1 )
{
sosCur.write(bBuffer, 0, nBytes);
}
}
catch(Exception ex)
{
System.out.println("Failed to download the file \""
+ sFile + "\"" + ex.getMessage());
}
finally
{
try
{
if ( bisFile != null )
bisFile.close();
}
catch(Exception ex)
{
}
if ( sosCur != null )
{
sosCur.flush();
}
}
Hope this will be helpful,
-Raja
--- Murali Thirumalai
<[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am using a Servlet to create a pdf file and
> display the output
> through the servlet. The pdf file gets created in
> the Server directory
> with all thePdfContentByte content. Any content
> added through
> pdfContentByte is not shown in the servlet output,(
> while
> non-pdfcontentbyte content is shown in the servlet
> output). The file
> has all these content (but does not display through
> Servletoutputstream)
> Can you please help?
>
> The code is listed below. Can someone tell me what
> I am doing wrong
> here?
>
> Thanks in advance for your help.
>
> regards
> Murali Thirumalai
>
>
> Document doc = new
> Document(PageSize.LETTER.rotate());
> PdfWriter writer = PdfWriter.getInstance(doc, new
> FileOutputStream("HTRSCertificates.pdf"));
> ByteArrayOutputStream baos = new
> ByteArrayOutputStream();
> PdfWriter.getInstance(doc, baos);
> doc.open();
> PdfContentByte cb = writer.getDirectContentUnder();
> doc.add(Chunk.NEWLINE);
> doc.add(new Chunk("Test content"));
> helv = BaseFont.createFont("Helvetica",
> BaseFont.WINANSI, false);
> // step 4: adding content
> for (int i=0; i<list1.size();i++)
> {
> cb.beginText();
> p1 = (Participant) list1.get(i);
> cb.setFontAndSize(helv,24);
> Font font = new Font(helv, 12, Font.ITALIC);
> doc.add(new Chunk(p1.getFirstName()+' ' +
> p1.getLastName()));
>
>
cb.showTextAlignedElement.ALIGN_CENTER,p1.getFirstName()+'
> ' +
> p1.getLastName(), doc.getPageSize().width() / 2,
> 350, 0);
> cb.showTextAligned(Element.ALIGN_CENTER, clsDt,
> doc.getPageSize().width() / 2, 250, 0);
> cb.endText();
> if (i<list1.size()-1) doc.newPage();
> }
> // step 5: closing the document
> doc.close();
> PdfWriter.getInstance(doc, baos);
> response.setHeader("Expires", "0");
> response.setHeader("Cache-Control",
> "must-revalidate, post-check=0,
> pre-check=0");
> response.setHeader("Pragma", "public");
> // setting the content type
> response.setContentType("application/pdf");
> // the contentlength is needed for MSIE!!!
> response.setContentLength(baos.size());
> // write ByteArrayOutputStream to the
> ServletOutputStream
> ServletOutputStream out =
> response.getOutputStream();
> baos.writeTo(out);
> out.flush();
>
>
>
> regards,
> Murali Thirumalai
> Consulting Engagement Manager-Information Technology
> Office of the Attorney General
>
>
> CONFIDENTIALITY NOTICE
>
> The information contained in this communication from
> the
> Office of the New Jersey Attorney General is
> privileged
> and confidential and is intended for the sole use of
> the
> persons or entities who are the addressees. If you
> are not
> an intended recipient of this e-mail, the
> dissemination,
> distribution, copying or use of the information it
> contains
> is strictly prohibited. If you have received this
> communication
> in error, please immediately contact the Office of
> the Attorney
> General at (609) 292-4925 to arrange for the return
> of this information.
>
>
>
>
-------------------------------------------------------
> This SF.Net email is sponsored by xPML, a
> groundbreaking scripting language
> that extends applications into web and mobile media.
> Attend the live webcast
> and join the prime developer group breaking into
> this new coding territory!
>
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> iText-questions mailing list
> [email protected]
>
https://lists.sourceforge.net/lists/listinfo/itext-questions
>
Thanks,
Raja Kantamneni
Home Phone # (732)452-0073
Cell Phone # (732)429-3337
Email: [EMAIL PROTECTED]
Web Page: http://www.kinneramemorialfoundation.org
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions