将MHT文件转换为图像

时间:2009-08-24 13:25:36

标签: converter

是否有可用于将MHT文件转换为图像的库或API?我们可以使用Universal Document Converter软件来做到这一点吗?感谢任何想法。

1 个答案:

答案 0 :(得分:1)

如果您真的想以编程方式执行此操作,

MHT

  

存档网页。保存Web时   页面作为Internet中的Web存档   资源管理器,网页保存这个   多用途互联网中的信息   邮件扩展HTML(MHTML)格式   .MHT文件扩展名。所有   网页中的相对链接是   重新映射和嵌入的内容   包含在.MHT文件中。

您可以使用 JEditorPane 将其转换为图片

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Test {
    private static volatile boolean loaded;

    public static void main(String[] args) throws IOException {
        loaded = false;
        URL url = new URL("http://www.google.com");
        JEditorPane editorPane = new JEditorPane();
        editorPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("page")) {
                    loaded = true;
                }
            }
        });
        editorPane.setPage(url);
        while (!loaded) {
            Thread.yield();
        }

        File file = new File("out.png");

        componentToImage(editorPane, file);
    }

    public static void componentToImage(Component comp, File file) throws IOException {
        Dimension prefSize = comp.getPreferredSize();
        System.out.println("prefSize = " + prefSize);
        BufferedImage img = new BufferedImage(prefSize.width, comp.getPreferredSize().height,
                                              BufferedImage.TYPE_INT_ARGB);
        Graphics graphics = img.getGraphics();
        comp.setSize(prefSize);
        comp.paint(graphics);
        ImageIO.write(img, "png", file);
    }

}
相关问题