.Net中Word转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;
}

}
}

posted on 2019-02-27 16:53  哦呵呵G  阅读(1394)  评论(0编辑  收藏  举报

导航