.net使用npoi把word转为pdf文件代码示例,可以使用其他的组件
时间: 2023-02-07 11:51:09 浏览: 334
可以使用 Aspose.Words for .NET 库将 Word 文档转换为 PDF。这是一个第三方库,你可以使用 NuGet 包管理器安装它。
下面是使用 Aspose.Words 转换 Word 文档为 PDF 的示例代码:
```
using Aspose.Words;
string inputFilePath = @"C:\Input.docx";
string outputFilePath = @"C:\Output.pdf";
// Load the document from the input file.
Document doc = new Document(inputFilePath);
// Save the document in PDF format.
doc.Save(outputFilePath, SaveFormat.Pdf);
```
如果你想使用其他的组件,Spire.Doc for .NET 和 NReco.PdfGenerator 也可以将 Word 文档转换为 PDF。
相关问题
.net使用NPOI把word转为pdf 的代码
可以使用下面的代码把 Word 文档转换为 PDF:
```csharp
using NPOI.XWPF.UserModel;
using NPOI.XWPF.Converter.Pdf;
// 读取 Word 文档
XWPFDocument doc = new XWPFDocument(FileInputStream inputStream);
// 写入 PDF
FileOutputStream outputStream = new FileOutputStream(new File(outputFile));
PdfOptions options = PdfOptions.Create();
PdfConverter.GetInstance().Convert(doc, outputStream, options);
// 关闭流
outputStream.Close();
inputStream.Close();
```
需要注意,这段代码只能在 .NET 中使用,且需要安装 NPOI 库。
.net使用npoi把word转为MigraDoc 代码示例
使用 NPOI 将 Word 转换为 MigraDoc 的代码示例如下:
```
using (var stream = new FileStream(wordFilePath, FileMode.Open, FileAccess.Read))
{
// 读取 Word 文档
XWPFDocument doc = new XWPFDocument(stream);
// 创建 MigraDoc 文档
Document migraDoc = new Document();
Section section = migraDoc.AddSection();
// 遍历所有段落
foreach (var paragraph in doc.Paragraphs)
{
// 创建 MigraDoc 段落
Paragraph migraParagraph = section.AddParagraph();
// 遍历段落中的所有文本
foreach (var text in paragraph.Runs)
{
// 创建 MigraDoc 文本
Text migraText = migraParagraph.AddText(text.ToString());
// 设置文本样式
migraText.Style = "Normal";
}
}
// 保存 MigraDoc 文档
migraDoc.Save(migraDocFilePath);
}
```
需要注意,上述代码仅将 Word 文档的文本内容转换为 MigraDoc 文档,不会转换文档中的其他元素,如图片、表格等。
希望这能帮到你。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)