0

I have this snippet of code that should open a docx file and I would like to save it as pdf.
Unfortunately no error is thrown but the PDF is not saved.

Any hint on how to save it?

var wordApplication = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDocument = null;

try
{
    wordDocument = wordApplication.Documents.Open(docPath + ".docx");

    if (wordDocument != null)
        wordDocument.SaveAs2(docPath + ".pdf", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
        //wordDocument.ExportAsFixedFormat(docPath + ".pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
    else throw new Exception("CUSTOM ERROR: Cannot Open Word Application");
}
catch (Exception ex)
{
    new Exception("CUSTOM ERROR" + ex.Message);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mauro
  • 2,032
  • 3
  • 25
  • 47
  • It is a horrible idea to use Office Interop from ASP.NET or another server technology. These APIs were written for use in a desktop application, for automating Office (a suite of desktop applications). Server applications are different in many ways that make it a very, very bad idea to use Office Interop in them. It's also unsupported by Microsoft, and may violate your Office license. See [Considerations for server-side Automation of Office](http://support.microsoft.com/kb/257757) – John Saunders Jan 06 '14 at 02:04

2 Answers2

2

There are a number of different options detailed on this post on stack already.

Word, and Interop especially, is particularly painful when doing this sort of this as it's COM based nature means it does not always play by the .NET rules. You might need to Activate() the document first before saving or there may be other issues. You might want to try saving the doc as a normal docx just to isolate the issue. It might also be that there is something in the doc that is not saving so it's worth testing with a basic doc first.

EDIT: See this link and this link

The IIS tweak is in advanced settings of the application pool. You will need to run the pool under a specific identity and then set Load User Profile to true. I have never used Interop like this and it may not work. If you are just trying to convert a word document to a pdf then there are easier ways than interop (OpenXML, pdf libraries etc). Is there are reason you are using Interop?

EDIT: You might want to give DocX a go. You can find it here

It looks like this is a common issue with conversion. I have done this in the past using Aspose but I have never had to convert a PDF on a server without being able to simply buy the best component (and Aspose is not cheap!). I would certainly advise trying to stay away from Interop if you can but after reading that post it may be needed.

Community
  • 1
  • 1
Cu Jimmy
  • 336
  • 1
  • 3
  • I'll give it a try. May be of any help saying that the very same code is working when launching the application from Visual Studio 2010, but fails when launched from the web application published in IIS? – Mauro Sep 14 '12 at 14:21
  • The issue is running it on IIS. For Interop to work I think it might need Word installed (and possibly open) to work. It's working locally on your dev machine because it is running in a user profile and is actually opening Word in the background. On the server there is no profile (tho you can set it to use one in IIS) and Word cannot open. You might find if you log in to the server and run it as a server user that it works. – Cu Jimmy Sep 14 '12 at 14:33
  • Might also need to Server.MapPath the docPath – Cu Jimmy Sep 14 '12 at 14:40
  • "On the server there is no profile (tho you can set it to use one in IIS)" Could you elaborate on that? How do I set to use it in IIS? – Mauro Sep 15 '12 at 10:24
  • I am just trying to find a free solution to convert a docx into a PDF. Free, Docx-to-PDF are ground rules. Cannot really change them – Mauro Sep 17 '12 at 07:21
1

having the same problem with server 2008r2 and now with server 2012r2 and office 2010/2016

try to create a "Desktop" folder in C:\Windows\SysWOW64\config\systemprofile

this is work for me

find another article about this

office interop does not work in windows service

Gpg
  • 11
  • 2