简体   繁体   中英

convert the contents of word file to JPEG in c#

I want to convert the contents of word file [single page] into JPEG file in C#.

Follwing is the code I have tried. But the Clipboard.GetImage() returns a null.

Please help me out.

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        object fileName = @"C:\Documents and Settings\ErabLK\Desktop\toTest.docx";
        object val = System.Reflection.Missing.Value;
        object falseVal = false;
        Document wordDoc = wordApp.Documents.Open(ref fileName, ref val, ref falseVal, ref val, ref val,
                             ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val,
                             ref val, ref val);


        wordDoc.ActiveWindow.Selection.WholeStory();
        wordDoc.ActiveWindow.Selection.Copy();
        Image img = System.Windows.Forms.Clipboard.GetImage();

GetImage() will only work if there is something on the Clipboard that is an image already.

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getimage(VS.80).aspx

One solution I can think of is to create a Bitmap and use the System.Drawing.Text functions to render the pages text to the Bitmap and save it as a JPEG. You can probably extract the font properties from the word document and use that for the drawing to render it with a similar look.

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();

        object falseVal = false;

        object fileName =@"C:\u.doc";
        object val= System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Document wordDoc1 = wordApp.Documents.Open(ref fileName, ref val, ref falseVal, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val, ref val);

        wordDoc.ActiveWindow.Selection.WholeStory();
        wordDoc.ActiveWindow.Selection.
        Image img = System.Windows.Forms.Clipboard.GetImage();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM