Converting Office Documents to PDF

If you have bought our Office to PDF Conversion Solution Accelerator, I want to give you a tip. Not only does this solution work with SharePoint but you also have a standalone document conversion service that you can use from outside SharePoint. If you read on, we will create a batch utility that will convert Office documents on your file system to PDF.
 
As the back-end interface, we have a simple HTML form page where you upload the file and get the pdf version back. It is very easy to test; just browse to the page, select a file and click “Convert File”. .
form
So what we are going to do is create a program that sends all files in a directory to the conversion service and then saves the returned PDF files we get back. 
First let’s bring up Visual Studio, and create a new console application and call it pdfconv. We will make it easy, the first command line argument is the source folder to convert; we will save all PDF files in a subfolder called PDF. The second argument is the url to the conversion service. So you will start the the app like this pdfconv.exe “c:\My Documents\” “http://myservice/…” and it will save the PDF files in c:\My Documents\Pdf\
 
project

Click OK so we can start. Paste in the following code (don’t we all love copy and paste coding ;)

using System;
using System.Net;
using System.IO;

public class Program
{
    static void Usage()
    {
        Console.WriteLine(@"pdfconv.exe <directory> <service>");
        Console.WriteLine(@"  pdfconv.exe 'C:\Documents and settings\word' 'http://localhost/Bamboo.Conversion.FileService/attach.aspx'");
    }

    static int Main(string[] args)
    {
        if (args.Length != 2)
        {
            Usage();
            return -1;
        }
        if (Directory.Exists(args[0]) == false )
        {
            Console.WriteLine("Directory [{0}] does not exist", args[0]);
            return -1;
        }
        string serviceEndpoint = args[1];
        string destPath = Path.Combine(Path.GetDirectoryName(args[0]), "pdf");
        Directory.CreateDirectory(destPath); 
        foreach (string path in Directory.GetFiles(args[0],"*.doc"))
        {
            using (WebClient wc = new WebClient())
            {
                wc.Credentials = CredentialCache.DefaultCredentials;
                wc.QueryString.Add("outExt", ".pdf");
                wc.QueryString.Add("fileName", Path.GetFileName(path));
                byte[] buffer = wc.UploadFile(serviceEndpoint, path);

                string pdfPath = Path.Combine(destPath,
                    Path.GetFileNameWithoutExtension(path) + ".pdf");

                // We use a header to indicate exception
                string error = wc.ResponseHeaders["ConversionError"];
                if (error != null)
                {
                    pdfPath += ".error.txt";
                }
                using (Stream stm = File.Create(pdfPath))
                {
                    stm.Write(buffer, 0, buffer.Length);
                }

            }
        }
        return 0;
    }
}
Let’s take a look at the code. The first thing we do is to make sure we were called with two arguments; a path and the URL to the conversion service. If not, we show a usage page. If all is good we enumerate the word documents in the specified directory. Then we send each file to the conversion service. When calling the service we have to add two query string parameters, the extension we want to convert to (PDF) and the filename.
If everything works, we will get back the PDF file. If an error occurs an http Header “ConversionError” will be set and the body of the request will contain the error. 

The reason we don’t just return status 500 is that there is no way to convey the error if we do this. The WebClient class will stop reading the stream if status is 500.

If an error is returned, we save the error in a file named “xxxx.error.txt”
That is pretty much it.  I hope you will have many occasions to make use of your PDF conversion service, both within SharePoint and as here as a standalone application.

Posted Dec 17 2009, 12:49 AM by Jonas Nilsson

Add a Comment

Please sign into Bamboo Nation to leave a comment.

Blogs

    The Bamboo Team Blog
  • Home

Bamboo Nation, Media Sponsor of:

SPTechCon

Subscribe by Email

Syndication

Bamboo Nation Almost Everywhere

Follow Bamboo Nation on:Bamboo Solutions on Facebook

Bamboo Solutions on Google+

Bamboo Solutions on LinkedIn

Bamboo Solutions on Twitter

Bamboo Solutions on YouTube

Bamboo Now in Alltop!

        Featured in Alltop

SharePoint Calendars

SharePoint Calendars

Bamboo Solutions Corporation, 2002-2012