.net用iTextSharp把word文件转为pdf的代码
时间: 2023-02-01 20:02:51 浏览: 162
你可以使用以下代码将 Word 文件转换为 PDF:
```
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Microsoft.Office.Interop.Word;
using System;
using System.IO;
namespace WordToPdf
{
class Program
{
static void Main(string[] args)
{
// 定义输入输出文件路径
string inputFilePath = @"C:\input.docx";
string outputFilePath = @"C:\output.pdf";
// 创建一个 Word 应用程序实例
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
// 打开要转换的 Word 文件
Document doc = appWord.Documents.Open(inputFilePath);
// 设置输出的 PDF 文件的保存路径
object outputFile = outputFilePath;
// 转换 Word 文件为 PDF 格式
doc.SaveAs(ref outputFile, WdSaveFormat.wdFormatPDF);
// 关闭 Word 应用程序
appWord.Quit();
}
}
}
```
需要注意的是,在使用 iTextSharp 库之前,你需要将它添加到项目的引用中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)