问题描述
- c# 百思不得其解:word一页得到的图片与原始大小不一致,有源码有数据,请测试
-
根据网上的一些大神的代码及帮助文档,做了一个word转图片的程序.简要测试一下,发现转换出来的图片跟原始文档的页内容不一致.请教一下各位,有没有什么思路之类的,十分感谢.
解决方案
解决方案二:
代码如下:
``` class Program
{
static void Main(string[] args)
{
//运行13版本word,请改一下引用的两个dll到dll的15版本,图片是透明的,想要不透明,转换成jpg即可
ConvertWord2Png("c:\2.docx", "d:\");
Console.WriteLine("已完成");
Console.ReadKey();
}
private static void ConvertWord2Png(string wordFile,string destinationFileDirectory)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Document doc = null;
object unknow = Type.Missing;
app.Visible = false;
object file = wordFile;
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
//获取总页数
int pages = doc.ComputeStatistics(WdStatistic.wdStatisticPages, ref unknow);
object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToNext;
for (int i = 1; i < pages + 1; i++)
{
object page = i + ""; // 页数
Microsoft.Office.Interop.Word.Range ran1 = doc.GoTo(ref What, ref Which, ref page, ref unknow);
Microsoft.Office.Interop.Word.Range ran2 = ran1.GoToPrevious(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
object start = ran2.End;
object end = ran1.Start;
Range ran3 = doc.Range(ref start, ref end);
if (i == pages)//最后一页处理
{
ran3 = doc.Range(ref end);
}
string x = ran3.Text;
SaveRangeToPng(ran3, destinationFileDirectory+ i + ".png");
}
doc.Close();
}
/// <summary>
/// 把word文档的一个范围存储成png图片
/// </summary>
/// <param name="range"></param>
private static void SaveRangeToPng(Range range, string saveFilePath)
{
byte[] bt = (byte[])range.EnhMetaFileBits;
FileStream fs = new FileStream(saveFilePath, FileMode.Create);
fs.Write(bt, 0, bt.Count());
fs.Close();
//用range.copytoPicture方法内存中找不到,所以用直接储存方式。
}
}
解决方案三:
代码如下:
``` class Program
{
static void Main(string[] args)
{
//运行13版本word,请改一下引用的两个dll到dll的15版本,图片是透明的,想要不透明,转换成jpg即可
ConvertWord2Png("c:\2.docx", "d:\");
Console.WriteLine("已完成");
Console.ReadKey();
}
private static void ConvertWord2Png(string wordFile,string destinationFileDirectory)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Document doc = null;
object unknow = Type.Missing;
app.Visible = false;
object file = wordFile;
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
//获取总页数
int pages = doc.ComputeStatistics(WdStatistic.wdStatisticPages, ref unknow);
object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToNext;
for (int i = 1; i < pages + 1; i++)
{
object page = i + ""; // 页数
Microsoft.Office.Interop.Word.Range ran1 = doc.GoTo(ref What, ref Which, ref page, ref unknow);
Microsoft.Office.Interop.Word.Range ran2 = ran1.GoToPrevious(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
object start = ran2.End;
object end = ran1.Start;
Range ran3 = doc.Range(ref start, ref end);
if (i == pages)//最后一页处理
{
ran3 = doc.Range(ref end);
}
string x = ran3.Text;
SaveRangeToPng(ran3, destinationFileDirectory+ i + ".png");
}
doc.Close();
}
/// <summary>
/// 把word文档的一个范围存储成png图片
/// </summary>
/// <param name="range"></param>
private static void SaveRangeToPng(Range range, string saveFilePath)
{
byte[] bt = (byte[])range.EnhMetaFileBits;
FileStream fs = new FileStream(saveFilePath, FileMode.Create);
fs.Write(bt, 0, bt.Count());
fs.Close();
//用range.copytoPicture方法内存中找不到,所以用直接储存方式。
}
}
解决方案四:
测试数据是百度文库的一个word文档(),
http://wenku.baidu.com/view/4ac2f0c38bd63186bcebbc0b.html?from=search
环境是,office2007,vs2010.
解决方案五:
用Aspose.Word不就可以了么,非得自己用Office组件?
解决方案六:
我觉得用截屏来的更方便,而且也不会有这些问题
时间: 2024-11-02 16:08:39