Hi tanws8,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button Text="Download" runat="server" OnClick="Download" />
Namespaces
C#
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
VB.Net
Imports System.IO
Imports System.Reflection
Imports Microsoft.Office.Interop.Word
Code
C#
protected void Download(object sender, EventArgs e)
{
CreateDocument(@"C:\Users\Wee Sin\Desktop\Test1.docx", 1);
}
private void CreateDocument(object obj_fileName, int int_value)
{
Document word_doc = null;
object obj_missing = Missing.Value;
string str_fileName = null;
Application word_app = new Application();
if (File.Exists((string)obj_fileName))
{
object obj_readOnly = false; // Default set as false
word_app.Application.Visible = false;
word_doc = word_app.Documents.Open(ref obj_fileName, ref obj_missing, ref obj_readOnly, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing);
word_app.ActiveWindow.Visible = false;
word_doc.Activate();
str_fileName = "Test";
}
if (int_value == 0)
{
word_doc.SaveAs("C:/Users/Wee Sin/Desktop/" + str_fileName + ".doc", 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, obj_missing);
}
else
{
if (!Directory.Exists(Server.MapPath("~/Files/")))
{
Directory.CreateDirectory(Server.MapPath("~/Files/"));
}
// Save the PDF file in Files folder.
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);
}
((Microsoft.Office.Interop.Word._Document)word_doc).Close(ref obj_missing, ref obj_missing, ref obj_missing);
((Microsoft.Office.Interop.Word._Application)word_app).Quit();
word_app = null;
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();
// Delete the generated file from Files folder.
if (File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
// Download File.
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();
}
VB.Net
Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
CreateDocument("C:\Users\Wee Sin\Desktop\Test1.docx", 1)
End Sub
Private Sub CreateDocument(ByVal obj_fileName As Object, ByVal int_value As Integer)
Dim word_doc As Document = Nothing
Dim obj_missing As Object = Missing.Value
Dim str_fileName As String = Nothing
Dim word_app As Application = New Application()
If File.Exists(CStr(obj_fileName)) Then
Dim obj_readOnly As Object = False
word_app.Application.Visible = False
word_doc = word_app.Documents.Open(obj_fileName, obj_missing, obj_readOnly, 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)
word_app.ActiveWindow.Visible = False
word_doc.Activate()
str_fileName = "Test"
End If
If int_value = 0 Then
word_doc.SaveAs("C:/Users/Wee Sin/Desktop/" & str_fileName & ".doc", 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, obj_missing)
Else
If Not Directory.Exists(Server.MapPath("~/Files/")) Then
Directory.CreateDirectory(Server.MapPath("~/Files/"))
End If
' Save the PDF file in Files folder.
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)
End If
CType(word_doc, Microsoft.Office.Interop.Word._Document).Close(obj_missing, obj_missing, obj_missing)
CType(word_app, Microsoft.Office.Interop.Word._Application).Quit()
word_app = Nothing
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()
' Delete the generated file from Files folder.
If File.Exists(pdfPath) Then
File.Delete(pdfPath)
End If
' Download File.
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 Sub
Screenshot
Original Word File
Downloaded PDF