我想從動態html佈局呈現圖像。我如何根據html中表格的高度和寬度動態設置圖片的寬度和高度。 我的意思是如何從html
找到表width(x)
和height(y)
?動態呈現圖像
JLabel label = new JLabel(html);
label.setSize(x, y);
BufferedImage image = new BufferedImage(label.getWidth(),label.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setColor(Color.WHITE);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
label.paint(g2d);
g2d.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", baos);
} catch (IOException e) {
e.printStackTrace();
}
下面是html的概要。
<html>
<head>....</head>
<body topmargin="0" leftmargin="0"><table border=0 cellpadding=0 cellspacing=0 width=700 style='border-collapse:collapse;table-layout:fixed;width:684pt'>
......
......
</body></html>
您可能需要使用標籤的首選大小。現在,問題是你強制標籤的大小爲'(x,y)'。發佈一個[SSCCE](http://sscce.org)以獲得更好的幫助 – 2013-05-13 16:35:42