Click here to Skip to main content
13,146,739 members (85,174 online)
Click here to Skip to main content
Add your own
alternative version

Stats

13.3K views
12 bookmarked
Posted 10 Apr 2015

Convert doc/docx Documents to PDF with Foxit Reader

, 10 Apr 2015
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

Björn Ranft
Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
YousefMMS2-Jun-15 3:29
memberYousefMMS2-Jun-15 3:29 
GeneralRe: My vote of 4 Pin
El_Codero2-Jun-15 10:36
memberEl_Codero2-Jun-15 10:36 
GeneralNOT USEFUL, IT IS NOT A FREE. Pin
uniquecoder113-Apr-15 10:27
memberuniquecoder113-Apr-15 10:27 
GeneralRe: NOT USEFUL, IT IS NOT A FREE. Pin
YousefMMS2-Jun-15 3:26
memberYousefMMS2-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 11 Apr 2015
Article Copyright 2015 by Björn Ranft
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid