C++: Convert Word to PDF

PDF is one of the most versatile, universal and feature-rich file formats in use today. Compared to Word, PDF documents are less likely to lose formatting when they’re opened on various devices. Additionally, PDF has absolute advantages over Word in terms of document security, archiving and transmission. These are some of the reasons why we should convert Word to PDF. In this article, you will learn how to convert Word to PDF and how to set conversion options in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Convert Doc or Docx to PDF in C++

The Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method provided by Spire.Doc for C++ allows to save Word as PDF, XPS, HTML, RTF, etc. If you just want to save your Word documents as regular PDFs without additional settings, follow the steps below.

  • Create a Document object.
  • Load a sample Word file using Document->LoadFromFile() method.
  • Save the document to PDF using Doucment->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

	//Specify input file path
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(inputFilePath.c_str());

	//Save the document to PDF
	document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
	document->Close();
	delete document;
}

C++: Convert Word to PDF

Convert Word to PDF with Bookmarks in C++

Bookmarks can enhance the readability of a document. When generating PDF from Word, you may would like to preserve existing bookmarks of the Word document or create bookmarks from the headings. The following are the steps to convert Word to PDF with bookmarks.

  • Create a Document object.
  • Load a sample Word file using Document->LoadFromFile() method.
  • Create a ToPdfParameterList object, which is used to set conversion options.
  • Create bookmarks in PDF from the headings in Word using ToPdfParameterList.SetCreateWordBookmarksUsingHeadings() method.
  • Save the document to PDF with bookmarks using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

	//Specify input file path
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(inputFilePath.c_str());

	//Create a ToPdfParameterList object
	ToPdfParameterList* parameters = new ToPdfParameterList();

	//Create bookmarks from Word headings
	parameters->SetCreateWordBookmarksUsingHeadings(true);

	//Create bookmarks in PDF from existing bookmarks in Word
	//parameters->SetCreateWordBookmarks(true);

	//Save the document to PDF
	document->SaveToFile(outputFile.c_str(), parameters);
	document->Close();
	delete document;
}

C++: Convert Word to PDF

Convert Word to PDF with Fonts Embedded in C++

By embedding fonts used in a Word document into the PDF document, you ensure that the PDF document looks the same on any device that does not have the appropriate fonts installed. The steps to embed fonts in PDF during conversion are as follows.

  • Create a Document object.
  • Load a sample Word file using Document->LoadFromFile() method.
  • Create a ToPdfParameterList object, which is used to set conversion options.
  • Embed fonts in generated PDF using ToPdfParameterList.SetIsEmbeddedAllFonts() method.
  • Save the document to PDF using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

	//Specify input file path
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(inputFilePath.c_str());

	//Create a ToPdfParameterList object
	ToPdfParameterList* parameters = new ToPdfParameterList();

	//Embed fonts used in Word in generated PDF
	parameters->SetIsEmbeddedAllFonts(true);

	//Save the document to PDF
	document->SaveToFile(outputFile.c_str(), parameters);
	document->Close();
	delete document;
}

C++: Convert Word to PDF

Set Image Quality When Converting Word to PDF in C++

A document containing a large number of high-quality images will often be large in size. When you convert Word to PDF, you can decide whether to compress the image quality or not. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word file using Document->LoadFromFile() method.
  • Set the image quality using Document->SetJPEGQuality() mehtod
  • Save the document to PDF using Doucment->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

	//Specify input file path
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(inputFilePath.c_str());

	//Compress image to 40% of the original quality
	document->SetJPEGQuality(40);

	//Preserve original image quality
	//document->SetJPEGQuality(100);

	//Save the document to PDF
	document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
	document->Close();
	delete document;
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.