Click here to Skip to main content
15,500,853 members
Articles / Web Development / ASP.NET
Posted 30 Jul 2009


102 bookmarked

Convert Word-Documents to PDF on an ASP.NET Server

Rate me:
Please Sign up or sign in to vote.
4.93/5 (20 votes)
28 Jul 2012CPOL3 min read
Convert Word-Documents to PDF-Files using Word 2007 on a Server


This PDFConverter converts Microsoft Word documents (*.doc) into PDF-files on a Webserver. There is a simple Webform, where you can upload your Word-Document, which will be converted and sent back as a PDF-File.



There are a lot of people trying to do this Word => PDF conversion using Com Interop directly from their ASP.NET code. As Microsoft reports, Word or any other Office products are not recommended to be automated on a server. The Office products are optimized for running as client applications using a Desktop for interaction. This is why, if you want to use an Office-product in any kind on a server, there must be a User logged in. But then, there is the next problem: Using COM Interop directly from ASP.NET means, the call is made by the ASP.NET-User which is not allowed to interact with Word. Even if this setting is changed in the DCOM-configuration, there will still remain a lot of access rights-related problems with this solution. That's why I considered the following way to do it:



The PDFConverter.exe is an executable containing RemotableObjects running all the time on the server. To do this, a User must be logged in. When starting the PDFConverter.exe, it will be checked if Word 2007 is available or not. I configured that Word should be Visible for this check, so you can see if Microsoft Word quickly opens and closes again, everything works fine.

Then there is the normal website with fileupload. Store the uploaded file somewhere (don't forget to give the appropriate rights to the ASP.NET and IIS User on this folder, in order to be able to save the uploaded file there).

When the file is saved, you call the convert()-method of the PDFConverter.RemoteConverter Instance which you get using Remoting (see code). The whole conversion thing is then called from the PDFConverter.exe which runs on a "Desktop" with the appropriate rights to interact with Microsoft Word.

When the conversion is finished, you can do whatever you want with the pdf-file. In the example, it will be streamed back as filedownload to the client. 

Using the Code


Serverside PDFConverter.exe (app.config):

        <wellknown objecturi="RemoteConverter" 
	type="PDFConverter.RemoteConverter, RemoteConverter" mode="Singleton">
        <channel port="8989" ref="http">

Serverside PDFConverter.exe (Form1.cs):

//initialize remoting
RemotingConfiguration.RegisterWellKnownServiceType(new RemoteConverter().GetType(), 

Serverside ASP.NET:

Add a Reference to the PDFConverter and then use this code to create instance: Default.aspx.cs:


Don't forget to set your correct storefolder in the Default.aspx.cs.


Serverside PDFConverter.exe:

I copied the important part of code for the PDF-Conversion using Word 2007 from a MSDN-Article. See here: Saving Word 2007 Documents to PDF and XPS Formats.

Serverside ASP.NET:

//Save original file
//call the converter method
converter.convert(sourcefile, outputfile);

//delete the original file
if (System.IO.File.Exists(sourcefile))

//Send back a downloadable File
System.IO.FileInfo downloadFile = new System.IO.FileInfo(outputfile);
HttpContext.Current.Response.ContentType = "application/pdf";
                                       string.Format("attachment; filename={0}",
HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());


As you see, it's not too much code. The whole conversion-code is copied from MSDN, so it's more just the idea doing this with Remoting. I am not a Remoting-expert, so there might be easier/nicer/better ways to do this, so please tell me if you have any considerations. Anyway, I think this solution is much better than trying to use Word directly from the ASP.NET-code.


  • July 30th, 2009: Initial version


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

Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionThank you! Nice work Pin
rongping16-Nov-21 10:32
Memberrongping16-Nov-21 10:32 
QuestionWindows Server 2019 with Classic ASP and Office 2013 Pin
Member 148674244-Sep-21 20:43
MemberMember 148674244-Sep-21 20:43 
Questionwill it support with MS -Office -10 Pin
kpv12324-Mar-19 23:08
Memberkpv12324-Mar-19 23:08 
QuestionHOw t o give appropriate rights to the ASP.NET and IIS User on this folder Pin
Member 1417115317-Mar-19 18:16
MemberMember 1417115317-Mar-19 18:16 
GeneralMy vote of 5 Pin
bhalaniabhishek27-Aug-17 21:17
Memberbhalaniabhishek27-Aug-17 21:17 
GeneralMy vote of 5 Pin
Danny Hulmston5-Aug-13 6:30
MemberDanny Hulmston5-Aug-13 6:30 
BonCoder27-Jul-12 12:13
MemberBonCoder27-Jul-12 12:13 
Questioncan we use it for ppt to pdf conversion also? Pin
Member 84911544-Jul-12 19:44
MemberMember 84911544-Jul-12 19:44 
AnswerRe: can we use it for ppt to pdf conversion also? Pin
pottwalblog5-Jul-12 4:37
Memberpottwalblog5-Jul-12 4:37 
GeneralRe: can we use it for ppt to pdf conversion also? Pin
Beatrice Monk6-Nov-14 22:33
professionalBeatrice Monk6-Nov-14 22:33 
QuestionSay what now? Pin
Member 285817521-Feb-12 11:31
MemberMember 285817521-Feb-12 11:31 
AnswerRe: Say what now? Pin
pottwalblog21-Feb-12 13:24
Memberpottwalblog21-Feb-12 13:24 
GeneralI could not download <<Download PDFConverter - 18.94 KB >> Pin
steven_wsoa27-May-11 10:42
Membersteven_wsoa27-May-11 10:42 
GeneralRe: I could not download > Pin
BonCoder27-Jul-12 12:12
MemberBonCoder27-Jul-12 12:12 
QuestionNice article.But I have a qn? Pin
Wonde Tadesse16-Dec-10 11:37
professionalWonde Tadesse16-Dec-10 11:37 
AnswerRe: Nice article.But I have a qn? Pin
pottwalblog16-Dec-10 21:18
Memberpottwalblog16-Dec-10 21:18 
QuestionDoes it work on Ms word 2003 documents? Pin
Member 147888031-Aug-09 6:04
MemberMember 147888031-Aug-09 6:04 
AnswerRe: Does it work on Ms word 2003 documents? Pin
pottwalblog31-Aug-09 7:19
Memberpottwalblog31-Aug-09 7:19 
GeneralRe: Does it work on Ms word 2003 documents? Pin
Member 14788801-Sep-09 4:27
MemberMember 14788801-Sep-09 4:27 
AnswerRe: Does it work on Ms word 2003 documents? Pin
pottwalblog1-Sep-09 20:52
Memberpottwalblog1-Sep-09 20:52 
GeneralRe: Does it work on Ms word 2003 documents? Pin
Member 14788803-Sep-09 6:22
MemberMember 14788803-Sep-09 6:22 
AnswerRe: Does it work on Ms word 2003 documents? Pin
pottwalblog6-Sep-09 21:36
Memberpottwalblog6-Sep-09 21:36 
GeneralMy vote of 2 Pin
ptmcomp4-Aug-09 8:10
Memberptmcomp4-Aug-09 8:10 
Generalnice artice but... Pin
ChiliBeanie31-Jul-09 3:19
MemberChiliBeanie31-Jul-09 3:19 
GeneralRe: nice artice but... Pin
pottwalblog31-Jul-09 3:47
Memberpottwalblog31-Jul-09 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.