c#调用office将word转pdf
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using O2S.Components.PDFRender4NET;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.IO;
using System.Text;
/*using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;*/
using System.Data;
using DTcms.Common;
namespace DTcms.Web.admin.download
{
public partial class edit : Web.UI.ManagePage
{
private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
private int channel_id;
private int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
string _action = DTRequest.GetQueryString("action");
this.channel_id = DTRequest.GetQueryInt("channel_id");
if (this.channel_id == 0)
{
JscriptMsg("频道参数不正确!", "back", "Error");
return;
}
if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
{
this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
this.id = DTRequest.GetQueryInt("id");
if (this.id == 0)
{
JscriptMsg("传输参数不正确!", "back", "Error");
return;
}
if (!new BLL.article().Exists(this.id))
{
JscriptMsg("信息不存在或已被删除!", "back", "Error");
return;
}
}
if (!Page.IsPostBack)
{
TreeBind(this.channel_id); //绑定类别
if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
{
ShowInfo(this.id);
}
}
}
/// <summary>
/// word转换PDF
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
private bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing);
result = true;
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch
{
result = false;
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}
using Aspose.Words; //去百度下载Aspose.Words.dll
Aspose.Words.Document doc = new Aspose.Words.Document(被转换文件的路径+名字);
doc.Save(保存pdf的路径+名字, SaveFormat.Pdf);
試試
public bool ConvertToPDF(object sourcePath, object targetPath)
{
bool result = false;
Object missing = System.Reflection.Missing.Value;
//创建一个名为WordApp的组件对象
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = null;
try
{
//创建一个名为WordDoc的文档对象并打开
doc = wordApp.Documents.Open(ref sourcePath, ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//设置保存的格式
object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
//保存为PDF
doc.SaveAs(ref targetPath, ref filefarmat, ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
result = true;
}
catch(Exception ex)
{
result = false;
_ErrMess = "\r\nError:" + ex.Message +
"\r\nSource:" + ex.Source +
"\r\nStackTrace:" + ex.StackTrace;
}
finally
{
//关闭文档对象
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
//推出组建
if (wordApp != null)
{
wordApp.Quit(ref missing, ref missing, ref missing);
wordApp = null;
}
}
return result;
}