Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
C#
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 = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
                string filename = TextBox1.Text;
                FileUpload1.SaveAs(Server.MapPath("pdf\\") + filename);
                ConvertTopdf(path, filename);
                Label1.Text = "File upload Successfull";
            }
            else
            {
                Label1.Text = "Please Select the word file only";
            }
        }
        else
        {
            Label1.Text = "Please Selet the File";
        }
    }
    public void ConvertTopdf(string path, string filename)
    {
        SautinSoft.UseOffice u = new SautinSoft.UseOffice();
        if (u.InitWord() == 0)
        {
            //convert Word (RTF, DOC, DOCX to PDF)
            u.ConvertFile(path, Server.MapPath("pdf\\") + filename, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
        }
        u.CloseOffice();
    }


What I have tried:

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 = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = TextBox1.Text;
FileUpload1.SaveAs(Server.MapPath("pdf\\") + filename);
ConvertTopdf(path, filename);
Label1.Text = "File upload Successfull";
}
else
{
Label1.Text = "Please Select the word file only";
}
}
else
{
Label1.Text = "Please Selet the File";
}
}
public void ConvertTopdf(string path, string filename)
{
SautinSoft.UseOffice u = new SautinSoft.UseOffice();
if (u.InitWord() == 0)
{
//convert Word (RTF, DOC, DOCX to PDF)
u.ConvertFile(path, Server.MapPath("pdf\\") + filename, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
}
u.CloseOffice();
}
Posted
Updated 21-Jun-16 4:55am
Comments
Animesh Datta 21-Jun-16 7:16am    
what is the problem ?

1 solution

Of course, using Office interop would be a bad solution. It's better to use Microsoft Open XML SDK:
https://msdn.microsoft.com/en-us/library/office/bb448854.aspx,
https://www.microsoft.com/en-us/download/details.aspx?id=30425.

This is one open-source alternative: NPOI.

This is a set of references to PDF libraries you can use: http://csharp-source.net/open-source/pdf-libraries.

See also my past answers:
Question Convert word to PDF without office or open office,
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900