Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Add image to dinamically created PDF with iText

843840Jul 23 2008 — edited Jul 23 2008
Hi,
i'm trying to dinamically create a PDF using iText library and send it as a byte stream to the browser.
When i create simple PDFs with just text, it works fine, but if i try to add images, the code doens't works...
The following error ocurrs: cannot find symbol symbol : variable document location: class valimar2d_digital._printMap document.add(img);
Any ideas?

Code that works:
<%@page import="javax.servlet.*, java.io.*, java.lang.*, java.util.*, com.lowagie.text.*, com.lowagie.text.pdf.*, java.awt.Color"%>
<%
response.setContentType("application/pdf");

Document doc=new Document();
ByteArrayOutputStream test=new ByteArrayOutputStream();
PdfWriter writer=PdfWriter.getInstance(doc,test);

doc.open();

doc.add(new Paragraph("Teste."));

doc.add(new Paragraph("Titulo."));
doc.add(new Paragraph("Sub-titulo."));

doc.close();

byte[] contents=test.toByteArray();
response.setContentLength(((ByteArrayOutputStream)test).size());
((ByteArrayOutputStream)test).writeTo(response.getOutputStream());
%>

Code that doesn't works:
<%@page import="javax.servlet.*, java.io.*, java.lang.*, java.util.*, com.lowagie.text.*, com.lowagie.text.pdf.*, java.awt.Color"%>
<%
response.setContentType("application/pdf");

Document doc=new Document();
ByteArrayOutputStream test=new ByteArrayOutputStream();
PdfWriter writer=PdfWriter.getInstance(doc,test);

doc.open();

doc.add(new Paragraph("Teste."));

Image img = Image.getInstance("http://localhost:8888/project/imgs/image.jpg");
img.scalePercent(50);
document.add(img);

doc.add(new Paragraph("Titulo."));
doc.add(new Paragraph("Sub-titulo."));

doc.close();

byte[] contents=test.toByteArray();
response.setContentLength(((ByteArrayOutputStream)test).size());
((ByteArrayOutputStream)test).writeTo(response.getOutputStream());
%>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2008
Added on Jul 23 2008
4 comments
1,982 views