Aspose相关操作代码
2015-08-04 10:48
561 查看
代码如下:
/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool XLSConvertToPDF(string sourcePath, string targetPath, out string OutPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
OutPath = targetPath;
}
return result;
}
/// <summary>
/// WinWord文件生成pdf并保存
/// </summary>
/// <param name="FilePath">需要生成的word文件的路径</param>
/// <param name="saveFilePath">生成以后保存HTML文件的路径</param>
/// <returns>是否生成成功,成功为true,反之为false</returns>
public static bool GenerationWordToPDF(string FilePath, string saveFilePath, out string OutPath)
{
object tempFileName = FilePath;
object savePath = saveFilePath;
FileInfo fi = new FileInfo(FilePath);
string astdt = fi.Extension;
object strFileName = fi.Name;
object flg = false;
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//oWord.Visible = true;
oDoc = oWord.Documents.Open(ref tempFileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
try
{
// if (!Directory.Exists(savePath.ToString()))
// {
// Directory.CreateDirectory(savePath.ToString());
// }
savePath = saveFilePath;//+ strFileName + "." + astdt;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
oDoc.SaveAs(ref savePath, ref format,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
catch (Exception ex)
{
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
throw (ex);
}
OutPath = saveFilePath;
return true;
}
/// <summary>
/// 将PPt转换为PDF文件
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath, out string OutPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
OutPath = targetPath;
return result;
}
public enum OfficeDocumentType
{
Word,
Excel,
PowerPoint,
}
public static bool OfficeConvertToPDF(string sourcePath, string targetPath, OfficeDocumentType documentType)
{
bool result = false;
try
{
switch (documentType)
{
case OfficeDocumentType.Word:
new Aspose.Words.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
result = true;
break;
case OfficeDocumentType.Excel:
new Aspose.Cells.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Cells.Workbook xls = new Aspose.Cells.Workbook(sourcePath);
xls.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
result = true;
break;
case OfficeDocumentType.PowerPoint:
new Aspose.Slides.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(sourcePath);
ppt.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);
result = true;
break;
default: break;
}
}
catch
{
}
return result;
}
public enum Definition
{
One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
}
/// <summary>
/// 将PDF文档转换为图片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">生成图片的名字</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
public static bool ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition, string FileType, out int page)
{
//if(!File.Exists(imageOutputPath+imageName+"1.Png"))
//{
if (FileType.ToLower().ToString().Trim() == ".doc")
{
if (!GenerationWordToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
if (FileType.ToLower().ToString().Trim() == ".xls")
{
if (!XLSConvertToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
if (FileType.ToLower().ToString().Trim() == ".ppt")
{
if (!PPTConvertToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
PDFFile pdfFile = null;
try
{
pdfFile = PDFFile.Open(pdfInputPath);
page = pdfFile.PageCount;
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// start to convert each page
if (!File.Exists(imageOutputPath + imageName + "_1.Png"))//不存在图片才转换
{
for (int i = 1; i <= pdfFile.PageCount; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
}
pdfFile.Dispose();
File.Delete(imageOutputPath + imageName + ".pdf");
}
catch
{
page = 0;
pdfFile.Dispose();
}
//}
return true;
实现方式2:
//1.选择pdf文件
var dialog = new OpenFileDialog();
dialog.Filter = "pdf文件|*.pdf";
var dialogResult = dialog.ShowDialog();
if (dialogResult != System.Windows.Forms.DialogResult.OK) {
return;
}
//和选择的文件并列创建一个目录
string filePath = dialog.FileName;
string directoryPath = filePath + "目录";
//aspose许可证
Aspose.Pdf.License l = new Aspose.Pdf.License();
string licenseName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Aspose.Total.Product.Family.lic");
l.SetLicense(licenseName);
//定义Jpeg转换设备
Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
var device = new Aspose.Pdf.Devices.JpegDevice();
int quality = int.Parse(this.comboBox1.SelectedItem.ToString());
directoryPath += quality;
Directory.CreateDirectory(directoryPath);
//默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩
device = new Aspose.Pdf.Devices.JpegDevice(quality);
//遍历每一页转为jpg
for (var i = 1; i <= document.Pages.Count; i++) {
string filePathOutPut = Path.Combine(directoryPath, string.Format("{0}.jpg", i));
FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);
try {
device.Process(document.Pages[i], fs);
fs.Close();
} catch (Exception ex) {
fs.Close();
File.Delete(filePathOutPut);
}
代码如下:
/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool XLSConvertToPDF(string sourcePath, string targetPath, out string OutPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
OutPath = targetPath;
}
return result;
}
/// <summary>
/// WinWord文件生成pdf并保存
/// </summary>
/// <param name="FilePath">需要生成的word文件的路径</param>
/// <param name="saveFilePath">生成以后保存HTML文件的路径</param>
/// <returns>是否生成成功,成功为true,反之为false</returns>
public static bool GenerationWordToPDF(string FilePath, string saveFilePath, out string OutPath)
{
object tempFileName = FilePath;
object savePath = saveFilePath;
FileInfo fi = new FileInfo(FilePath);
string astdt = fi.Extension;
object strFileName = fi.Name;
object flg = false;
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//oWord.Visible = true;
oDoc = oWord.Documents.Open(ref tempFileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
try
{
// if (!Directory.Exists(savePath.ToString()))
// {
// Directory.CreateDirectory(savePath.ToString());
// }
savePath = saveFilePath;//+ strFileName + "." + astdt;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
oDoc.SaveAs(ref savePath, ref format,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
catch (Exception ex)
{
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
throw (ex);
}
OutPath = saveFilePath;
return true;
}
/// <summary>
/// 将PPt转换为PDF文件
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath, out string OutPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
OutPath = targetPath;
return result;
}
public enum OfficeDocumentType
{
Word,
Excel,
PowerPoint,
}
public static bool OfficeConvertToPDF(string sourcePath, string targetPath, OfficeDocumentType documentType)
{
bool result = false;
try
{
switch (documentType)
{
case OfficeDocumentType.Word:
new Aspose.Words.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
result = true;
break;
case OfficeDocumentType.Excel:
new Aspose.Cells.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Cells.Workbook xls = new Aspose.Cells.Workbook(sourcePath);
xls.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
result = true;
break;
case OfficeDocumentType.PowerPoint:
new Aspose.Slides.License().SetLicense(@"../Lic/aspose.lic");
Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(sourcePath);
ppt.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);
result = true;
break;
default: break;
}
}
catch
{
}
return result;
}
public enum Definition
{
One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
}
/// <summary>
/// 将PDF文档转换为图片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">生成图片的名字</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
public static bool ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition, string FileType, out int page)
{
//if(!File.Exists(imageOutputPath+imageName+"1.Png"))
//{
if (FileType.ToLower().ToString().Trim() == ".doc")
{
if (!GenerationWordToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
if (FileType.ToLower().ToString().Trim() == ".xls")
{
if (!XLSConvertToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
if (FileType.ToLower().ToString().Trim() == ".ppt")
{
if (!PPTConvertToPDF(pdfInputPath, imageOutputPath + imageName + ".pdf", out pdfInputPath))
{
page = 0;
return false;
}
}
PDFFile pdfFile = null;
try
{
pdfFile = PDFFile.Open(pdfInputPath);
page = pdfFile.PageCount;
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// start to convert each page
if (!File.Exists(imageOutputPath + imageName + "_1.Png"))//不存在图片才转换
{
for (int i = 1; i <= pdfFile.PageCount; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
}
pdfFile.Dispose();
File.Delete(imageOutputPath + imageName + ".pdf");
}
catch
{
page = 0;
pdfFile.Dispose();
}
//}
return true;
实现方式2:
//1.选择pdf文件
var dialog = new OpenFileDialog();
dialog.Filter = "pdf文件|*.pdf";
var dialogResult = dialog.ShowDialog();
if (dialogResult != System.Windows.Forms.DialogResult.OK) {
return;
}
//和选择的文件并列创建一个目录
string filePath = dialog.FileName;
string directoryPath = filePath + "目录";
//aspose许可证
Aspose.Pdf.License l = new Aspose.Pdf.License();
string licenseName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Aspose.Total.Product.Family.lic");
l.SetLicense(licenseName);
//定义Jpeg转换设备
Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
var device = new Aspose.Pdf.Devices.JpegDevice();
int quality = int.Parse(this.comboBox1.SelectedItem.ToString());
directoryPath += quality;
Directory.CreateDirectory(directoryPath);
//默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩
device = new Aspose.Pdf.Devices.JpegDevice(quality);
//遍历每一页转为jpg
for (var i = 1; i <= document.Pages.Count; i++) {
string filePathOutPut = Path.Combine(directoryPath, string.Format("{0}.jpg", i));
FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);
try {
device.Process(document.Pages[i], fs);
fs.Close();
} catch (Exception ex) {
fs.Close();
File.Delete(filePathOutPut);
}
相关文章推荐
- 直接在线预览Word、Excel、TXT文件之ASP.NET
- ASP.NET - 上传图片方法(单张)
- asp:Repeater数据排序
- ABP(现代ASP.NET样板开发框架)系列之18、ABP应用层——权限验证
- ASP.NET - 跳转页面
- 用Jasper report实现横向分栏报表
- 数独解算器(ASP.NET 2.0)
- 详解ASP.NET七大身份验证方式以及解决方案
- ASP.NET MVC 小牛之旅3:Routing——网址路由
- 维翔主机asp主机使用遇到的问题及解决方案总结
- ASP.NET MVC5 插件机制中插件的简单实现
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- ABP(现代ASP.NET样板开发框架)系列之17、ABP应用层——参数有效性验证
- asp.net之生成验证码的方法集锦(一)
- Asp.net 5学习
- ASP.NET MVC Cookie 身份验证
- asp.net 5 新特性简述(学习笔记)
- ABP(ASP.NET样板项目)的学习资料链接
- asp.net mvc中加入log4net记录错误日志
- ASP.NET5,MVC 6,Beta 7与VS 2015 RTM的兼容问题