Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / Win32
Tip/Trick

Convert doc/docx Documents to PDF with Foxit Reader

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
10 Apr 2015CPOL1 min read 27.9K   12   4
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.

C#
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

License

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


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
YousefMMS2-Jun-15 3:29
YousefMMS2-Jun-15 3:29 
GeneralRe: My vote of 4 Pin
El_Codero2-Jun-15 10:36
El_Codero2-Jun-15 10:36 
GeneralNOT USEFUL, IT IS NOT A FREE. Pin
uniquecoder113-Apr-15 10:27
uniquecoder113-Apr-15 10:27 
GeneralRe: NOT USEFUL, IT IS NOT A FREE. Pin
YousefMMS2-Jun-15 3:26
YousefMMS2-Jun-15 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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