Limited time promo

CSharp Sample - Convert Office Docs

English

This help topic applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Mar 28, 2022
novaPDF 11.x
We strive to keep our help as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
The Convert Office Docs sample is a simple Windows console application that converts some Microsoft Office documents (Word, Excel, Publisher, PowerPoint and Visio) using Convert... functions. It uses Office OLE automation so Microsoft Office has to be installed.
The sample documents converted are located in the "OfficeDocuments" folder in novaPDF SDK installation folder. The pdf files are saved in the same folder.
Hidden links and internal document links are converted in pdf hyperlinks (except for Excel documents).

Source code

using System;
using System.Windows.Forms;
// the novapiLib package must be added as a	COM	refference
using novapiLib<%VERSION_MAJOR%>;
using Globals;

namespace Hello_World_CSharp
{
    class Class1
    {
        public static string PRINTER_NAME = "novaPDF SDK <%VERSION_MAJOR%>";
        public static string PROFILE_NAME = "ConvertOffice";
        public static int PROFILE_IS_PUBLIC = 0;

        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                // create the NovaPdfOptions object
                NovaPdfOptions<%VERSION_MAJOR%> pNova = new NovaPdfOptions<%VERSION_MAJOR%>();
                // initialize the NovaPdfOptions object
                // if you have an application license for novaPDF SDK, 
                // pass the license key to the Initialize() function
                pNova.Initialize(PRINTER_NAME, "");

                // get the active profile ...
                string activeProfile = null;
                try
                {
                    pNova.GetActiveProfile(out activeProfile);
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    // ignore profile exists error
                    if (NovaErrors.NV_NO_ACTIVE_PROFILE == (uint)e.ErrorCode)
                    {
                        System.Console.WriteLine("The printer does not have an active profile");
                    }
                    else
                    {
                        // more	serious	error, propagate it
                        throw e;
                    }
                }

                string _newProfileID = String.Empty;
                //add a new profile
                pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, out _newProfileID);

                //load the new profile
                pNova.LoadProfile(_newProfileID);

                //DO NOT OPEN PDF 
                //an Open action is added by default in the profile - delete or disable this open action
                //pNova.DisableActionType((int)ActionType.NOVA_ACTION_OPEN);
           
                // set save options
                nova.NovaTools.AddSaveOptions(pNova);

                //save the new added profile
                pNova.SaveProfile();

                //set the new profile as the active profile
                pNova.SetActiveProfile(_newProfileID);

                //Path for novaPDF <%VERSION_MAJOR%> SDK sample documents
                string strDocumentsPath = "C:\\Users\\Public\\Documents\\novaPDF <%VERSION_MAJOR%>\\SDK\\OfficeDocuments\\";
                string strDocPath;
		        
                // --------------------------------------------------
                // Convert Word document
                // Microsoft Office Word has to be installed
                // --------------------------------------------------
                //Document file path
                strDocPath = strDocumentsPath + "Word.docx";
                //style name | level | type 
                string strHeadings = "Heading 1|1|0;Heading 2|2|0;Heading 3|3|0";
                pNova.LicenseApplication("WINWORD.EXE");
                pNova.LicenseApplication("SPLWOW64.EXE");
                pNova.ConvertWordDocument(strDocPath, 1, 0, 1, 1, 1, 1, 1, 1, 1, 3, strHeadings);
        		
                // --------------------------------------------------
                // Convert PowerPoint document
                // Microsoft Office PowerPoint has to be installed
                // --------------------------------------------------
                //Document file path
                strDocPath = strDocumentsPath + "PowerPoint.pptx";
                pNova.LicenseApplication("POWERPNT.EXE");
                pNova.LicenseApplication("SPLWOW64.EXE");
                pNova.ConvertPowerPointDocument(strDocPath, 1, 1, 1, 1, 1, 1);
        		
                // --------------------------------------------------
                // Convert Publisher document
                // Microsoft Office Publisher has to be installed
                // --------------------------------------------------
                //Document file path
                strDocPath = strDocumentsPath + "Publisher.pub";
                pNova.LicenseApplication("MSPUB.EXE");
                pNova.LicenseApplication("SPLWOW64.EXE");
                pNova.ConvertPublisherDocument(strDocPath, 1, 1, 1, 1, 1, 1);
        		
                // --------------------------------------------------
                // Convert Excel document
                // Microsoft Office Excel has to be installed
                // hidden links in Excel documents will not be converted to pdf links
                // --------------------------------------------------
                //Document file path
                strDocPath = strDocumentsPath + "Excel.xlsx";
                pNova.LicenseApplication("EXCEL.EXE");
                pNova.LicenseApplication("SPLWOW64.EXE");
                pNova.ConvertExcelDocument(strDocPath, 1);
        		
                // --------------------------------------------------
                // Convert Visio document
                // Microsoft Office Visio has to be installed
                // --------------------------------------------------
                //Document file path
                strDocPath = strDocumentsPath + "Visio.vsd";
                pNova.LicenseApplication("VISIO.EXE");
                pNova.LicenseApplication("SPLWOW64.EXE");
                pNova.ConvertVisioDocument(strDocPath, 1, 1, 1, 1, 1);

                pNova.SetActiveProfile(activeProfile);
                pNova.DeleteProfile(_newProfileID);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}