Converting word doc file to pdf with using Microsoft.Office.Interop.Word Dll

Karthikreddy
Posted by Karthikreddy under ASP.NET category on | Points: 40 | Views : 8008
1.open the visual studio 2010 and add the references
using Microsoft.Office.Interop.Word;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
2.drag and drop one fileupload control,button and label
3.write the following code in the code behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void ConvertTopdf(string path,string filename)
{
Microsoft.Office.Interop.Word.ApplicationClass wordApplication = null;
Microsoft.Office.Interop.Word.Document wordDocument = null;

// Declare a variable for the path of the file to convert.
string paramSourceDocPath = path;

// Declare variables for the Document.ExportAsFixedFormat method parameters.
string paramExportFilePath = @"I:\karthik\WebSite8\pdf\"+filename;
Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
//wdExportFormatXPS
bool paramOpenAfterExport = false;
Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
Int32 paramStartPage = 0;
Int32 paramEndPage = 0;
Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentWithMarkup;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
try
{
// Start an instance of Word.
wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();

// Open the source document.
wordDocument = wordApplication.Documents.Open(paramSourceDocPath);

// Export it in the specified format.if (wordDocument != null)
{
wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM,
paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1);
}
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
// Close and release the Document object.if (wordDocument != null)
{
// wordDocument.Close(false);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.if (wordApplication != null)
{
wordApplication.Quit(false);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string extention = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (extention.ToLower() == ".doc" || extention.ToLower() == ".docx")
{
string path = Convert.ToString(System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName));
string filename = TextBox1.Text;
FileUpload1.SaveAs(@"I:\karthik\doc2pdf\TempDoc1\" + filename);

ConvertTopdf(path,filename);
Label1.Text = "File uplod Successfull";
}
else
{
Label1.Text = "please Select Word file";
}
}
else
{
Label1.Text = "Please select The File";
}
}
}

Comments or Responses

Posted by: Marrisa on: 8/16/2013 Level:Starter | Status: [Member] | Points: 10
Should i enter all the difficult code when i conve
Posted by: Marrisa on: 8/16/2013 Level:Starter | Status: [Member] | Points: 10
I'd like to a easier way to [LINK=http://www.raste
Posted by: Getmore on: 8/25/2013 Level:Starter | Status: [Member] | Points: 10
thank you for sharing your code in converting word
Posted by: Barcodelib on: 11/25/2013 Level:Starter | Status: [Member] | Points: 10
Do I need to use any MS Word product when using this dll to convert Word to PDF?



Source from:http://www.rasteredge.com/solutions/image-converting/

Login to post response