如何使用c#为mvc web应用程序将单词转换为图像文件 [英] how to convert word to image file using c# for mvc web application

查看:56
本文介绍了如何使用c#为mvc web应用程序将单词转换为图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi, how to convert word file to image file using c#?

推荐答案

您好,



您可以使用下面的代码将单词转换为jpeg.it对我有用。



Hello,

You can use below code to convert word to jpeg.it works for me.

var docPath = Path.Combine(startupPath, filename1);
var app = new Microsoft.Office.Interop.Word.Application();

MessageFilter.Register();

app.Visible = true;

var doc = app.Documents.Open(docPath);

doc.ShowGrammaticalErrors = false;
doc.ShowRevisions = false;
doc.ShowSpellingErrors = false;

if (!Directory.Exists(startupPath + "\\" + filename1.Split('.')[0]))
{
     Directory.CreateDirectory(startupPath + "\\" + filename1.Split('.')[0]);
}

//Opens the word document and fetch each page and converts to image
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
      foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
      {
            for (var i = 1; i <= pane.Pages.Count; i++)
            {
                 var page = pane.Pages[i];
                 var bits = page.EnhMetaFileBits;
                 var target = Path.Combine(startupPath + "\\" + filename1.Split('.')[0], string.Format("{1}_page_{0}", i, filename1.Split('.')[0]));

                 try
                 {
                     using (var ms = new MemoryStream((byte[])(bits)))
                     {
                          var image = System.Drawing.Image.FromStream(ms);
                          var pngTarget = Path.ChangeExtension(target, "png");
                          image.Save(pngTarget, ImageFormat.Png);
                     }
                 }
                 catch (System.Exception ex)
                 { }
         }
    }
}
doc.Close(Type.Missing, Type.Missing, Type.Missing);
app.Quit(Type.Missing, Type.Missing, Type.Missing);
MessageFilter.Revoke();


这篇关于如何使用c#为mvc web应用程序将单词转换为图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆