利用Aspose文档转图片
通过使用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文档转图片的更多相关文章
- 利用iTextSharp组件给PDF文档添加图片水印,文字水印
最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了.以下做个记录: 首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来. 代码中添加引用为: using S ...
- Java解析word,获取文档中图片位置
前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...
- C#向PPT文档插入图片以及导出图片
PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版Power ...
- 判断pdf、word文档、图片等文件类型(格式)、大小的简便方法
判断pdf.word文档.图片等文件类型(格式).大小的简便方法 很久没发文了,今天有时间就写一下吧. 关于上传文件,通常我们都需要对其进行判断,限制上传的类型,如果是上传图片,我们甚至会把图片转化成 ...
- C# 替换Word文本—— 用文档、图片、表格替换文本
编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改.在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文 ...
- 富文本粘贴word文档内容图片处理
公司做的项目要用到文本上传功能. 网上找了很久,大部分都有一些不成熟的问题,终于让我找到了一个成熟的项目. 下面就来看看: 1.打开工程: 对于文档的上传我们需要知道这个项目是否符合我们的初衷. 运行 ...
- 向Docx4j生成的word文档添加图片和布局--第一部分
原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...
- PDF文档转换为图片、图片转成PDF 及PDF合并
简介 功能:PDF文档按每页转换成一张图片,一张图片转换成一张PDF 并将多张PDF合成一个多页的PDF文档. 经历:在各个网站上搜索始终出现各种问题,尤其是遇到引用的版本问题尤其头疼,不是不能适用当 ...
- icepdf和pdfbox转pdf文档为图片
icepdf转pdf文档为图片 首先导入icepdf jar包或maven pdfPath为pdf文件路径.pdfimgpsth为图片保存的路径 public static void icePdfIm ...
随机推荐
- HTML编程
通俗的解释:HTML是一个没有穿衣服的人 CSS是穿上了华丽衣服的人 JS是使这个人动起来 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万 ...
- Sql语句,先查询再插入一条语句完成。
if ( (select COUNT(*) from Hr where 考勤号码 = '149' and 日期时间 = '2015/7/3 12:00:26') = 0 )INSERT INTO [ ...
- [MSSQL2012]CUME_DIST函数
CUME_DIST函数以某列作为基准,计算其它行相对于基准行数据的比例.差距比例,比较容易理解 先看下测试数据 DECLARE @TestData TABLE( ID INT IDENTITY ...
- 初学者利用git 上传代码到Coding的简单操作步骤
1.首先登陆coding网站注册账号https://coding.net/ (注册完后需登陆邮箱激活邮件) 2.登陆刚注册的coding账号 ,添加项目 添加项目—〉输入项目名称—〉输入对项目的简单描 ...
- [读书笔记]C#学习笔记三: C#类型详解..
前言 这次分享的主要内容有五个, 分别是值类型和引用类型, 装箱与拆箱,常量与变量,运算符重载,static字段和static构造函数. 后期的分享会针对于C#2.0 3.0 4.0 等新特性进行. ...
- atititt.java定时任务框架选型Spring Quartz 注解总结
atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...
- Javascript入门学习
编程之道,程序员不仅仅要精通一门语言,而是要多学习几门. 本学习之源出自柠檬学院http://www.bjlemon.com/,特此声明. 第一课1:javascript的主要特点解释型:不需要编译, ...
- Android SQLite3工具常用命令行总结
Android SDK的tools目录下提供了一个sqlite3.exe工具,这是一个简单的sqlite数据库管理工具.开发者可以方便的使用其对sqlite数据库进行命令行的操作. 程序运行生成的*. ...
- javascript设计模式与开发实践阅读笔记(8)——观察者模式
发布-订阅模式,也叫观察者模式:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. 在JavaScript开发中,我们一般用事件模型来替代传统的观察者模式. ...
- Help Viewer 2.2 独立运行
"C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...