×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Trouble in generating pdf reports with Jasper and iText

Trouble in generating pdf reports with Jasper and iText

Trouble in generating pdf reports with Jasper and iText

(OP)
Hey everyone,

I have a report created on iReports, and I wanna present it on my browser in my web app. The problem is I need to add a barcode in a specific point of this report. This barcode is being generated correctly, using class BarcodeInter25 of iText.

The problem happens when I'm gonna insert this barcode in the existing report in runtime. I tried it two ways:

First way:

         ServletOutputStream out = response.getOutputStream();
         Document document = new Document();
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         
         try {
             
             PdfWriter writer = PdfWriter.getInstance(document, baos);
             
             document.open();
             
             PdfReader reader = new PdfReader("/caminho/meurelatorio.pdf");
             PdfImportedPage page1 = writer.getImportedPage(reader, 1);
             writer.getDirectContent().addTemplate(page1, 1, 0, 0, 1, 0, 0);            
             
             PdfContentByte cb = writer.getDirectContent();
             BarcodeInter25 codeI25 = new BarcodeInter25();
             codeI25.setCode("00190000090050201018400016324188");
             Image imageI25 = codeI25.createImageWithBarcode(cb, null, null);
 
             //ao criar o Chunk, os parametros correspondem a imagem, posicao horizontal,
             //posicao vertical
             document.add(new Phrase(new Chunk(imageI25, 10, -600)));            
             
             document.close();
             } catch (Exception e) {
             e.printStackTrace();
         }
         
         response.setContentType("application/pdf");
         response.setContentLength(baos.size());
         baos.writeTo(out);
         out.flush();



In this first attempt, I use PdfImportedPage to import the page from my existing pdf and put it in my new report. But it it not working: the report shows only with the barcode, it hasn't imported the file I pointed.

Second way:

         ServletOutputStream out = response.getOutputStream();
         Document document = new Document();
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         
         try {
 
             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/caminho/meurelatorio.pdf"));
             
             document.open();
 
             PdfContentByte cb = writer.getDirectContent();
             BarcodeInter25 codeI25 = new BarcodeInter25();
             codeI25.setCode("00190000090050201018400016324188");
             Image imageI25 = codeI25.createImageWithBarcode(cb, null, null);
             //ao criar o Chunk, os parametros correspondem a imagem, posicao horizontal,
             //posicao vertical
             document.add(new Phrase(new Chunk(imageI25, 10, -600)));            
             
             document.close();
             } catch (Exception e) {
             e.printStackTrace();
         }
         
         response.setContentType("application/pdf");
         response.setContentLength(baos.size());
         baos.writeTo(out);
         out.flush();



In this second attempt, I try to open the pdf directly in my document and write the image on it with pdfwriter. The code is normally interpreted and it gives me no error, but when it tries to open the pdf of the screen, it doesn't open. The screen remains gray, like if it was forever loading.

The documentation I'm using comes mainly from here:
http://itextdocs.lowagie.com/tutorial/general/copystamp/.

I accept any kind of help on ways to do it

Thanks in advance

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close