0

Is there a way a Java API or application to convert CSV tabular data as text and convert that into a PNG file with lines and a grid and header?

Right now, I am thinking xhtmlrenderer which converts HTML data into an image.

Updated: Andrew gave a good response, I set that as the answer. Also, I used xhtmlrenderer/flying saucer with the same result to convert a html document to an image. It took the same amount of effort as his example.

http://code.google.com/p/flying-saucer/

Now, on github:

https://github.com/berlinbrown/XHTMLRendererForHtmlDataToImage

Berlin Brown
  • 11,504
  • 37
  • 135
  • 203

1 Answers1

2

Can you read the tabular data and put it into a JTable?

If so, call table.paintComponent(Graphics) is a protected method - see instead table.paint(Graphics), where the Graphics object is obtained from an image that is the preferred size of the table.


Could you provide a more complete example?

Table image

This example uses the Nimbus PLAF for the 'alternate row shading' which I think every table should have.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.table.JTableHeader;

import javax.imageio.ImageIO;
import java.io.File;

class TableImage {

    public static void main(String[] args) throws Exception {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception useDefault) {
        }

        Object[][] data = {
            {"Hari", new Integer(23), new Double(78.23), new Boolean(true)},
            {"James", new Integer(23), new Double(47.64), new Boolean(false)},
            {"Sally", new Integer(22), new Double(84.81), new Boolean(true)}
        };

        String[] columns = {"Name", "Age", "GPA", "Pass"};

        JTable table = new JTable(data, columns);
        JScrollPane scroll = new JScrollPane(table);

        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll,BorderLayout.CENTER);

        // JTable must have been added to a TLC in order to render
        // correctly - go figure.
        JFrame f = new JFrame("Never shown");
        f.setContentPane(scroll);
        f.pack();

        JTableHeader h = table.getTableHeader();
        Dimension dH = h.getSize();
        Dimension dT = table.getSize();
        int x = (int)dH.getWidth();
        int y = (int)dH.getHeight() + (int)dT.getHeight();

        scroll.setDoubleBuffered(false);

        BufferedImage bi = new BufferedImage(
            (int)x,
            (int)y,
            BufferedImage.TYPE_INT_RGB
            );

        Graphics g = bi.createGraphics();
        h.paint(g);
        g.translate(0,h.getHeight());
        table.paint(g);
        g.dispose();

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
        ImageIO.write(bi,"png",new File("table.png"));

        // our TLC forces us to explicitly exit the VM
        System.exit(0);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I probably could. I just need to convert my data to text. Can I include lines and any kind of formatting? – Berlin Brown Sep 09 '11 at 15:57
  • The image should include the usual cell and table borders, is that the type of lines you are referring to? What do you mean by 'any type' of formatting? A table cell can render HTML (e.g. formatted) text, but I am neither used to seeing, nor programming, table cells to contain data with more advanced formatting (e.g. headers, lists, paragraphs, images etc.). – Andrew Thompson Sep 09 '11 at 16:10
  • Could you provide a more complete example? – Berlin Brown Sep 09 '11 at 17:37
  • Well, one that fails, sure. See [Dude, where's my header?](http://stackoverflow.com/questions/7369814/dude-wheres-my-header) for the details. ;) – Andrew Thompson Sep 10 '11 at 05:18
  • Better variants were devised by camickr & kleopatra. See the linked thread for details. – Andrew Thompson Sep 11 '11 at 05:02
  • I like your answer and will give it to you. But, I ended up using xhmtmlrenderer/flying saucer to convert a html document to an image and it took 10 minutes. Thanks though. – Berlin Brown Sep 12 '11 at 17:11
  • No worries. I was wondering if you'd gone the alternate route. ;) – Andrew Thompson Sep 12 '11 at 23:09