1

I have an situation where i need to convert Doc file into PDF file. I am devepoing windows application in vb.net. and also i don't want to user third party dll if possible. so can anyone give me some more idea ?

Brijesh Patel
  • 2,901
  • 15
  • 50
  • 73

4 Answers4

2
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)
JEL
  • 27
  • 2
2

You can use Office Interop for this. But it is better to use some managed library like 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;
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
  • its great. but i don't want to use third party dll. is there any other way to do? and if i am using Microsoft.Office.Interop.Word then i need to install office on each pc where i need to install my exe. so its not possible on pc. – Brijesh Patel Mar 19 '12 at 13:44
1

The 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS allow a Microsoft Office Word 2007 to export and save documents in the PDF and XPS formats.

Check these:
Saving Word 2007 Documents to PDF and XPS Formats
How to convert Word to PDF in asp.net

If you want to use Thirt party dll then check this SO thread: Converting MS Word Documents to PDF in ASP.NET

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

You can get idea in my code, I generate file from word template file save file as pdf using Office.Interop. Dont forget to add reference the 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()
John Mark
  • 73
  • 11