首页 > 解决方案 > JEdi​​torPane 和内容宽度

问题描述

我有一些代码,我在其中获取一些 html 格式的文本,将其放入 JEditorPane 并从中创建图像,如下所示:

String text = getMyText();
int height = getContentHeight(text);
int width = 850;

image = new BufferedImage(WIDTH, height, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

JEditorPane jep = new JEditorPane();
jep.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
jep.setContentType("text/html");
jep.setFont(new Font(myFont));

jep.setSize(width, height);
jep.setText(text);
jep.print(graphics);

graphics.dispose();

String fileName = "myFileName" + ".extension";

File outputfile = new File(myPath + "/" + fileName );

ImageIO.write(image, "png", outputfile);

这非常有效,直到我的 html 文本中有一些具有固定宽度的元素(即 div 或表格等)

由于我的原始宽度固定为 850 像素,如果该元素的宽度大于该宽度,则会发生图像被剪切的情况,这意味着您无法阅读某些文本。

有没有办法让文字不会超过我的固定宽度?还是只是事先确保宽度在限制范围内?

谢谢你的帮助

标签: javahtmlswingwidthjeditorpane

解决方案


推荐阅读