You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中使用PDFsharp或MigraDoc将DOCX文件转换为PDF?

如何在VB控制台应用中用MigraDoc/PDFsharp将DOCX转为PDF?

首先得给你理清一个关键点:PDFsharp和MigraDoc本身都不支持直接读取现成的DOCX文件并转成PDF。MigraDoc的定位是让你从头构建文档(比如用它的对象模型创建段落、表格、设置样式),然后导出成PDF或RTF——你之前看到的C#示例是用MigraDoc创建好文档后导出,根本不是处理已有DOCX的场景,难怪你照着改会出问题!

接下来给你两种可行的实现思路,按需选择:


思路1:用OpenXML SDK解析DOCX,再用MigraDoc重建导出PDF

这种方式需要你先读取DOCX里的内容(文本、样式、表格等),再把这些内容映射到MigraDoc的对象模型中,最后生成PDF。适合需要精细控制转换效果的场景,不过要自己处理DOCX的格式解析。

步骤&VB示例代码

首先要安装两个NuGet包:DocumentFormat.OpenXml(用来读DOCX)和MigraDoc.DocumentObjectModel+MigraDoc.Rendering(用来生成PDF)。

Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering

Private Sub ConvertDocxToPdf(docxPath As String, pdfOutputPath As String)
    ' 第一步:读取DOCX里的文本内容(这里只做了简单的段落读取,复杂格式需要额外处理)
    Dim docContent As New StringBuilder()
    Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(docxPath, False)
        Dim body As Body = wordDoc.MainDocumentPart.Document.Body
        For Each para In body.Elements(Of Paragraph)()
            docContent.AppendLine(para.InnerText)
            ' 这里可以扩展处理样式、字体、表格等,比如提取段落的对齐方式、字体大小等
        Next
    End Using

    ' 第二步:用MigraDoc创建新文档并填充内容
    Dim migraDoc As New Document()
    Dim section As Section = migraDoc.AddSection()
    Dim paragraph As Paragraph = section.AddParagraph(docContent.ToString())
    ' 可以自定义样式,比如设置字体和字号
    ' paragraph.Format.Font.Name = "微软雅黑"
    ' paragraph.Format.Font.Size = 12

    ' 第三步:渲染并保存为PDF
    Dim pdfRenderer As New PdfDocumentRenderer(True)
    pdfRenderer.Document = migraDoc
    pdfRenderer.RenderDocument()
    pdfRenderer.PdfDocument.Save(pdfOutputPath)
End Sub

思路2:用现成工具/库直接转换(更省心)

如果不想折腾DOCX的格式解析,推荐用封装好的方案:

方案A:用Office Interop(需要安装Microsoft Office)

适合桌面场景,直接调用Word的功能转PDF,代码简单:

Imports Microsoft.Office.Interop.Word

Private Sub ConvertDocxToPdfWithInterop(docxPath As String, pdfOutputPath As String)
    Dim wordApp As New Application()
    Dim doc As Document = Nothing
    Try
        doc = wordApp.Documents.Open(docxPath)
        ' 另存为PDF格式
        doc.SaveAs2(pdfOutputPath, WdSaveFormat.wdFormatPDF)
    Catch ex As Exception
        Console.WriteLine($"转换失败:{ex.Message}")
    Finally
        ' 务必关闭文档和Word进程,释放COM对象避免内存泄漏
        doc?.Close()
        wordApp.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp)
    End Try
End Sub

方案B:用无依赖的第三方库

比如DinkToPdf(依赖wkhtmltopdf)或者PdfSharpCore的扩展包,这类库不需要安装Office,适合服务器或无Office环境。


再说说你之前的MigraDoc困惑

你看到的那个C#示例是给WinForms的DocumentViewer控件用的,而你的是控制台应用,根本没有pagePreview这种UI控件——在控制台里用MigraDoc,直接创建Document对象,填充内容后渲染成PDF就行,完全不需要UI相关的东西,就像思路1里的示例那样。

总结一下:如果一定要用PDFsharp/MigraDoc生态,就得先解析DOCX再重建;如果想快速搞定,用Interop或者第三方转换库会省力很多。

内容的提问来源于stack exchange,提问作者shlomi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:40:03