PDF文件转图片,网上有很多方式。这里我主要说说 以下两种方式:office、Ghostscript和O2S.Components.PDFRender4NET.dll(主要是因为其它方式尝试了但是没用达到预期的效果)。
PPT文件与图片相互转换用使用 office 组件。
首先项目添加引用这两个dll,注意两个版本要一致
/// <summary>
/// 使用office组件将PPT转图片
/// </summary>
/// <param name="pptPath">ppt路径</param>
/// <param name="imgPath">图片存放路径</param>
public void pptToImg(string pptPath, string imgPath)
{
try
{
//从流加载PDF文件
//byte[] pdfstream = File.ReadAllBytes(pptPath);
//FileStream pdfstream = new FileStream(pptPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
//if (pdfstream.Length > 1024 * 1024 * 30)
//{
// MessageBox.Show("文件大于30M,无法展示!");
// return;
//}
//pdfstream.Dispose();
labelLoadPage.Text = "正在加载 ...";
//调用office组件
Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
var ppt = app.Presentations.Open(pptPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
var index = 0;
//var fileName = Path.GetFileNameWithoutExtension(pptPath);
foreach (Microsoft.Office.Interop.PowerPoint.Slide slid in ppt.Slides)
{
//设置图片大小
slid.Export(imgPath, "jpg", this.Width, this.Height);
//不设置大小,使用PPT文件中的尺寸
//slid.Export(imgPath, "jpg");
Bitmap tempimg = ReadImageFile(imgPath);
//处理tempimg
++index;
}
//释放资源
ppt.Close();
app.Quit();
GC.Collect();
}
catch (Exception e)
{
if (e.Message.Contains("正由另一进程使用"))
{
MessageBox.Show(e.Message);
}
else
{
MessageBox.Show("当前环境文件无法转换,请打开pdf格式文件!");
}
}
}
/// <summary>
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Bitmap ReadImageFile(string path)
{
if (!File.Exists(path))
{
return null;//文件不存在
}
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
Bitmap bit = new Bitmap(result);
return bit;
}
因为需求需要在图片上进行类似批注功能,再把修改的图片保存成PPT,所以这里一并分享一下(图片保存代码块要根据自己的代码稍微修改一下)。
/// <summary>
/// 图片保存为PPT
/// </summary>
/// <param name="path">PPT文件存储更目录</param>
public void savePPT(string path)
{
int width = 0, height = 0;
string datatimename = (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()).ToString();
//保存为ppt格式
Microsoft.Office.Interop.PowerPoint.Application PPT = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation MyPres = null;//PPT应用的实例
Microsoft.Office.Interop.PowerPoint.Slide MySlide = null;//PPT中的幻灯片
//创建一个ppt文件
File.Create(path + "\\" + filename + datatimename + ".pptx").Close();
//此处将一个PPT实例给了MyPres
MyPres = PPT.Presentations.Open(path + "\\" + filename + datatimename + ".pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
for (int i = 0; i < CommandTool.listDraware_page.Count; i++)
{
#region 程序处理的图片保存
//width = tempBitmap.Width;
//height = tempBitmap.Height;
width = CommandTool.listDraware_page[i].BackgroundImage.Width;
height = CommandTool.listDraware_page[i].BackgroundImage.Height;
//Bitmap tempBitmap = KiResizeImage((Bitmap)CommandTool.listDraware_page[i].BackgroundImage, this.Width, this.Height);
CommandTool.listDraware_page[i].layer.Graphics.setupBitmap((Bitmap)CommandTool.listDraware_page[i].BackgroundImage);
CommandTool.listDraware_page[i].Refresh();
CommandTool.listDraware_page[i].layer.Graphics.bp.Save(Application.StartupPath + "\\test.jpg", ImageFormat.Jpeg);
#endregion
//向PPT实例中,添加一个空白页,位置是“第一页”
if (i == 0 && MyPres.Slides.Count > 0)
{
MySlide = MyPres.Slides[1];
}
else
{
MySlide = MyPres.Slides.Add(i + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);
}
//MySlide.Shapes.AddPicture(Application.StartupPath + "\\test.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, width * 72 / 96, height * 72 / 96);//官网最后两个参数单位是磅,把像素转磅效果不佳
MySlide.Shapes.AddPicture(Application.StartupPath + "\\test.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, width / 2, height / 2);//测试发现 width / 2 效果还不错,我就用了这个写法
}
MyPres.Save();
MyPres.Close();
PPT.Quit();
File.Delete(Application.StartupPath + "\\test.jpg");
}
Ghostscript
首先准备gsdll32.dll和gswin32c.exe文件百度网盘下提取码:ywu4、原地址下载
把文件放置项目根目录,方便使用时调用,更多的使用命令自行百度。
private void button14_Click(object sender, EventArgs e)
{
System.Diagnostics.Process gscProcess = new System.Diagnostics.Process();
gscProcess.StartInfo.WorkingDirectory = Application.StartupPath;//程序所处根目录
gscProcess.StartInfo.FileName = @"\gswin32c.exe";//程序名根据下载版本修改
gscProcess.StartInfo.Arguments = @"-q -dSAFER -dBATCH -dNOPAUSE -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sDEVICE=jpeg -sOutputFile=D:\PPT\%d.jpg D:\PPT\微立体融资.pdf";
//窗口隐藏运行
gscProcess.StartInfo.UseShellExecute = true;//关键代码
gscProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//关键代码
gscProcess.Start();
gscProcess.WaitForExit();
}
O2S.Components.PDFRender4NET.dll
这个就是dll不好找,dll下载提取码: b00t;提供下载的dll版本是2.5.4.0比较早的一个版本,没有水印,但是较大的pdf文件会报错(测试五十多兆的pdf dll会报错无法转图片),代码没什么难度自行百度吧。
总结
如果有金主要用第三方的dll(Acrobat.dll,Components.PDFRender4NET.dll),不建议找破解版,我们应该支持版权;如果想了解更多的方式请点击跳转;个人感觉还是office的组件更强大些(已知缺点就是电脑必须安装WPS)。