我正在写一些加载模板html文档的东西,在其上做一些关键字替换,然后将其打印出来.除了模板中包含图像之外,这种方法很好.如果我浏览模板.html图像显示正常(所以我猜路径没问题),但它们在最终输出中显示为空白区域.
模板html是这样的:
<html>
<body>
<img src="file://c:/temp/my-logo.png" width="50" height="50"/>
[[[some stuff I want to replace]]]
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
很简单.并加载此模板如下:
public void test() {
JEditorPane text = new JEditorPane("text/html", "default");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
HTMLDocument htmlDocument = (HTMLDocument)htmlEditorKit.createDefaultDocument();
text.setEditorKit(htmlEditorKit);
// read the html template into the JEditorPane's text
text.read(new BufferedReader(new InputStreamReader(new FileInputStream(new File("path to my template html")))), htmlDocument);
// then do some replacements
text.setText(magicReplacements(text.getText()));
text.repaint();
// and then print job stuff, fire off the job, check if it worked etc...
}
Run Code Online (Sandbox Code Playgroud)
文本正确显示和格式化,只显示图像.谁能发现什么是错的?
干杯.
我认为这条路是错的.
而不是file://c:/temp/my-logo.png你应该尝试file:/c:/temp/my-logo.png
在测试我的例子时,我使用了两个//c:/和/c:/第二个工作,第一个失败了.
我能够让这个工作......

HTML
<html>
<body>
<img src="file:/c:/backgroundtext.png"/>
[[[some stuff I want to replace]]]
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
示例代码
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileReader;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTextPane {
public static void main(String[] args) {
new TestTextPane();
}
public TestTextPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
FileReader fr = null;
try {
fr = new FileReader(new File("c:/Test.html"));
JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.read(fr, "Test");
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(editor));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
fr.close();
} catch (Exception e) {
}
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1923 次 |
| 最近记录: |