TorK TorK - 8 months ago 37
Java Question

Javax.swing.text, Lowagie, HTMLWriter adding image (Not from file)

I am trying to create a document with htmlWriter in com.lowagie.text in Java.
What I do, is that I create an image (from qr-code) and try to add it to the document.
The document is connected to an ByteArrayOutputStream, and then I write it out to a ServletOutputStream.

When I create an image from bitmatrix, nothing happens.
I wonder if this is because the html need an image-URL. So If I get the image from url, it shows. But when I just create an image in java, it will not display this in the html?!?
Can anyone help me?

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// setting some response headers
response.setHeader("Expires", EXPIRES);

// setting the content type

ServletOutputStream out = null;
ByteArrayOutputStream baos = null;
try {
baos = getHtmlTicket();

// write ByteArrayOutputStream to the ServletOutputStream
out = response.getOutputStream();
catch (Exception e) {

log.error(e.getMessage(), e);
// response.setHeader("Content-Disposition", "filename=\"" + filename + "\"");
response.getWriter().write("<p>Det har oppst�tt en feil!</p>");
response.getWriter().write("<p>" + new Date().toString() + "</p>");
response.getWriter().write("<!-- " + e.getMessage() + " -->");

public ByteArrayOutputStream getHtmlTicket() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

Document document = new Document();

String myCodeText = "YO YOU";
int size = 128;
try {
HtmlWriter.getInstance(document, baos);;
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));

Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int pictureWidth = byteMatrix.getWidth();
BufferedImage bimage = new BufferedImage(pictureWidth, pictureWidth,

Graphics2D graphics = (Graphics2D) bimage.getGraphics();
graphics.fillRect(0, 0, pictureWidth, pictureWidth);

for (int i = 0; i < pictureWidth; i++) {
for (int j = 0; j < pictureWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);

com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(bimage , null);

catch (DocumentException e) {
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
catch (IOException e) {
// TODO Auto-generated catch block
catch (WriterException e) {
// TODO Auto-generated catch block


return baos;

Answer Source

HtmlWriter was created to test the Itext library during development. That is why the image only display as a square without content. That is also why the creators of Itext have removed htmlWriter it in later versions.

If you want the response to display the image (must be bufferedImage) in HTML, you can do so by converting the image to Base64 like this:

 private String addImageToHTML(BufferedImage bf) {
    String base64String = "";
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try {
        ImageIO.write(bf, "png", baos);
        base64String = DatatypeConverter.printBase64Binary(baos.toByteArray());
    catch (IOException e) {
    return "<img style='max-width:100%' src='data:image/png;base64,"+ base64String + "' alt='IMG DESC'/>";