在VB.Net中将文档文件转换为PDF

在VB.Net中将文档文件转换为PDF,vb.net,pdf-generation,doc,windows-applications,Vb.net,Pdf Generation,Doc,Windows Applications,我有一个情况,我需要将文档文件转换为PDF文件。我正在用vb.net开发windows应用程序。如果可能的话,我也不想使用第三方dll。 有人能给我一些更多的想法吗?Microsoft另存为PDF和:Microsoft另存为XPS允许Microsoft Office Word 2007以PDF和XPS格式导出和保存文档 检查这些: 如果要使用第三方dll,请选中此线程:Microsoft另存为PDF和:Microsoft另存为XPS允许Microsoft Office Word 2007以P

我有一个情况,我需要将文档文件转换为PDF文件。我正在用vb.net开发windows应用程序。如果可能的话,我也不想使用第三方dll。 有人能给我一些更多的想法吗?

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

检查这些:

如果要使用第三方dll,请选中此线程:

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

检查这些:


如果要使用第三方dll,请检查此线程:

您可以使用Office Interop进行此操作。但最好使用像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;

您可以为此使用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;

您可以在我的代码中了解我的想法,我使用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()

您可以在我的代码中了解我的想法,我使用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()

太棒了。但我不想使用第三方dll。还有别的办法吗?如果我使用的是Microsoft.Office.Interop.Word,那么我需要在需要安装exe的每台电脑上安装Office。所以这在pc上是不可能的,这太棒了。但我不想使用第三方dll。还有别的办法吗?如果我使用的是Microsoft.Office.Interop.Word,那么我需要在需要安装exe的每台电脑上安装Office。所以这在pc上是不可能的。那么您采用了哪种解决方案来实现这一点,或者是下面的一种解决方案,或者是其他的解决方案?我正在使用下面答案中的第二种解决方案。我使用的是Microsoft.Office.Interop.Word。那么您采用了哪种解决方案来实现此功能,或者是下面的解决方案,或者是其他解决方案??我使用的是下面答案中的第二个解决方案。我正在使用Microsoft.Office.Interop.Word。