【问题标题】:Convert Doc file to PDF in VB.Net在 VB.Net 中将 Doc 文件转换为 PDF
【发布时间】:2012-04-03 23:32:37
【问题描述】:

我遇到需要将 Doc 文件转换为 PDF 文件的情况。我正在 vb.net 中开发 Windows 应用程序。如果可能的话,我也不想使用第三方 dll。 那么谁能给我更多的想法?

【问题讨论】:

  • 那么您采用了哪种解决方案来实现这一点,无论是以下一种还是其他??
  • 我正在使用以下答案中的第二个。我正在使用 Microsoft.Office.Interop.Word。

标签: vb.net pdf-generation doc windows-applications


【解决方案1】:

您可以为此使用 Office 互操作。但最好使用一些托管库,如 Aspose

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;

【讨论】:

  • 它很棒。但我不想使用第三方 dll。还有其他方法吗?如果我使用的是 Microsoft.Office.Interop.Word,那么我需要在每台需要安装我的 exe 的电脑上安装 Office。所以在电脑上是不可能的。
【解决方案2】:

2007 Microsoft Office Add-in:Microsoft 另存为 PDF 和 2007 Microsoft Office Add-in:Microsoft 另存为 XPS 允许 Microsoft Office Word 2007 以 PDF 和 XPS 格式导出和保存文档。

检查这些:
Saving Word 2007 Documents to PDF and XPS Formats
How to convert Word to PDF in asp.net

如果您想使用第三方 dll,请查看此 SO 线程:Converting MS Word Documents to PDF in ASP.NET

【讨论】:

    【解决方案3】:
    Imports Microsoft.Office.Interop
    
    'This code happens to be loading a template, but it isn't necessary...
    
    'Opens Word Application
    
    Dim MyApp As New Word.Application
    
    'Opens new WordDoc
    
    Dim MyWordDoc As Word.Document = MyApp.Documents.Add(template)
    
    MyApp.Visible = True
    
    MyWordDoc = MyApp.ActiveDocument
    
    'code to fill doc
    
    'code to fill doc
    
    'code to fill doc
    
    MyWordDoc.SaveAs(FileLocation, Word.WdSaveFormat.wdFormatPDF)
    

    【讨论】:

      【解决方案4】:

      您可以在我的代码中得到想法,我使用 Office.Interop 从 word 模板文件将文件保存为 pdf 生成文件。不要忘记添加引用 office.Interop.Word

          sFileName = "billing"
          wdApp = New Word.Application
          wdDocs = wdApp.Documents
      
          Dim wdDoc As Word.Document = wdDocs.Add(sPath & "template.dotx")
          Dim wdBooks As Word.Bookmarks = wdDoc.Bookmarks
          Dim wdTable As Word.Table
      
      
          Dim r As Integer, c As Integer
          wdTable = wdDoc.Tables.Add(wdDoc.Bookmarks.Item("bkTable").Range, 3, 6)
          Dim rowCOunt As Integer = dgvSample.Rows.Count, colCount As Integer = dgvSample.Columns.Count
      
          'DATAGRIDVIEW TO WORDTABLE
          For r = 1 To rowCOunt
              For c = 1 To colCount
                  wdTable.Cell(r, c).Range.Text = dgvSample.Rows(r - 1).Cells(c - 1).Value
              Next
          Next
      
          wdTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
          wdTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
      
          wdBooks("bkClient_name").Range.Text = txtClient.Text.ToString
          wdBooks("bkDate").Range.Text = dtpDate.Text.ToString
          wdDoc.SaveAs2(sPath & sFileName & ".pdf", Word.WdSaveFormat.wdFormatPDF)
      
          ReleaseObject(wdBooks)
          wdDoc.Close(False)
          ReleaseObject(wdDoc)
          ReleaseObject(wdDocs)
          wdApp.Quit()
      

      【讨论】:

        猜你喜欢
        • 2012-04-14
        • 1970-01-01
        • 1970-01-01
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        • 1970-01-01
        相关资源
        最近更新 更多