.Net中World转PDF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aspose.Words;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace MyFilterTest.Tools
{
public class WorldTranslatePDF
{
public bool WordToPDF2(string sourcePath)
{
bool result = false;
Application application = new Application();
Microsoft.Office.Interop.Word.Document document = null;

try

{
application.Visible = false;

document = application.Documents.Open(sourcePath);

string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置

if (!File.Exists(PDFPath))//存在PDF,不需要继续转换

{
document.ExportAsFixedFormat(PDFPath, WdExportFormat.wdExportFormatPDF);
}
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
document.Close();
}
return result;
}
public bool WordToPDF1(string sourcePath)
{
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);
string targetPath = sourcePath.ToUpper().Replace(".DOCX", ".PDF");
doc.Save(targetPath, SaveFormat.Pdf);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}

}
}

上一篇:WebSocket是什么原理?为什么可以实现持久连接?


下一篇:配置动态加载模块和js分模块打包,生产环境和开发环境公共常量配置