通过使用Aspose您可以轻松的将您的文档转换成真正的图片格式,最好的保证您的内容将实际可见,与其他格式相比,它并不存在查看工具的安装问题。

准备工作:

1:下载Aspose组件包:http://download.csdn.net/detail/laoge/6931819

编写代码:

核心代码AsposeFileToImg,以下代码在文档页数超过100以上生成会变慢,页数越大生成越慢,在实际使用中请注意。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Aspose.Cells; namespace PdfOrImg
{
/// <summary>
/// 功能:
/// pdf,doc,docx,ppt,pptx,xls,xlsx文件转图片
/// 在当前文件下创建一个以文件名命名的文件夹,该文件夹的保存的是该文件生成是页图片
/// </summary>
public class AsposeFileToImg
{
private static string imageDirectoryPath = "";
public static void FileToImg(string filePath)
{
if (File.Exists(filePath))
{
FileInfo pdfFi = new FileInfo(filePath);
string filename = pdfFi.Name.Replace(pdfFi.Extension, "");
imageDirectoryPath = System.IO.Path.Combine(pdfFi.DirectoryName, filename);
if (!Directory.Exists(imageDirectoryPath))
{
Directory.CreateDirectory(imageDirectoryPath);
}
string a = Path.GetExtension(filePath).ToLower();
if (a == ".pdf")
{
PdfToImg(filePath);
}
else
{
if (a == ".doc" || a == ".docx")
{
DocToImg(filePath);
}
else
{
if (a == ".ppt")
{
PptToImg(filePath);
}
else if (a == ".pptx")
{
PptxToImg(filePath);
}
else
{
if (a == ".xls" || a == ".xlsx")
{
XlsToImg(filePath);
}
}
}
}
}
}
private static void PdfToImg(string filePath)
{
string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
FileStream docStream = new FileStream(filePath, FileMode.Open);
var pdf = new Aspose.Pdf.Document(docStream);
for (int i = 1; i < pdf.Pages.Count + 1; i++)
{
try
{
string imgpath = imageDirectoryPath + "/" + filename + "_" + i + ".jpg";
if (!File.Exists(imgpath))
{
using (FileStream imageStream = new FileStream(imgpath, FileMode.Create))
{
Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300);
Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 100); jpegDevice.Process(pdf.Pages[i], imageStream);
imageStream.Close();
}
}
}
catch (Exception)
{ }
}
}
private static void DocToImg(string filePath)
{
string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
var words = new Aspose.Words.Document(filePath); Aspose.Words.Saving.ImageSaveOptions imageSaveOptions =
new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg);
imageSaveOptions.Resolution = 128; int pageindex = 0;
int pagecount = words.PageCount;
for (int i = 1; i < pagecount + 1; i++)
{
try
{
string imgpath = imageDirectoryPath + "/" + filename + "_" + i + ".jpg";
if (!File.Exists(imgpath))
{
imageSaveOptions.PageIndex = pageindex;
words.Save(imgpath, imageSaveOptions);
pageindex++;
}
}
catch (Exception)
{ }
}
}
private static void PptToImg(string filePath)
{
string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
var ppt = new Aspose.Slides.Presentation(filePath);
string imgpath = imageDirectoryPath + "/" + filename + ".pdf";
if (!File.Exists(imgpath))
{
ppt.Save(imgpath, Aspose.Slides.Export.SaveFormat.Pdf);
}
PdfToImg(imgpath);
}
private static void PptxToImg(string filePath)
{
string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
var ppt = new Aspose.Slides.Pptx.PresentationEx(filePath);
string imgpath = imageDirectoryPath + "/" + filename + ".pdf";
if (!File.Exists(imgpath))
{
ppt.Save(imgpath, Aspose.Slides.Export.SaveFormat.Pdf);
}
PdfToImg(imgpath);
}
private static void XlsToImg(string filePath)
{
string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
Workbook book = new Workbook(filePath);
//创建一个图表选项的对象
Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imgOptions.ImageFormat = ImageFormat.Jpeg;
int count = book.Worksheets.Count;
for (int i = 0; i < count; i++)
{
//获取一张工作表
Worksheet sheet = book.Worksheets[i];
//创建一个纸张底色渲染对象
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.PageCount; j++)
{
string imgpath = imageDirectoryPath + "/" + filename + "_" + i + "_" + j + "_.jpg";
if (!File.Exists(imgpath))
{
sr.ToImage(j, imgpath);
}
}
}
}
}
}

调用代码:

using System;
using System.IO;
using System.Collections; namespace PdfOrImg
{
//Adobe Acrobat9.0
class Program
{
static void Main(string[] args)
{
Console.WriteLine("========文件转图片开始========");
try
{ ArrayList alist = new ArrayList();
alist.Add("temp_pdf.pdf"); alist.Add("temp_ppt.ppt");
alist.Add("temp_pptx.pptx"); alist.Add("temp_doc.doc");
alist.Add("temp_docx.docx"); alist.Add("temp_xls.xls");
alist.Add("temp_xlsx.xlsx"); for (int i = 0; i < alist.Count; i++)
{
try
{
string pdfFilePath = alist[i].ToString();
FileInfo pdfFi = new FileInfo(pdfFilePath);
string filepath = pdfFi.FullName; Console.WriteLine("正在转换" + pdfFilePath + "文件...");
AsposeFileToImg.FileToImg(filepath);
}
catch (Exception)
{ }
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("========文件转图片结束========");
Console.Read(); } }
}

运行情况:

根据文档名称建立同名的文件夹,并在文件夹中存放文档每一页的图片,图片命名格式:文件名_页码.jpg 效果如下:

获取代码:

http://download.csdn.net/detail/luomingui/9151083

利用Aspose文档转图片的更多相关文章

  1. 利用iTextSharp组件给PDF文档添加图片水印,文字水印

    最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了.以下做个记录: 首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来. 代码中添加引用为:   using S ...

  2. Java解析word,获取文档中图片位置

    前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...

  3. C#向PPT文档插入图片以及导出图片

    PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版Power ...

  4. 判断pdf、word文档、图片等文件类型(格式)、大小的简便方法

    判断pdf.word文档.图片等文件类型(格式).大小的简便方法 很久没发文了,今天有时间就写一下吧. 关于上传文件,通常我们都需要对其进行判断,限制上传的类型,如果是上传图片,我们甚至会把图片转化成 ...

  5. C# 替换Word文本—— 用文档、图片、表格替换文本

    编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改.在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文 ...

  6. 富文本粘贴word文档内容图片处理

    公司做的项目要用到文本上传功能. 网上找了很久,大部分都有一些不成熟的问题,终于让我找到了一个成熟的项目. 下面就来看看: 1.打开工程: 对于文档的上传我们需要知道这个项目是否符合我们的初衷. 运行 ...

  7. 向Docx4j生成的word文档添加图片和布局--第一部分

    原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...

  8. PDF文档转换为图片、图片转成PDF 及PDF合并

    简介 功能:PDF文档按每页转换成一张图片,一张图片转换成一张PDF 并将多张PDF合成一个多页的PDF文档. 经历:在各个网站上搜索始终出现各种问题,尤其是遇到引用的版本问题尤其头疼,不是不能适用当 ...

  9. icepdf和pdfbox转pdf文档为图片

    icepdf转pdf文档为图片 首先导入icepdf jar包或maven pdfPath为pdf文件路径.pdfimgpsth为图片保存的路径 public static void icePdfIm ...

随机推荐

  1. Java中FileOutputStream和FileInputStream使用例子

    package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...

  2. 横竖屏切换时Activity的生命周期

    设置横竖屏切换时Activity生命周期的属性设置,在清单文件中的Activity节点中设置.根据具体需求设置: 1.不设置Activity的android:configChanges时,切屏会重新调 ...

  3. 4.13-4.17c语言学习

    这周学习开始接触c语言,使用的工具是c-free5,主要是把之前的一些函数流程图通过编写代码实现运行,本周最后一天的作业是做简易的atm机运行逻辑程序,是在main主函数外附加使用void函数,其主要 ...

  4. PHP类与继承

    <?php class Person { private $name; private $age; function __construct($name,$age){ $this->nam ...

  5. linux下卸载mysql

    卸载mysql rpm -qa|grep -i mysql rpm -ev MySQL-server-4.0.14-0 MySQL-client-4.0.14-0 卸载后/var/lib/mysql中 ...

  6. 深入浅出话VC++(2)——MFC的本质

    一.引言 上一专题中,纯手动地完成了一个Windows应用程序,然而,在实际开发中,我们大多数都是使用已有的类库来开发Windows应用程序.MFC(Microsoft Foundation Clas ...

  7. SQL SERVER 2014 安装图解(含 SQL SERVER 2014 安装程序共享)

    开篇介绍 2015年1月1日,新的一年开始之际,本来应该好好做点有意义的事情来跨个年的.结果,老习惯 - 睡觉之前一定要折腾一下电脑,说干就干,给新到的 DELL 电脑装虚机,下载 SQL SERVE ...

  8. [ucgui] 对话框4——模式消息窗口

    >_<" 这里实现点击灰色窗口的按钮出现一个模式消息窗口,点击OK之后才能再聚焦到灰窗口:点击灰窗口除了按钮的地方,弹出一个非模式窗口.

  9. Vue学习笔记1

    目录 前言 1.vue和avalon一样,都不支持VM初始时不存在的属性 2.input元素中属性与v-model同时存在以属性为优先 3.VM中的函数放到data属性和methods属性中的区别,以 ...

  10. android-sdks/build-tools/17.0.0/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

    fedora 23:dnf install zlib.i686 libstdc++.i686