Hi tanws8,
Please refer below sample.
HTML
<asp:Button Text="Open" runat="server" OnClick="Open" />
Namespaces
C#
using System.IO;
using Microsoft.Office.Interop.Word;
VB.Net
Imports System.IO
Imports Microsoft.Office.Interop.Word
Code
C#
protected void Open(object sender, EventArgs e)
{
string str_fileName = "A";
int int_value = 0;
object obj_missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord = new Application();
var word_doc = oWord.Documents.Add();
if (int_value == 0)
{
if (!Directory.Exists(Server.MapPath("~/Files/")))
{
Directory.CreateDirectory(Server.MapPath("~/Files/"));
}
word_doc.SaveAs(Server.MapPath("~/Files/") + str_fileName + ".doc", WdSaveFormat.wdFormatDocument, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing);
string pdfPath = Server.MapPath("~/Files/") + str_fileName + ".doc";
byte[] bytes;
FileStream fs = File.Open(pdfPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
word_doc.Close();
if (File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + str_fileName + ".doc");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
else
{
if (!Directory.Exists(Server.MapPath("~/Files/")))
{
Directory.CreateDirectory(Server.MapPath("~/Files/"));
}
word_doc.SaveAs(Server.MapPath("~/Files/") + str_fileName + ".pdf", WdSaveFormat.wdFormatPDF, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing);
string pdfPath = Server.MapPath("~/Files/") + str_fileName + ".pdf";
FileStream fs = File.Open(pdfPath, FileMode.Open);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
if (File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + str_fileName + ".pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
((Microsoft.Office.Interop.Word._Document)word_doc).Close(ref obj_missing, ref obj_missing, ref obj_missing);
((Microsoft.Office.Interop.Word._Application)oWord).Quit();
oWord = null;
}
VB.Net
Protected Sub Open(ByVal sender As Object, ByVal e As EventArgs)
Dim str_fileName As String = "A"
Dim int_value As Integer = 0
Dim obj_missing As Object = System.Reflection.Missing.Value
Dim oWord As Microsoft.Office.Interop.Word._Application = New Application()
Dim word_doc = oWord.Documents.Add()
If int_value = 0 Then
If Not Directory.Exists(Server.MapPath("~/Files/")) Then
Directory.CreateDirectory(Server.MapPath("~/Files/"))
End If
word_doc.SaveAs(Server.MapPath("~/Files/") & str_fileName & ".doc", WdSaveFormat.wdFormatDocument, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing)
Dim pdfPath As String = Server.MapPath("~/Files/") & str_fileName & ".doc"
Dim bytes As Byte()
Dim fs As FileStream = File.Open(pdfPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
bytes = New Byte(fs.Length - 1) {}
fs.Read(bytes, 0, Convert.ToInt32(fs.Length))
fs.Close()
word_doc.Close()
If File.Exists(pdfPath) Then
File.Delete(pdfPath)
End If
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment;filename=" & str_fileName & ".doc")
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
Else
If Not Directory.Exists(Server.MapPath("~/Files/")) Then
Directory.CreateDirectory(Server.MapPath("~/Files/"))
End If
word_doc.SaveAs(Server.MapPath("~/Files/") & str_fileName & ".pdf", WdSaveFormat.wdFormatPDF, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing, obj_missing)
Dim pdfPath As String = Server.MapPath("~/Files/") & str_fileName & ".pdf"
Dim fs As FileStream = File.Open(pdfPath, FileMode.Open)
Dim bytes As Byte() = New Byte(fs.Length - 1) {}
fs.Read(bytes, 0, Convert.ToInt32(fs.Length))
fs.Close()
If File.Exists(pdfPath) Then
File.Delete(pdfPath)
End If
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment;filename=" & str_fileName & ".pdf")
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End If
CType(word_doc, Microsoft.Office.Interop.Word._Document).Close(obj_missing, obj_missing, obj_missing)
CType(oWord, Microsoft.Office.Interop.Word._Application).Quit()
oWord = Nothing
End Sub
Screenshot