通过使用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. networkx的绘图中文显示方块问题

    修改matplotlibrc文件 font.family : sans-serif #打开该选项 font.sans-serif : Microsoft YaHei , Bitstream Vera ...

  2. Web大文件下载控件更新-Xproer.HttpDownloader

    资源下载:cab安装包(x86),cab安装包(x64),xpi安装包,crx安装包,nat安装包,exe安装包,开发文档,根证书,VC库,   更新时间:2016-08-19 版本号:1,2,56, ...

  3. python 静态方法,类方法 ,类的继承

    转自:  http://cowboy.1988.blog.163.com/blog/static/75105798201091141521583/ 1.关于定义类的一些奇特之处  今天在Python中 ...

  4. [原] XAF 如何基于业务规则禁用属性

    How to: Disable Property Editors Based on a Business Rule // Developer Express Code Central Example: ...

  5. UITableView表格视图、UITableView代理方法及应用

    一.基本知识点 UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列. 使用UITableView: 1.展示信 ...

  6. LINQ to SQL大全

    LINQ to SQL语句 (1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的 ...

  7. [算法]——全排列(Permutation)以及next_permutation

    排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...

  8. weblogic10.3.6 自动启动服务后停止的解决方案

    windows部署weblogic后,需要手动开启weblogic管理员服务器,即Start Admin Server for Weblogic Server Domain,不过这样的话每次重启或者不 ...

  9. jboss7(01)服务器开启和关闭命令

    1.简单开启服务器的命令:进入到 bin 目录下,输入 ./standalone.sh 命令. 这种开启服务器的方式有个缺点,当你的命令窗口关闭后,服务自动down了 2.让服务器开启后在后台运行:进 ...

  10. nginx location模块--匹配规则

    Location语法语法:location [=|~|~*|^~] /uri/ { … } = --> 开头表示精确匹配 ^~ --> 开头表示uri以某个常规字符串开头,理解为匹配url ...