65.9K
CodeProject is changing. Read more.
Home

Convert doc/docx Documents to PDF with Foxit Reader

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (2 votes)

Apr 11, 2015

CPOL

1 min read

viewsIcon

28717

Convert doc/docx documents to pdf with Foxit Reader Printer. Works without use of Word Interop/SharePoint.

Introduction

I stumbled over a requirement to convert (and print) *.doc, *.docx, ... Documents to pdf with C#. Seems easy, like a standard task and pdfs are a standard document format. I thought that. 

Background

There are many great (commercial) document libraries which support a doc/docx to pdf conversation and a few other scopes to do this, but I just needed a conversation so I was a bit scared of using a whole library for that. 
Also the other scopes like using Word interop or SharePoint has not been an option for me because a licensed Word/Sharepoint installation is needed for that task.

A quick look into foxit's printer settings revealed that this could be an option so I had a try and I was really happy that it works for my requirements. 

In addition, one will have a few more options to configure your pdf output if you're going to hack some Windows Registry values at application runtime. 

Depending on registy manipulating, full silent conversation, showing a FileSaveDialog as well as 
automatically open up converted document is possible.

Using the code

Just Foxit Reader (Free) must be installed on the client.

public class DocumentProcessing
{
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool SetDefaultPrinter(string Name);

    public void ConvertDocxToPdfWithFoxitReader(string documentPath)
    {
        if(!this.FoxitTPdfPrinterFound())
            return;
        
        if(String.IsNullOrWhiteSpace(documentPath))
            return;

        if(!File.Exists(documentPath))
            return;

        string currentPrinter = this.GetDefaultPrinter();
        SetDefaultPrinter("Foxit Reader PDF Printer");
        this.PrintReportAsAProcess(documentPath);

        if(currentPrinter == null
             || String.IsNullOrWhiteSpace(currentPrinter)) 
           return;
            
         SetDefaultPrinter(currentPrinter); 
    }

    public void PrintReportAsAProcess(string documentPath)
    {
            ProcessStartInfo info = new ProcessStartInfo(documentPath);
            info.Verb = "Print";
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(info);
    }

    private bool FoxitTPdfPrinterFound()
    {
            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                if (string.equalsIgnoreCase(printer, "Foxit Reader PDF Printer") == 0)
                {
                   return true;
                }
            }

            return false;
     }

     private List GetInstalledPrinter()
     {
            List printerList = new List();

            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                printerList.Add(printer);
            }

            return printerList;
     }

     private string GetDefaultPrinter()
     {
            PrinterSettings printerSettings = new PrinterSettings();
            foreach (string printer in PrinterSettings.InstalledPrinters)
            {
                if (printerSettings.IsDefaultPrinter)
                    return printer;
            }

            return null;
      }
}

Points of Interest

Interesting registy values: 
[HKEY_CURRENT_USER\Software\Foxit Software\Foxit Reader 6.0\Preferences\Others] 
"bDisableSharePointFeatures"=dword:00000000 
"bEnableJS"="1" 
"nPreferItem"="" 
"nAdvertiseId"="0" 
"bShowAD"="1" 
"nFileSaveType"="0" 
"csFileSavePLGEXT"=" " 
"bCheckExpireDate"="1" 
"bCheckRegister"="1" 
"bCheckStandardSave"="1" 
"bCheckStandardLimited"="1"
"bCheckStandardFeature"="1" 
"csInitialOpenDir"="C:\\Users\\Joker\\Desktop\\pdf_output\\"
"bSeparateMenubar"="1" 
"bShowXFAWarn"="1" 
"bShowWelcomeDlg"="0" 
"bShowTraileDlg"="1" <
"bShowOpenURLMessageBox"="0" 

History

11th April, 2015