ASP.NET将WORD、PDF、PPT转为图片
ASP.NET将WORD、PDF、PPT转为图片在在ASP.NET网页中,有时需要将word、pdf、ppt文档在页面中以图片的形式显示,就需要将这些文档的每一页转换成一张对应的图片
一、实现方式一、使用Office COM组件 (不支持PDF文档)
1、要求用户的电脑上必须安装有微软的Office,可以通过.NET与Office COM组件的互操作(Interop)来操作Office文档
2、实现原理:通过COM互操作可以在内存中打开Office文档,然后可以访问文档的每一页,并且支持将任意一页的内容复制到粘贴板(以图的形式),这样,我们再将粘贴板上的内容保存为图片
3、实现代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using MSWord = Microsoft.Office.Interop.Word;
using MSPowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ESBasic.Office
...{
/**//// <summary>
/// OfficeScanner 用于将word文档和ppt文档中的页转换为图片。
/// </summary>
public class OfficeScanner
...{
Scan#region Scan
/**//// <summary>
/// 将一个word/powerpoint文档中的指定页转换为位图。
/// </summary>
public Bitmap[] Scan(string filePath)
...{
if (!File.Exists(filePath))
...{
throw new Exception("File is not exist !");
}
string[] ary = filePath.Split('.');
string extendName = ary[ary.Length - 1];
if (extendName == "doc" || extendName == "docx")
...{
return Scan4Word(filePath);
}
if (extendName == "ppt" || extendName == "pptx")
...{
return Scan4PowerPoint(filePath, null);
}
throw new Exception("OfficeScanner only Support Word file and PowerPoint file !");
}
Scan4PowerPoint#region Scan4PowerPoint
private Bitmap[] Scan4PowerPoint(string filePath, ICollection<int> pageIndexCollection)
...{
List<Bitmap> bmList = new List<Bitmap>();
MSPowerPoint.ApplicationClass pptApplicationClass = new MSPowerPoint.ApplicationClass();
try
...{
MSPowerPoint.Presentation presentation = pptApplicationClass.Presentations.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
int totalCount = presentation.Slides.Count;
if (pageIndexCollection != null)
...{
totalCount = pageIndexCollection.Count;
}
int index = 0;
foreach (MSPowerPoint.Slide slide in presentation.Slides)
...{
if (pageIndexCollection == null || pageIndexCollection.Contains(index))
...{
slide.Copy();
System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.MetafilePict))
...{
object obj = data.GetData(DataFormats.MetafilePict);
Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero);
//ClipboardMetafileHelper.SaveMetafile(metafile, string.Format("{0}.bmp" ,index));
Bitmap bm = new Bitmap(metafile);
bmList.Add(bm);
Clipboard.Clear();
}
}
++index;
}
presentation.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(presentation);
return bmList.ToArray();
}
catch (Exception ex)
...{
throw ex;
}
finally
...{
pptApplicationClass.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(pptApplicationClass);
}
}
#endregion
Scan4Word#region Scan4Word
private Bitmap[] Scan4Word(string filePath)
...{
//复制目标文件,后续将操作副本
string tmpFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\\\" + Path.GetFileName(filePath) + ".tmp";
File.Copy(filePath, tmpFilePath);
List<Bitmap> bmList = new List<Bitmap>();
MSWord.ApplicationClass wordApplicationClass = new MSWord.ApplicationClass();
wordApplicationClass.Visible = false;
object missing = System.Reflection.Missing.Value;
try
...{
object readOnly = false;
object filePathObject = tmpFilePath;
MSWord.Document document = wordApplicationClass.Documents.Open(ref filePathObject, ref missing,
ref readOnly, 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);
bool finished = false;
while (!finished)
...{
document.Content.CopyAsPicture(); //拷贝到粘贴板
System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.MetafilePict))
...{
object obj = data.GetData(DataFormats.MetafilePict);
Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero); //从粘贴板获取数据
Bitmap bm = new Bitmap(metafile.Width, metafile.Height);
using (Graphics g = Graphics.FromImage(bm))
...{
g.Clear(Color.White);
g.DrawImage(metafile, 0, 0, bm.Width, bm.Height);
}
bmList.Add(bm);
Clipboard.Clear();
}
object What = MSWord.WdGoToItem.wdGoToPage;
object Which = MSWord.WdGoToDirection.wdGoToFirst;
object startIndex = "1";
document.ActiveWindow.Selection.GoTo(ref What, ref Which, ref missing, ref startIndex); // 转到下一页
MSWord.Range start = document.ActiveWindow.Selection.Paragraphs[1].Range;
MSWord.Range end = start.GoToNext(MSWord.WdGoToItem.wdGoToPage);
finished = (start.Start == end.Start);
if (finished) //最后一页
...{
end.Start = document.Content.End;
}
object oStart = start.Start;
object oEnd = end.Start;
document.Range(ref oStart, ref oEnd).Delete(ref missing, ref missing); //处理完一页,就删除一页。
}
((MSWord._Document)document).Close(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(document);
return bmList.ToArray();
}
catch (Exception ex)
标签: