Sei sulla pagina 1di 5

Convert Word-Documents to PDF on an ASP.

NET Server - CodeProject

http://www.codeproject.com/Articles/38592/Convert-Word-Documents-t...

Sign up for our free weekly Web Developer Newsletter.

home help

articles

quick answers

discussions

features

community
Search for articles, questions, tips

Articles Web Development ASP.NET General

Next

Article Browse Code Bugs / Suggestions Stats Revisions (5) Alternatives Comments (27)

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


By pottwalblog, 28 Jul 2012
4.91 (17 votes) Rate:

About Article
Convert Word-Documents to PDF-Files using Word 2007 on a Server Type Licence Article CPOL 30 Jul 2009 97,283 4,667 98 times

Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.

First Posted Views Downloads Bookmarked

Download PDFConverter - 18.94 KB Upload a File to webserver, which will be converted to PDF and sent back to you as File-Download.
View this article's Workspace Fork this Workspace

C# ASP.NET Windows .NET Dev COM COM+ Beginner +

Introduction
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.

Connect using Git

Top News
C# 6: First reactions
Get the Insider News free each morning.

Prerequisites
Add your own alternative version

Share

Microsoft Word 2007 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS A User, who has the rights to execute Word, must be logged in on the Server The PDFconverter.exe must be running (part of this project)

Related Videos

Background
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 Officeproduct 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:

Related Articles
Manipulate (Add/Edit) PDF

1 of 5

4/18/2014 4:32 PM

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

http://www.codeproject.com/Articles/38592/Convert-Word-Documents-t...

using .NET Pdfizer, a dumb HTML to PDF converter, in C# Text2PDF A SOA approach to dynamic DOCX-PDF report generation Part 2 ASP.NET PDF Viewer User Control Without Acrobat Reader Installed on Client or Server Convert text into PDF using ASP.NET and C# Editing PDF Forms (AcroForms) within a Silverlight Application SharePoint OCR image files indexing Creating a Proxy to Download PDF Reports from SRS Display PDF within web browser using MVC3

Explanation
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.

PDF reporting using ASP.NET MVC3 ASP.NET C# Search Engine (Highlighting, JSON, jQuery & Silverlight) PDF for Silverlight Automatically Printing an RDLC file in ASP.NET MVC 3 PDF Printing to Microsoft Azure and Google Cloud Using Amyuni PDF Converter Add Images and Textboxes to PDF Merge PowerPoint Presentations (PPTX) & save as PDF Blend PDF with Silverlight Generating Dynamic PDF Documents using the Open Source Scryber Library PDF Viewer Control Without Acrobat Reader Installed

Using the Code


Remoting
Serverside PDFConverter.exe (app.config):
Collapse | Copy Code

Related Research

<configuration> <system.runtime.remoting> <application> <service> <wellknown objecturi="RemoteConverter" type="PDFConverter.RemoteConverter, RemoteConverter" mode="Singleton"> </wellknown> <channels> <channel port="8989" ref="http"> </channel> </channels> </service> </application> </system.runtime.remoting></configuration>

Fine-Tuning the Engines of SMB Growth: 4 strategies for growing your business

Serverside PDFConverter.exe (Form1.cs):

2 of 5

4/18/2014 4:32 PM

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

http://www.codeproject.com/Articles/38592/Convert-Word-Documents-t...

Collapse | Copy Code

... //initialize remoting RemotingConfiguration.Configure("PDFConverter.exe.config",false); RemotingConfiguration.RegisterWellKnownServiceType(new RemoteConverter().GetType(), "RemoteConverter",WellKnownObjectMode.Singleton); ...

Serverside ASP.NET:
Add a Reference to the PDFConverter and then use this code to create instance: Default.aspx.cs:
Collapse | Copy Code

Custom API Management for the Enterprise: Learn how to build a successful API strategy [Webinar]

... converter=(PDFConverter.RemoteConverter)Activator.GetObject (typeof(PDFConverter.RemoteConverter),"http://localhost:8989/RemoteConverter"); ...

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

Converting
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.

Protecting Your Business Data: Five Dos and Donts

Serverside ASP.NET:
Collapse | Copy Code

Insider Secrets on API Security From Experts at Securosis [Webinar]

... //Save original file FileUpload1.SaveAs(sourcefile); //call the converter method converter.convert(sourcefile, outputfile); //delete the original file if (System.IO.File.Exists(sourcefile)) System.IO.File.Delete(sourcefile); //Send back a downloadable File System.IO.FileInfo downloadFile = new System.IO.FileInfo(outputfile); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", downloadFile.Name)); HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString()); HttpContext.Current.Response.WriteFile(downloadFile.FullName); HttpContext.Current.Response.End(); HttpContext.Current.Response.Close(); ...

Conclusion
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.

History
July 30th, 2009: Initial version

3 of 5

4/18/2014 4:32 PM

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

http://www.codeproject.com/Articles/38592/Convert-Word-Documents-t...

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

About the Author


pottwalblog
Switzerland

No Biography provided Article Top

Comments and Discussions


Add a Comment or Question
Search this forum Profile popups Spacing Relaxed Noise High Layout Normal Per page 10 Go Update

First Prev Next

My vote of 5 DOWNLOAD LOCATION FOR THIS ARTICLE SOURCE CODE can we use it for ppt to pdf conversion also? Re: can we use it for ppt to pdf conversion also? Say what now? Re: Say what now? Another solution without COM I could not download <<Download PDFConverter - 18.94 KB >> Re: I could not download > Nice article.But I have a qn?
Last Visit: 31-Dec-99 23:00 Last Update: 18-Apr-14 2:00

Danny Hulmston BonCoder

5-Aug-13 10:30 27-Jul-12 16:13

Member 8491154

4-Jul-12 23:44

pottwalblog

5-Jul-12 8:37

Member 2858175 pottwalblog Lvse steven_wsoa

21-Feb-12 15:31 21-Feb-12 17:24 1-Nov-11 0:12 27-May-11 14:42

BonCoder Wonde Tadesse Refresh

27-Jul-12 16:12 16-Dec-10 15:37 1 2 3 Next

General

News

Suggestion

Question

Bug

Answer

Joke

Rant

Admin

4 of 5

4/18/2014 4:32 PM

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

http://www.codeproject.com/Articles/38592/Convert-Word-Documents-t...

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

Permalink | Advertise | Privacy | Mobile Web04 | 2.8.140415.2 | Last Updated 28 Jul 2012

Layout: fixed | fluid

Article Copyright 2009 by pottwalblog Everything else Copyright CodeProject, 1999-2014 Terms of Use

5 of 5

4/18/2014 4:32 PM

Potrebbero piacerti anche