Hi Waghmare,
Refer below sample.
Namespaces
C#
using System.Data;
using Word = Microsoft.Office.Interop.Word;
VB.Net
Imports System.Data
Imports Word = Microsoft.Office.Interop.Word
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("LastName"), new DataColumn("FatherName"), new DataColumn("Adress"), new DataColumn("Name"), new DataColumn("Birthday") });
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987");
if (dt.Rows.Count > 0)
{
object fileName = Server.MapPath("~/Files/Test.docx");
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
{
if (docRange.Text.Trim() == "#")
{
docRange.Text = docRange.Text.Replace("#", "");
}
else if (docRange.Text.Trim() == "Ln")
{
docRange.Text = docRange.Text.Replace("Ln", dt.Rows[0]["LastName"].ToString());
}
else if (docRange.Text.Trim() == "Fn")
{
docRange.Text = docRange.Text.Replace("Fn", dt.Rows[0]["FatherName"].ToString());
}
else if (docRange.Text.Trim() == "Ad")
{
docRange.Text = docRange.Text.Replace("Ad", dt.Rows[0]["Adress"].ToString());
}
else if (docRange.Text.Trim() == "Nm")
{
docRange.Text = docRange.Text.Replace("Nm", dt.Rows[0]["Name"].ToString());
}
else if (docRange.Text.Trim() == "Bth")
{
docRange.Text = docRange.Text.Replace("Bth", dt.Rows[0]["Birthday"].ToString());
}
}
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
object destination = @"D:\PdfFile.pdf";
word.ActiveDocument.SaveAs(ref destination, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
((Word._Application)word).Quit();
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {
New DataColumn("LastName"), New DataColumn("FatherName"), New DataColumn("Adress"), New DataColumn("Name"), New DataColumn("Birthday")})
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987")
If dt.Rows.Count > 0 Then
Dim fileName As Object = Server.MapPath("~/Files/Test.doc")
Dim word As New Word.Application()
Dim doc As New Word.Document()
Dim missing As Object = System.Type.Missing
Try
doc = word.Documents.Open(fileName, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
doc.Activate()
For Each docRange As Microsoft.Office.Interop.Word.Range In doc.Words
If docRange.Text.Trim() = "#" Then
docRange.Text = docRange.Text.Replace("#", "")
ElseIf docRange.Text.Trim() = "Ln" Then
docRange.Text = docRange.Text.Replace("Ln", dt.Rows(0)("LastName").ToString())
ElseIf docRange.Text.Trim() = "Fn" Then
docRange.Text = docRange.Text.Replace("Fn", dt.Rows(0)("FatherName").ToString())
ElseIf docRange.Text.Trim() = "Ad" Then
docRange.Text = docRange.Text.Replace("Ad", dt.Rows(0)("Adress").ToString())
ElseIf docRange.Text.Trim() = "Nm" Then
docRange.Text = docRange.Text.Replace("Nm", dt.Rows(0)("Name").ToString())
ElseIf docRange.Text.Trim() = "Bth" Then
docRange.Text = docRange.Text.Replace("Bth", dt.Rows(0)("Birthday").ToString())
End If
Next
Dim format As Object = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF
Dim destination As Object = "D:\PdfFile.pdf"
doc.ActiveDocument.SaveAs(destination, format, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing)
Catch ex As Exception
Response.Write(ex.Message)
Finally
doc.Close(missing, missing, missing)
DirectCast(word, Word._Application).Quit()
End Try
End If
End Sub
The Word file and generated PDF