Add image to dinamically created PDF with iText
843840Jul 23 2008 — edited Jul 23 2008Hi,
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());
%>