Introduction

This article explains a way to convert a Microsoft Word Document to PDF Format. The operation is very simple.

Steps

Choose Website -> Add Reference -> .NET -> Microsoft.Office.Interop.Word 12.0.0.0 and click on Ok.

Code

Namespace

using Microsoft.Office.Interop.Word;
private Microsoft.Office.Interop.Word.ApplicationClass MSWordDoc;
object UnknownType = Type.Missing;

To Open the Word Document

MSWordDoc.Documents.Open(ref InputLocation,  	//Input File Name Location
                ref UnknownType,    		// Conversion Conformation
                ref UnknownType,    		// Set ReadOnly Property
                ref UnknownType,    		// Add to the Recent Files
                ref UnknownType,    		// Document Password Setting
                ref UnknownType,    		// Password Template
                ref UnknownType,    		// Revert
                ref UnknownType,    		// Write Password to Document
                ref UnknownType,    		// Write Password Template
                ref UnknownType,    		// File Format
                ref UnknownType,    		// Encoding File
                ref UnknownType,    		// Visibility
                ref UnknownType,    		// To Open or Repair
                ref UnknownType,    		// Document Direction
                ref UnknownType,    		// Encoding Dialog
                ref UnknownType);   		// XML Text Transform

To Get Document in PDF Format

object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

To SaveAs the Document

MSWordDoc.ActiveDocument.SaveAs(ref OutputLocation, 	//Output File Location
            ref SavePDFFormat,    			// File Format
            ref UnknownType,    			// Comment to PDF File
            ref UnknownType,    			// Password
            ref UnknownType,    			// Add to Recent File
            ref UnknownType,    			// Write Password
            ref UnknownType,    			// ReadOnly Property
            ref UnknownType,    			// Original Font Embedding
            ref UnknownType,    			// Save Picture
    	    ref UnknownType,				// Saving Form Data
            ref UnknownType,    			// Save as AOVE Letter
            ref UnknownType,    			// Encoding
            ref UnknownType,    			// Inserting Line Breaks
            ref UnknownType,    			// Allow Substitution
            ref UnknownType,    			// Line Ending
            ref UnknownType);   			// Add BiDi Marks    

To Close the Document File

 MSWordDoc.Documents.Close(ref UnknownType, ref UnknownType, ref UnknownType);

To Exit the Word Application

MSWordDoc.Quit(ref UnknownType, ref UnknownType, ref UnknownType);

History

  • 3rd June 2011: Initial post
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"