1

我正在写一些东西,它加载一个模板 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>

很简单。并像这样加载这个模板:

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...
}

文本显示和格式正确,只是图像从不显示。任何人都可以发现有什么问题吗?

干杯。

4

1 回答 1

6

我认为路径是错误的。

而不是file://c:/temp/my-logo.png你应该尝试file:/c:/temp/my-logo.png

在测试我的示例时,我同时使用了//c:/and/c:/并且第二个有效,而第一个失败了。

我能够让它工作......

在此处输入图像描述

HTML

<html>
<body>
<img src="file:/c:/backgroundtext.png"/>
[[[some stuff I want to replace]]]
</body>
</html>

示例代码

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) {
                    }
                }
            }
        });
    }
}
于 2013-02-26T04:06:16.153 回答