如何将word文档页面转换为特定图像?
我使用以下代码将其转换为.tiff
图片
object varMissing = Type.Missing;
object varFalseValue = false;
object varTrueValue = true;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.ActivePrinter = "Microsoft XPS Document Writer";
object fileName = startupPath + "\\" + filename1;
object falseValue = false;
object trueValue = true;
object missing = Type.Missing;
word.Visible = true;
word.Activate();
Document doc = word.Documents.Open(ref fileName, ref missing,
ref falseValue, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);
doc.Activate();
object PrintToFile = true;
object OutputFileName = startupPath + "\\" + filename1.Split('.')[0] + ".tif";
doc.PrintOut(ref varMissing, ref varFalseValue, ref varMissing,
ref OutputFileName, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref PrintToFile,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing);
doc.Close(ref varMissing, ref varMissing, ref varMissing);
word.Quit(ref varMissing, ref varMissing, ref varMissing);
//Multi to Single
MemoryStream ms;
Image myImage;
FileStream fs = new FileStream(startupPath + "\\" + filename1.Split('.')[0] + ".tif", FileMode.Open);
fs.Seek(0, SeekOrigin.Begin);
//Here I am getting error....
myImage = Image.FromStream(fs, true, false);
Guid myGuid = myImage.FrameDimensionsList[0];
FrameDimension myDimension = new FrameDimension(myGuid);
int myPageCount = myImage.GetFrameCount(myDimension);
for (int i = 0; i < myPageCount; i++)
{
ms = new MemoryStream();
myImage.SelectActiveFrame(myDimension, i);
myImage.Save(string.Format(startupPath + "\\" + filename1.Split('.')[0] + ".tif", i), ImageFormat.Tiff);
}
fs.Close();
但是当我试图将文件流转换为Image时,它给出了像
这样的错误Parameter is not valid.
有没有其他方法可以将word文件转换为图像文件?
我无法使用市场上出售的dll ,但我需要使用Microsoft's core interop dll.
所以,任何人都可以建议我将.doc文件转换为.jpg的链接或方式。
答案 0 :(得分:10)
您可以使用对我有用的bellow代码将Doc文件转换为图像。
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();
答案 1 :(得分:0)
我想我在@WarLock提供的代码中发现了错误。我改变了它的一些部分并且仅使用一个文件来完成它:
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
Document myWordDoc = new Document();
object missing = System.Type.Missing;
object path1= path + filename + ".doc";
myWordDoc = myWordApp.Documents.Add(path1, missing, missing, missing);
foreach (Microsoft.Office.Interop.Word.Window window in myWordDoc.Windows)
{
foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
{
for (var i = 1; i <= pane.Pages.Count; i++)
{
var bits = pane.Pages[i].EnhMetaFileBits;
var target =path1 + "_image.doc";
try
{
using (var ms = new MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var pngTarget = Path.ChangeExtension(target, "png");
image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception ex)
{ }
}
}
}
myWordDoc.Close(Type.Missing, Type.Missing, Type.Missing);
myWordApp.Quit(Type.Missing, Type.Missing, Type.Missing);