Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i have an application in which i upload a word file and when i click on button it'll convert it into pdf file. I also tried itextsharp but not getting exact code for that also. Can any one help me to solve this using itextsharp or any other method?? Thank you in advance...
Posted
Updated 25-Jun-19 23:38pm

I think the _A_K_ suggested you a good article

You can also to use online API, ie I would like to suggest the Restfull service http://www.convertapi.com/api/word-pdf-api.

A following console app example below can be used in ASP.NET applications too.
C#
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace Word2Pdf
{
    class Program
    {
        [STAThreadAttribute]
        static void Main(string[] args)
        {

            using (var client = new WebClient())
            {
                Console.WriteLine("Please choose a Word document to convert to PDF.");

                var openFileDialog = new OpenFileDialog {Filter = 
"Word document(*.doc;*.docx)|*.doc;*.docx"};
                if (openFileDialog.ShowDialog()!=DialogResult.OK) return;                               
                var fileToConvert = openFileDialog.FileName;

                Console.WriteLine(string.Format("Converting the file {0} Please wait.", fileToConvert));

                var data = new NameValueCollection();               
                data.Add("OutputFileName", "MyFile.pdf"); 

                try
                {                    
                    client.QueryString.Add(data);
                    var response = 
client.UploadFile("http://do.convertapi.com/word2pdf", fileToConvert);
                    var responseHeaders = client.ResponseHeaders;
                    var web2PdfOutputFileName = responseHeaders["OutputFileName"];
                    var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), web2PdfOutputFileName);
                    File.WriteAllBytes(path, response);
                    Console.WriteLine("The conversion was successful! The word file {0} converted to PDF and saved at {1}", fileToConvert, path);
                }
                catch (WebException e)
                {
                    Console.WriteLine("Exception Message :" + e.Message);
                    if (e.Status == WebExceptionStatus.ProtocolError)
                    {
                        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                    }

                }


            }

            Console.ReadLine();
        }
    }
}



Or you can also to convert document of Word to PDF by Spire.Doc (http://www.e-iceblue.com/Introduce/word-for-net-introduce.html[^]), for example:
C#
Spire.Doc.Document doc = new Spire.Doc.Document("my document.doc/docx")
doc.SaveToFile("pdf document.pdf", Spire.Doc.FileFormat.PDF);

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET. It supports C#, VB.NET, ASP.NET, ASP.NET and MVC.
 
Share this answer
 
v2
Comments
Pandvi 13-Jul-12 2:01am    
Good! my 5!
Volynsky Alex 13-Jul-12 2:20am    
Thanks Pandvi!
Grigor_I 13-Jul-12 2:35am    
+5!
Naikniket 13-Jul-12 10:21am    
may be you think this is silly but i have to ask as in future i am going to upload on server.. is this api free???
Naikniket 13-Jul-12 10:23am    
i know spire is not free.. i am asking about 1st one..
This function will help you to make it:
_Document.ExportAsFixedFormat Method[^]

Here is an example:
Export Word Documents to PDF with C#[^]

Also have a look at some similar threads:
Convert .xls,.doc files to PDF with C#[^]
Convert word doc to pdf[^]
and this CP article PDF creation using C# (and Office) from RTF/DOC files[^] might help you.

Here is simple example where you get a lot of it.
How do I convert Word files to PDF programmatically?[^]

Try a search on CodeProject QA[^] & CodeProject Articles[^] on same.
 
Share this answer
 
Hi,
Refer the link:
Convert Word-Documents to PDF on an ASP.NET Server[^]

All the best.
--Amit
 
Share this answer
 
Hello, I found a method to convert with Word automation.
But it seems a little slow to convert

C#
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
 
Share this answer
 
Comments
Hatepeora 15-Feb-16 7:32am    
Tracy I'm afraid that when you're working with Word automation in .NET you can expect bad performances because from behind the scene you're actually running the MS Word application itself. Also it's not recommended to use it on server side.
Instead you should try this Word processing library for .NET, it provides a direct API for converting Word documents to PDF files in C#.

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