利用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 ...
随机推荐
- Controller将Model数据传给View层,View层应该如何处理?
首先,我们在Model层中添加一个Person类. namespace MVCTest.Models{ public class Person { public string ...
- js的变量使用<bean><list:write>赋值时需加' '
script的变量使用<bean><list:write>赋值时需加' ',如:var code ='<bean:write name="target" ...
- commons-logging和log4j
1.Apache通用日志接口(commons-logging.jar)介绍 Apache Commons包中的一个,包含了日志功能,必须使用的jar包.这个包本身包含了一个Simple Logger, ...
- phpcms开启、关闭在线编辑模板的方法
打开 caches/configs/system.php 文件 找到 'tpl_edit'=> 0,//是否允许在线编辑模板 修改此行代码中的数字为 1或0 即可
- 制作SMD Package及SMD焊盘制作
1.设置公英制. 2.设置栅格点.在Etch里设置0.1MM. 3.封装包括以下内容:REF标识, Place bound层包括高度, silkscreen 层, 1pin标识及pin序, 中心坐标, ...
- ContentControl 与 ViewModel (一)
前阵子有人问我MVVM模式下,在View中嵌套View,切换View.想一想还是写下来吧. 主要就是用到 ContentControl 和 DataTemplate,这算是一种 ViewModel F ...
- 【转】URL的井号
去年9月,twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改版后,就变成了 ...
- [安卓] 7、页面跳转和Intent简单用法
这里有一个layout资源,2个activity.首先在MainActivity.java中实例化按钮和添加按钮监听绑定都是我们知道的,这里要注意的是第22行Intent intent = new I ...
- Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决(续)
作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2013/12/17/dro ...
- 使用ThreadSanitizer线程检查工具
ThreadSanitizer又叫TSan,是一个检查线程Data Race的C/C++工具.它集成在新版的gcc和clang中,通过编译时加-fsanitize=thread,可以在运行时检测出Da ...