PDF Printer‎ > ‎Examples‎ > ‎

Printing Microsoft Word Documents to PDF using C#

The following code is from an example that shows a couple of different methods to print a Microsoft Word document to a PDF file.

At the bottom of this page you will find a zip file with the entire solution for Microsoft Visual Studio 2010.

One of the printing methods used are using printing via Verbs. This is documented in detail here http://msdn.microsoft.com/en-us/library/bb776883.aspx?ppud=4


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using Bullzip.PdfWriter;

 

namespace PrintWordDocument

{

    public partial class Form1 : Form

    {

        const string PRINTERNAME = "Bullzip PDF Printer";

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// This function shows how to print a Microsoft Word document

        /// using the default printing application for the document type.

        /// </summary>

        private static void PrintUsingVerb()

        {

            try

            {

                string fileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

                    + @"\..\..\test.doc";

 

                //

                // Set the PDF settings

                //

                DateTime now = DateTime.Now;

                PdfSettings pdfSettings = new PdfSettings();

                pdfSettings.PrinterName = PRINTERNAME;

                pdfSettings.SetValue("Output", string.Format(@"<desktop>\test {0,0:00}-{1,0:00}-{2,0:00}.pdf", now.Hour, now.Minute, now.Second));

                pdfSettings.SetValue("ShowPDF", "yes");

                pdfSettings.SetValue("ShowSettings", "never");

                pdfSettings.SetValue("ShowSaveAS", "never");

                pdfSettings.SetValue("ShowProgress", "no");

                pdfSettings.SetValue("ShowProgressFinished", "no");

                pdfSettings.SetValue("ConfirmOverwrite", "yes");

                pdfSettings.WriteSettings(PdfSettingsFileType.RunOnce);

 

                PdfUtil.PrintFile(fileName, PRINTERNAME);

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error printing Word document.\n\n" + ex.Message);

            }

       

        }

        /// <summary>

        /// This function shows how to print a document to PDF

        /// using the Microsoft Word interop.

        /// </summary>

        private static void PrintDocumentUsingInterop()

        {

            Microsoft.Office.Interop.Word.Application word = null;

            Microsoft.Office.Interop.Word.Document doc = null;

            try

            {

                word = new Microsoft.Office.Interop.Word.Application();

                object fileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

                    + @"\..\..\test.doc";

                word.Visible = false;

                doc = word.Documents.Open(ref fileName);

                word.ActivePrinter = PRINTERNAME;

 

                //

                // Set the PDF settings

                //

                DateTime now = DateTime.Now;

                PdfSettings pdfSettings = new PdfSettings();

                pdfSettings.PrinterName = PRINTERNAME;

                pdfSettings.SetValue("Output", string.Format(@"<desktop>\test {0,0:00}-{1,0:00}-{2,0:00}.pdf", now.Hour, now.Minute, now.Second));

                pdfSettings.SetValue("ShowPDF", "yes");

                pdfSettings.SetValue("ShowSettings", "never");

                pdfSettings.SetValue("ShowSaveAS", "never");

                pdfSettings.SetValue("ShowProgress", "no");

                pdfSettings.SetValue("ShowProgressFinished", "no");

                pdfSettings.SetValue("ConfirmOverwrite", "yes");

                pdfSettings.WriteSettings(PdfSettingsFileType.RunOnce);

 

                doc.PrintOut();

                word.Quit();

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error printing Word document.\n\n" + ex.Message);

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            PrintDocumentUsingInterop();

        }

        private void button2_Click(object sender, EventArgs e)

        {

            PrintUsingVerb();

        }

    }

}

 

 

ċ
PrintWordDocument.zip
(15k)
Bullzip Webmaster,
Aug 24, 2010, 3:39 AM
Comments