Limited time promo

C++ 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 22, 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

#include "stdafx.h"
//Include novaPDF headers
#include "..\..\..\include\novaOptions.h"
#include "..\..\..\include\novaEvents.h"
#include "..\..\..\include\novapi.h"
#include "nova.h"

#define PRINTER_NAME    L"novaPDF SDK <%VERSION_MAJOR%>"

//Print profile name
#define PROFILE_NAME    L"ConvertOfficeDocs Profile" 
#define PROFILE_IS_PUBLIC  0


int _tmain(int argc, _TCHAR* argv[])
{
	HRESULT hr = S_OK;

	//initialize COM
	hr = CoInitialize(NULL);
	if (FAILED (hr)) 
	{ 
		MessageBox(NULL, L"Failed to initialize COM", L"novaPDF", MB_OK); 
		return hr; 
	} 

	//create one NovaPdfOptions instance
	INovaPdfOptions<%VERSION_MAJOR%> *pNova = 0;
	hr = CoCreateInstance(__uuidof(NovaPdfOptions<%VERSION_MAJOR%>), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions<%VERSION_MAJOR%>), (LPVOID*) &pNova);

	if (FAILED(hr))   
	{
		MessageBox(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK); 
		return hr;
	}

	// initialize the NovaPdfOptions object to use with a printer licensed for SDK
	// if you have an application license for novaPDF SDK, 
	// pass the license key to the Initialize() function
	hr = pNova->Initialize(PRINTER_NAME, L"");

	if (SUCCEEDED(hr)) 
	{
		LPWSTR pwsOldActiveProfileID = NULL;
		LPWSTR pwsNewProfileID = NULL;

		hr = pNova->GetActiveProfile(&pwsOldActiveProfileID);

		//create a new profile with default settings
		hr = pNova->AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, &pwsNewProfileID);

		//load the newly created profile
		if (SUCCEEDED(hr) && pwsNewProfileID) 
		{
			hr = pNova->LoadProfile(pwsNewProfileID);
		}
		else
		{
			MessageBox(NULL, L"Failed to create profile", L"novaPDF", MB_OK); 
			return hr;
		}

		// set novaPDF options 
		// uncomment the function calls for the options you wish to set and change the options in nova.cpp unit
		
		//DO NOT OPEN PDF 
		//an Open action is added by default in the profile - delete or disable this open action
		//pNova->DisableActionType(NOVA_ACTION_OPEN);

		//Save pdf files to OfficeDocuments folder
		AddSaveOptions(pNova); 

		//save profile changes
		hr = pNova->SaveProfile();
		//set as active profile for printer
		pNova->SetActiveProfile(pwsNewProfileID);

		//Path for novaPDF <%VERSION_MAJOR%> SDK sample documents
		WCHAR wsDocumentsPath[MAX_PATH];
		WCHAR wsDocPath[MAX_PATH];
		SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, wsDocumentsPath);
		wcscat(wsDocumentsPath, L"\\novaPDF <%VERSION_MAJOR%>\\SDK\\OfficeDocuments\\");
		
		// --------------------------------------------------
		// Convert Word document
		// Microsoft Office Word has to be installed
		// --------------------------------------------------
		//Document file path
		wcscpy(wsDocPath, wsDocumentsPath);
		wcscat(wsDocPath, L"Word.docx");
		//style name | level | type 
		WCHAR wsHeadings[200] = L"Heading 1|1|0;Heading 2|2|0;Heading 3|3|0";
		pNova->LicenseApplication(L"WINWORD.EXE");
		pNova->LicenseApplication(L"SPLWOW64.EXE");
		hr = pNova->ConvertWordDocument(wsDocPath, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 3, wsHeadings);
		
		// --------------------------------------------------
		// Convert PowerPoint document
		// Microsoft Office PowerPoint has to be installed
		// --------------------------------------------------
		//Document file path
		wcscpy(wsDocPath, wsDocumentsPath);
		wcscat(wsDocPath, L"PowerPoint.pptx");
		pNova->LicenseApplication(L"POWERPNT.EXE");
		pNova->LicenseApplication(L"SPLWOW64.EXE");
		hr = pNova->ConvertPowerPointDocument(wsDocPath, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
		
		// --------------------------------------------------
		// Convert Publisher document
		// Microsoft Office Publisher has to be installed
		// --------------------------------------------------
		//Document file path
		wcscpy(wsDocPath, wsDocumentsPath);
		wcscat(wsDocPath, L"Publisher.pub");
		pNova->LicenseApplication(L"MSPUB.EXE");
		pNova->LicenseApplication(L"SPLWOW64.EXE");
		hr = pNova->ConvertPublisherDocument(wsDocPath, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
		
		// --------------------------------------------------
		// 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
		wcscpy(wsDocPath, wsDocumentsPath);
		wcscat(wsDocPath, L"Excel.xlsx");
		pNova->LicenseApplication(L"EXCEL.EXE");
		pNova->LicenseApplication(L"SPLWOW64.EXE");
		hr = pNova->ConvertExcelDocument(wsDocPath, TRUE);
		
		// --------------------------------------------------
		// Convert Visio document
		// Microsoft Office Visio has to be installed
		// --------------------------------------------------
		//Document file path
		wcscpy(wsDocPath, wsDocumentsPath);
		wcscat(wsDocPath, L"Visio.vsd");
		pNova->LicenseApplication(L"VISIO.EXE");
		pNova->LicenseApplication(L"SPLWOW64.EXE");
		hr = pNova->ConvertVisioDocument(wsDocPath, TRUE, TRUE, TRUE, TRUE, TRUE);
		

		//restore default profile
		pNova->SetActiveProfile(pwsOldActiveProfileID);

		//delete newly created profile
		pNova->DeleteProfile(pwsNewProfileID);
		//free memory
		CoTaskMemFree(pwsNewProfileID);
		if(pwsOldActiveProfileID)
		{
			CoTaskMemFree(pwsOldActiveProfileID);
		}
	}
	else
	{
		MessageBox(NULL, L"Failed to initialize novaPDF Printer", L"novaPDF", MB_OK); 
	}

	//release NovaPdfOptions
	pNova->Release();
	CoUninitialize();
	return 0;
}