代码如下:
/// <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文件