Good day fancy guys 🙂
We’re presenting today the best of the best of the best. A remote converter for word to pdf.

Using the Microsoft Word Interop apis, we can convert the word file to pdf with conserving the fonts, styles and images in the pdf file.
The Microsoft Interop library is a com library that help developing managed applications for Word. It extends the Word 2013 Developer Reference from the COM environment to the managed environment and focuses on how to use the PIA.

Using this library, actually we are opening several word hidden applications (can be defined from the constructor). For instance, we opened 4 Word applications. These applications will remain active in the background for any needed conversion. The goal for the background applications is that opening and closing a word application is slow, resource consuming, and a causes a headache for the machine itself. So we keep a number of processes opened in the background to be used for fast word convert.

We can choose to dispose and close the word application after like a 100 word conversion. So a new Word application will be opened and will be ready to start receiving tasks.

Why it’s a remote converter, let’s discuss it here
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
For that reason we are using the remote way to convert the word file into pdf file. This remote service can be accessed from any type of applications. As discussed here, so we won’t use the direct conversion way anymore, and the conversion will be faster.

Implementing the remote application

First we need to create a new windows form application, or you can make it a windows service later.
This project will contains a RemoteConverter class, the word helper that can convert a word to pdf file, and a form showing the availability of a word application.
The main 2 parts in this project is the remote converter and the word helper.
The below is the RemoteConverter class. This one have public methods as “isWordAvailable”, “Convert”, and “Dispose”. Dispose method is mainly used to dispose the word object in case you finished.


    public class RemoteConverter : MarshalByRefObject
    {
        private SingleWordApp wordApp;
        private bool wordavailable = false;
        private bool checkedword = false;

        public RemoteConverter()
        { 
        }

        public bool WordIsAvailable()
        {
        }

        public string Convert(string source, out Exception ex)
        {
        }

        public void Dispose()
        {
        }
    }

Now the form application, the windows service or your console application should register the remote converter class as a service hosted on a port.


RemotingConfiguration.Configure("WordRemoteConverter.exe.config", false);
RemotingConfiguration.RegisterWellKnownServiceType(new RemoteConverter().GetType(), 
"RemoteConverter", WellKnownObjectMode.Singleton);

The port is configured in the app settings for this project as below.


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

Then when running the application or the service you will have the remote converter ready to receive requests.

Implementing the requester application

Now you need to call it from other applications/web/consoles etc..
First you need to reference the “.exe” file of the remote converter. After that, you need to get the object of the remote by using the activator. Here we are specifying the address of the remote converter and its port. We can change this to be a configuration.


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

After that, you call any public method from the converter object, and it will be executed in the remote service.
You can use this for any type of applications. The full project is located on github.
The projects just one single word application and process the requests one by one. It can dispose and initialize every couple of conversion, you can change that in the code.

In the next post, we will cover how do you handle multi word files conversion.
Please stay tuned for the next post 🙂