SuperMoonster
Programmer
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:
I accept any kind of help on ways to do it
Thanks in advance
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:
I accept any kind of help on ways to do it
Thanks in advance