我有一个 VB 控制台应用程序。 今天我使用 PDFsharp (http://www.pdfsharp.net/) 来合并 PDF 文件 像这样:
Private Sub mergePdfFiles(pdfFiles As String(), output As String)
Dim out As PdfDocument = New PdfDocument
Try
For Each file As String In pdfFiles
Dim inputDoc As PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(file,IO.PdfDocumentOpenMode.Import)
Dim count As Integer = inputDoc.PageCount
For idx As Integer = 0 To count - 1
Dim page As PdfPage = inputDoc.Pages(idx)
out.AddPage(page)
Next
Next
out.Save(output)
Catch ex As Exception
End Try
End Sub
这个效果很好。 现在我想将DOCX文件转换为PDF,但PDFsharp似乎无法做到这一点。 我知道 MigraDoc 可以做到
MigraDoc Foundation 是开源 .NET 库,可基于包含段落、表格、样式等的对象模型轻松创建文档,并将其呈现为 PDF 或 RTF。
但我没能做到。示例不是很清楚,适用于 C#。 http://www.pdfsharp.net/wiki/DocumentViewer-sample.ashx
例如:
//create RTF file From document
private void miRtf_Click(object sender, System.EventArgs e)
{
RtfDocumentRenderer rtf = new RtfDocumentRenderer();
rtf.Render(this.pagePreview.Document, "test.rtf", null);
Process.Start("test.rtf");
}
在 VB 中,我尝试使用
Me
而不是 this
,但我没有 pagePreview。
如何在 VB 中使用 MigraDoc 或 PDFsharp 将 DOCX 文件转换为 PDF 文件?
只要您可以解析输入文件,您就可以将任何文件转换为 MigraDoc。
您必须编写代码来读取 DOCX 文件并从中提取表格、文本等。
MigraDoc 文档没有说 MigraDoc 可以导入 RTF 或 DOC 或 DOCX 或 HTML。您可以使用 MigraDoc 创建新文档。