在c#中将word文件的内容转换为JPEG

时间:2010-09-30 03:55:31

标签: c#

我想将word文件[单页]的内容转换为C#中的JPEG文件。

Follwing是我尝试过的代码。但Clipboard.GetImage()返回null。

请帮帮我。

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

2 个答案:

答案 0 :(得分:0)

GetImage()仅在剪贴板上有图像的东西时才有效。

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

我能想到的一个解决方案是创建一个Bitmap并使用 System.Drawing.Text 函数将页面文本渲染到Bitmap并将其另存为JPEG。您可以从word文档中提取字体属性,并将其用于绘图,以使其具有相似的外观。

答案 1 :(得分:0)

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