Aspose相关操作代码
来源:互联网 发布:淘宝伊人靓妆是正品吗 编辑:程序博客网 时间:2024/06/06 01:24
首先必须有这几个dll文件:
代码如下:
/// <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文件
- Aspose相关操作代码
- Aspose相关操作代码
- ASPOSE.WORD操作
- 单链表相关操作代码
- 图片操作相关代码
- .net使用Aspose.Words进行Word替换操作的实现代码
- Aspose操作Excel和Word
- Aspose.World 操作使用教程
- Aspose.Cells.dll操作exel
- Aspose.Words 操作 Word文件
- Aspose打印Excel方法代码
- jsr75相关操作代码新版本
- poi----操作word。相关代码
- js 表格相关操作代码
- android文件操作相关代码
- 操作MySQL数据库相关代码
- Jquery 操作table 相关代码
- JS时间相关操作代码
- 三角类2
- 数学库中应用SSE
- $_GET['goods_id']+0
- 第五周上机任务项目一 长方体
- PHP安装新的扩展库
- Aspose相关操作代码
- java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column
- 春意何时带来生机
- requestWindowFeature()的应用
- 圆角层样式
- mysql 游标使用
- android33——意图过滤器
- servlet 读取相对路径 读取本地文件
- 第五周项目一----通过增加构造函数使对象在定义时初始化(4种方法统编)