写个word转pdf程序代码
时间: 2023-03-13 12:30:35 浏览: 82
这里有一个示例程序,可以用来将Word文档转换成PDF格式:using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace WordToPDF
{
public class Program
{
static void Main(string[] args)
{
// 创建Word应用程序实例
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
// 设置默认的打开模式为只读
wordApp.Visible = false;
wordApp.Options.CheckSpellingAsYouType = false;
wordApp.Options.CheckGrammarAsYouType = false;
wordApp.Options.CheckGrammarWithSpelling = false;
// 打开要转换的Word文档
object fileName = "source.doc";
object missing = System.Reflection.Missing.Value;
Document doc = wordApp.Documents.Open(ref fileName,
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,
ref missing);
// 指定输出文件的路径
object outputFileName = "target.pdf";
// 保存文档为PDF格式
doc.ExportAsFixedFormat(outputFileName,
WdExportFormat.wdExportFormatPDF, false,
WdExportOptimizeFor.wdExportOptimizeForPrint,
WdExportRange.wdExportAllDocument, 0, 0,
WdExportItem.wdExportDocumentContent, true, true,
WdExportCreateBookmarks.wdExportCreateNoBookmarks, true,
true, false);
// 关闭文档并退出Word应用程序
doc.Close(WdSaveOptions.wdDoNotSaveChanges);
wordApp.Quit();
}
}
}