Convert document file to HTML, PDF, RTF, XPS


In this article you will see how is too easy to convert your document from .doc or .docx format to different formats like .html, .pdf, .rtf and .xps.. Online conversation is also available but why don't we chose if we don't have a internet and you want to convert the file urgently.

//First you need to add the references of office file to your project
Step 1:Add reference of Office files
reference dll files name: Microsoft.Office.Interop.Word.dll, Microsoft.Vbe.Interop.dll, office.dll

//You have to create a new form in your project
Step 2:Create a new form

//Add the controls to the form
Step 3:Add the controls
See the attached snapshot for GUI of the form

//Write this code on the different control of the forms
Step 4:Write code on form.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Diagnostics;

object format;//define it as globally at form level
Step 5:Write code on Source Button click
//Source means from which location(path) you are choosing a file
private void ButtonSource_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Document files (*.doc)|*.doc|Document files (*.docx)|*.docx";
dialog.InitialDirectory = "C:\\";
dialog.Title = "Select a document file";
if (dialog.ShowDialog() == DialogResult.OK)
{
TextBoxSource.Text = dialog.FileName;
}
}

Step 6:Write code on Destination Button click
//Destination means at which location(path) you want the converted file.
private void ButtonDestination_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fdialog = new FolderBrowserDialog())
{
if (DialogResult.OK == fdialog.ShowDialog())
{
TextBoxDestination.Text = fdialog.SelectedPath;
}
}
}
Step 7:Write code on Convert Button Click
private void ButtonConvert_Click(object sender, EventArgs e)
{
string existpath = TextBoxDestination.Text + "\\" + TextBoxFileName.Text + "." + CBCnvrttype.Text;
if (File.Exists(existpath))
{
MessageBox.Show("File name already exists." + Environment.NewLine + "Please give a different name.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
TextBoxFileName.Focus();
TextBoxFileName.SelectAll();
}
else
{
if (TextBoxSource.Text == "")
{
MessageBox.Show("Please specify the source.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ButtonSource_Click(ButtonSource, null);
}
else if (TextBoxDestination.Text == "")
{
MessageBox.Show("Please specify the destination.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ButtonDestination_Click(ButtonDestination, null);
}
else if (TextBoxFileName.Text == "")
{
MessageBox.Show("Please specify the target file name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
TextBoxFileName.Focus();
}
else
{
try
{
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();

// specifying the Source & Target file names
object Source = TextBoxSource.Text;
object Target = TextBoxDestination.Text + "\\" + TextBoxFileName.Text + "." + CBCnvrttype.Text;

// Use for the parameter whose type are not known or
// say Missing
object Unknown = Type.Missing;

// Source document open here
// Additional Parameters are not known so that are
// set as a missing type
newApp.Documents.Open(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);

// Specifying the format in which you want the output file
if (CBCnvrttype.Text == "HTML")
{
format = WdSaveFormat.wdFormatHTML;
}
else if (CBCnvrttype.Text == "PDF")
{
format = WdSaveFormat.wdFormatPDF;
}
else if (CBCnvrttype.Text == "RTF")
{
format = WdSaveFormat.wdFormatRTF;
}
else if (CBCnvrttype.Text == "XPS")
{
format = WdSaveFormat.wdFormatXPS;
}

//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);

// for closing the application
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
MessageBox.Show("File converted successfully to" + " " + "'" + TextBoxFileName.Text + "." + CBCnvrttype.Text + "'" + Environment.NewLine + "at location" + " :" + " " + "'" + TextBoxDestination.Text + "'", "Conversion successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
string convertedfilelocation = TextBoxDestination.Text + "\\" + TextBoxFileName.Text + "." + CBCnvrttype.Text;
DialogResult ans = MessageBox.Show("Do you want to convert another file?","File Converter",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (ans == DialogResult.Yes)
{
TextBoxSource.Clear();
TextBoxDestination.Clear();
TextBoxFileName.Clear();
CBCnvrttype.SelectedItem = null;
ButtonSource.Focus();
}
else
{
string selectexistfile = "/select," + convertedfilelocation;
Process.Start("explorer.exe", selectexistfile);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}



Your file will be converted at destination location, than you will be asking for convert another file if you choose no you will be redirected to the destination location using file explorer and converted file will be selected automatically.


Attachments

Article by Nirav Lalan
Regards, Nirav Lalan DNS Gold Member "If you can dream it, you can do it."

Follow Nirav Lalan or read 17 articles authored by Nirav Lalan

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: