tags:

views:

359

answers:

2

Hi, Are there any libraries or APIs available to convert MHT files to images? Can we use Universal Document Converter software to do this? Appreciate any thoughts.

A: 

If you really want to do this programatically,

MHT

Archived Web Page. When you save a Web page as a Web archive in Internet Explorer, the Web page saves this information in Multipurpose Internet Mail Extension HTML (MHTML) format with a .MHT file extension. All relative links in the Web page are remapped and the embedded content is included in the .MHT file.

you can use the JEditorPane to convert this into an Image

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);
    }

}
Narayan
A: 

Hi, We plan to do this configuring Universal Document Writer as virtual printer. The following code works fine for Word Documents. However, I am wondering if we have any COM DLLs available for MHT files which we could use for printing MHT documents. Thanks in advance.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                object oRange = WdPrintOutRange.wdPrintCurrentPage;
                ApplicationClass oAppclass = null;
                Application App = null;
                Document Doc = null;
                object missingValue = Type.Missing;
                object oTrue = true;
                object oFalse = false;
                oAppclass = new ApplicationClass();
                App = oAppclass.Application;
                object fileName = (object)"C:\\e.doc";

                Doc = App.Documents.Open(ref fileName, ref missingValue, ref oTrue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue);
                App.ActivePrinter = "Universal Document Converter";

                App.ActiveDocument.PrintOutOld(ref oTrue, ref oFalse, ref oRange, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue, ref oFalse, ref missingValue, ref missingValue, ref missingValue);



        }
        catch (Exception e)
        {
           Console.WriteLine("Exception {0}",e);
        }
    }
}

}

Are there any COM APIs to open MHT documents in IE and invoke virtual printing?