When you need to share or present your PowerPoint presentations on different computers/devices, you may occasionally find that some content cannot be displayed properly. To avoid such incompatibility issues, a common method is to convert your PowerPoint document to PDF to ensure document integrity. In this article, you will learn how to convert a PowerPoint Presentation to PDF in C++ using Spire.Presentation for C++.

Install Spire.Presentation for C++

There are two ways to integrate Spire.Presentation 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.Presentation for C++ in a C++ Application

Convert an Entire PowerPoint Presentation to PDF in C++

The Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method allows you to convert each slide in PowerPoint to a PDF page. The following are the steps to convert a whole PowerPoint presentation to PDF.

  • C++
#include "Spire.Presentation.o.h";

using namespace Spire::Presentation;
using namespace std;

int main()
{
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\sample.pptx";
	std::wstring outputFile = L"Output\\PowerPointToPDF.pdf";

	//Create a Presentation object
	intrusive_ptr⁢Presentation> ppt = new Presentation();

	//Load a PowerPoint document from disk
	ppt->LoadFromFile(inputFile.c_str());

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

	ppt->Dispose();

}

C++: Convert PowerPoint Presentation to PDF

Convert a Specific PowerPoint Slide to PDF in C++

If you only want to convert a particular slide to PDF, you can use the ISlide->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method provided by Spire.Presentation for C++. The following are the detailed steps.

  • Create a Presentation object.
  • Load a PowerPoint presentation using Presentation->LoadFromFile() method.
  • Get a specified slide by index using Presentation->GetSlides()->GetItem(slideIndex) method.
  • Save the slide to PDF using ISlide->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method.
  • C++
#include "Spire.Presentation.o.h";

using namespace Spire::Presentation;
using namespace std;

int main()
{
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\sample.pptx";
	std::wstring outputFile = L"Output\\SlideToPDF.pdf";

	//Create a Presentation object
	intrusive_ptr⁢Presentation> ppt = new Presentation();

	//Load a PowerPoint document from disk
	ppt->LoadFromFile(inputFile.c_str());

	//Get the second slide
	intrusive_ptr slide = ppt->GetSlides()->GetItem(1);

	//Save the second slide to PDF
	slide->SaveToFile(outputFile.c_str(), FileFormat::PDF);
	ppt->Dispose();
}

C++: Convert PowerPoint Presentation to PDF

Convert a PowerPoint to PDF with Specific Page Size in C++

Spire.Presentation for C++ also allows you to set a desired slide size and orientation for a PowerPoint document before converting it to PDF. The following are the steps to convert a PowerPoint to PDF with Specific Page Size (A4 slide size = 10.83x7.05 inch).

  • Create a Presentation object.
  • Load a PowerPoint presentation using Presentation->LoadFromFile() method.
  • Set the slide size of the PowerPoint document using Presentation->GetSlideSize()->SetType() method.
  • Set the slide orientation of the PowerPoint document using Presentation->GetSlideSize()->SetOrientation() method.
  • Save the document to PDF using Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method.
  • C++
#include "Spire.Presentation.o.h";

using namespace Spire::Presentation;
using namespace std;

int main()
{
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\sample.pptx";
	std::wstring outputFile = L"Output\\ToPdfWithSpecificPageSize.pdf";

	//Create a Presentation object
	intrusive_ptr⁢Presentation> ppt = new Presentation();

	//Load a PowerPoint document from disk
	ppt->LoadFromFile(inputFile.c_str());

	//Set the slide size to A4
	ppt->GetSlideSize()->SetType(SlideSizeType::A4);

	//Set the slide orientation to Landscape
	ppt->GetSlideSize()->SetOrientation(SlideOrienation::Landscape);

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

	ppt->Dispose();
}

C++: Convert PowerPoint Presentation to PDF

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.

Watermarks are faded text or images that appear behind the existing content of a document. You can use them to indicate a document's state (confidential, draft, etc.), or add a subtle company logo. A watermark helps prove the origin of a document and discourage unauthorized copies or distribution. In this article, you will learn how to add image watermarks to PDF in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF 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.PDF for C++ in a C++ Application

Add a Single Image Watermark to PDF in C++

The PdfPageBase->GetCanvas()->DrawImage() method provided by Spire.PDF for C++ allows developers to draw images at any position of a PDF page. The image can be faded by adjusting the transparency of the canvas to prevent it from overwriting the text. The detailed steps to add a single image watermark to PDF are as follows.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Get a specific page from the document using PdfDocument->GetPages()->GetItem() method.
  • Set the transparency of the page using PdfPageBase->GetCanvas()->SetTransparency() method.
  • Draw an image at the center of the page using PdfPageBase->GetCanvas()->DrawImage() method.
  • Save the document to a different PDF file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;

int main()
{
	//Specify input file and output file paths
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFilePath = L"C:\\Users\\Administrator\\Desktop\\ImageWatermark.pdf";

	//Create a PdfDocument object
	PdfDocument* doc = new PdfDocument();

	//Load a PDF file
	doc->LoadFromFile(inputFilePath.c_str());

	//Load an image
	boost::intrusive_ptr image = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\logo.png");

	//Get image height and width
	int imgHeight = image->GetHeight();
	int imgWidth = image->GetWidth();

	//Traverse through the pages in the document
	for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
	{
		//Get a specific page
		boost::intrusive_ptr page = doc->GetPages()->GetItem(i);

		//Set the page transparency
		page->GetCanvas()->SetTransparency(0.5);

		//Get the page width and height
		float pageWidth = page->GetActualSize()->GetWidth();
		float pageHeight = page->GetActualSize()->GetHeight();

		//Draw image at the center of the page
		RectangleF* rect = new RectangleF((float)(pageWidth - imgWidth) / 2, (float)(pageHeight - imgHeight) / 2, imgWidth, imgHeight);
		page->GetCanvas()->DrawImage(image, rect);
	}

	//Save the document
	doc->SaveToFile(outputFilePath.c_str());
	doc->Close();
	delete doc;
}

C++: Add Image Watermarks to PDF Documents

Add a Tiled Image Watermark to PDF in C++

A tiled watermark effect can be achieved by using the PdfTilingBrush class. The tiling brush produces a tiled pattern that is repeated to fill a graphics area. The following are the steps to add a tiled image watermark to a PDF document.

  • Create a custom method InsertTiledImagetWatermark(PdfPageBase* page, PdfImage* image, int rowNum, int columnNum) to add a tiled watermark to a PDF page. The parameter rowNum and columnNum specify the row number and column number of the tiled watermark.
  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Traverse through all pages in the document, and invoke the custom method InsertTiledImageWatermark() to apply watermark to each page.
  • Save the document to another file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;

static void InsertTiledImagetWatermark(PdfPageBase* page, PdfImage* image, int rowNum, int columnNum)
{
	//Get page height and width
	float height = page->GetActualSize()->GetHeight();
	float width = page->GetActualSize()->GetWidth();

	//Get image heigh and width
	float imgHeight = image->GetHeight();
	float imgWidth = image->GetWidth();

	//Create a tiling brush
	PdfTilingBrush* brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum));

	//Set transparency
	brush->GetGraphics()->SetTransparency(0.5f);

	//Translate coordinate system to a certain position
	brush->GetGraphics()->TranslateTransform((brush->GetSize()->GetWidth() - imgWidth) / 2, (brush->GetSize()->GetHeight() - imgHeight) / 2);

	//Draw image on the brush at the specified coordinates
	brush->GetGraphics()->DrawImage(image, 0.0, 0.0);

	//Draw rectangle that covers the whole page with the brush
	page->GetCanvas()->DrawRectangle(brush, 0, 0, width, height);
}

int main()
{
	//Specify input file and output file paths
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFilePath = L"C:\\Users\\Administrator\\Desktop\\TiledImageWatermark.pdf";

	//Create a PdfDocument object
	PdfDocument* doc = new PdfDocument();

	//Load a PDF file
	doc->LoadFromFile(inputFilePath.c_str());

	//Load an image
	PdfImage* image = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\small-logo.png");

	//Traverse through the pages in the document
	for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
	{
		//Get a specific page
		PdfPageBase* page = doc->GetPages()->GetItem(i);

		//Add tiled image watermark to the page
		InsertTiledImagetWatermark(page, image, 4, 4);
	}

	//Save the document
	doc->SaveToFile(outputFilePath.c_str());
	doc->Close();
	delete doc;
}

C++: Add Image Watermarks to PDF Documents

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.

C++: Create Tables in Word Documents

2023-03-08 01:26:00 Written by hayes Liu

A table is a powerful tool for organizing and presenting data. It arranges data into rows and columns, making it easier for authors to illustrate the relationships between different data categories and for readers to understand and analyze complex data. In this article, you will learn how to programmatically create tables in Word documents 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

Create a Table in Word in C++

Spire.Doc for C++ offers the Section->AddTable() method to add a table to a section of a Word document. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Add a section to the document using Document->AddSection() method.
  • Define the data for the header row and remaining rows, storing them in a one-dimensional vector and a two-dimensional vector respectively.
  • Add a table to the section using Section->AddTable() method.
  • Specify the number of rows and columns in the table using Table->ResetCells(int, int) method.
  • Add data in the one-dimensional vector to the header row and set formatting.
  • Add data in the two-dimensional vector to the remaining rows and set formatting.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> doc = new Document();

	//Add a section to the document
	intrusive_ptr<Section> section = doc->AddSection();

	//Set page margins for the section
	section->GetPageSetup()->GetMargins()->SetAll(72);

	//Define the data for the header row
	vector<wstring> header = { L"Name", L"Capital", L"Continent", L"Area", L"Population" };
	//Define the data for the remaining rows
	vector<vector<wstring>> data =
	{
		{L"Argentina", L"Buenos Aires", L"South America", L"2777815", L"32300003"},
		{L"Bolivia", L"La Paz", L"South America", L"1098575", L"7300000"},
		{L"Brazil", L"Brasilia", L"South America", L"8511196", L"150400000"},
		{L"Canada", L"Ottawa", L"North America", L"9976147", L"26500000"},
		{L"Chile", L"Santiago", L"South America", L"756943", L"13200000"},
		{L"Colombia", L"Bogota", L"South America", L"1138907", L"33000000"},
		{L"Cuba", L"Havana", L"North America", L"114524", L"10600000"},
		{L"Ecuador", L"Quito", L"South America", L"455502", L"10600000"},
		{L"El Salvador", L"San Salvador", L"North America", L"20865", L"5300000"},
		{L"Guyana", L"Georgetown", L"South America", L"214969", L"800000"},
		{L"Jamaica", L"Kingston", L"North America", L"11424", L"2500000"},
		{L"Mexico", L"Mexico City", L"North America", L"1967180", L"88600000"},
		{L"Nicaragua", L"Managua", L"North America", L"139000", L"3900000"},
		{L"Paraguay", L"Asuncion", L"South America", L"406576", L"4660000"},
		{L"Peru", L"Lima", L"South America", L"1285215", L"21600000"},
		{L"United States", L"Washington", L"North America", L"9363130", L"249200000"},
		{L"Uruguay", L"Montevideo", L"South America", L"176140", L"3002000"},
		{L"Venezuela", L"Caracas", L"South America", L"912047", L"19700000"}
	};

	//Add a table to the section
	intrusive_ptr<Table> table = section->AddTable(true);
	//Specify the number of rows and columns for the table
	table->ResetCells(data.size() + 1, header.size());

	//Set the first row as the header row
	intrusive_ptr<TableRow> row = table->GetRows()->GetItemInRowCollection(0);
	row->SetIsHeader(true);

	//Set height and background color for the header row
	row->SetHeight(20);
	row->SetHeightType(TableRowHeightType::Exactly);
	for (int i = 0; i < row->GetCells()->GetCount(); i++)
	{
		row->GetCells()->GetItemInCellCollection(i)->GetCellFormat()->GetShading()->SetBackgroundPatternColor(Color::FromArgb(142, 170, 219));
	}

	//Add data to the header row and set formatting
	for (size_t i = 0; i < header.size(); i++)
	{
		//Add a paragraph
		intrusive_ptr<Paragraph> p1 = row->GetCells()->GetItemInCellCollection(i)->AddParagraph();
		//Set alignment
		p1->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
		row->GetCells()->GetItemInCellCollection(i)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
		//Add data
		intrusive_ptr<TextRange> tR1 = p1->AppendText(header[i].c_str());
		//Set data formatting
		tR1->GetCharacterFormat()->SetFontName(L"Calibri");
		tR1->GetCharacterFormat()->SetFontSize(12);
		tR1->GetCharacterFormat()->SetBold(true);
	}

	//Add data to the remaining rows and set formatting
	for (size_t r = 0; r < data.size(); r++)
	{
		//Set height for the remaining rows
		intrusive_ptr<TableRow> dataRow = table->GetRows()->GetItemInRowCollection(r + 1);
		dataRow->SetHeight(20);
		dataRow->SetHeightType(TableRowHeightType::Exactly);

		for (size_t c = 0; c < data[r].size(); c++)
		{
			//Add a paragraph
			intrusive_ptr<Paragraph> p2 = dataRow->GetCells()->GetItemInCellCollection(c)->AddParagraph();
			//Set alignment
			dataRow->GetCells()->GetItemInCellCollection(c)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
			//Add data
			intrusive_ptr<TextRange> tR2 = p2->AppendText(data[r][c].c_str());
			//Set data formatting
			tR2->GetCharacterFormat()->SetFontName(L"Calibri");
			tR2->GetCharacterFormat()->SetFontSize(11);
		}
	}

	//Save the result document
	doc->SaveToFile(L"CreateTable.docx", FileFormat::Docx2013);
	doc->Close();
}

C++: Create Tables in Word Documents

Create a Nested Table in Word in C++

Spire.Doc for C++ offers the TableCell->AddTable() method to add a nested table to a specific table cell. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Add a section to the document using Document->AddSection() method.
  • Add a table to the section using Section.AddTable() method.
  • Specify the number of rows and columns in the table using Table->ResetCells(int, int) method.
  • Get the rows of the table and add data to the cells of each row.
  • Add a nested table to a specific table cell using TableCell->AddTable() method.
  • Specify the number of rows and columns in the nested table.
  • Get the rows of the nested table and add data to the cells of each row.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> doc = new Document();
	//Add a section to the document
	intrusive_ptr<Section> section = doc->AddSection();

	//Set page margins for the section
	section->GetPageSetup()->GetMargins()->SetAll(72);

	//Add a table to the section
	intrusive_ptr<Table> table = section->AddTable(true);
	//Set the number of rows and columns in the table
	table->ResetCells(2, 2);

	//Autofit the table width to window
	table->AutoFit(AutoFitBehaviorType::AutoFitToWindow);

	//Get the table rows
	intrusive_ptr<TableRow> row1 = table->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> row2 = table->GetRows()->GetItemInRowCollection(1);

	//Add data to cells of the table
	intrusive_ptr<TableCell> cell1 = row1->GetCells()->GetItemInCellCollection(0);
	intrusive_ptr<TextRange> tR = cell1->AddParagraph()->AppendText(L"Product");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell2 = row1->GetCells()->GetItemInCellCollection(1);
	tR = cell2->AddParagraph()->AppendText(L"Description");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell3 = row2->GetCells()->GetItemInCellCollection(0);
	cell3->AddParagraph()->AppendText(L"Spire.Doc for C++");
	intrusive_ptr<TableCell> cell4 = row2->GetCells()->GetItemInCellCollection(1);
	cell4->AddParagraph()->AppendText(L"Spire.Doc for C++ is a professional Word "
		L"library specifically designed for developers to create, "
		L"read, write and convert Word documents in C++ "
		L"applications with fast and high-quality performance.");

	//Add a nested table to the fourth cell
	intrusive_ptr<Table> nestedTable = cell4->AddTable(true);
	//Set the number of rows and columns in the nested table
	nestedTable->ResetCells(3, 2);

	//Autofit the table width to content
	nestedTable->AutoFit(AutoFitBehaviorType::AutoFitToContents);

	//Get table rows
	intrusive_ptr<TableRow> nestedRow1 = nestedTable->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> nestedRow2 = nestedTable->GetRows()->GetItemInRowCollection(1);
	intrusive_ptr<TableRow> nestedRow3 = nestedTable->GetRows()->GetItemInRowCollection(2);

	//Add data to cells of the nested table
	intrusive_ptr<TableCell> nestedCell1 = nestedRow1->GetCells()->GetItemInCellCollection(0);
	tR = nestedCell1->AddParagraph()->AppendText(L"Item");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell2 = nestedRow1->GetCells()->GetItemInCellCollection(1);
	tR = nestedCell2->AddParagraph()->AppendText(L"Price");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell3 = nestedRow2->GetCells()->GetItemInCellCollection(0);
	nestedCell3->AddParagraph()->AppendText(L"Developer Subscription");
	intrusive_ptr<TableCell> nestedCell4 = nestedRow2->GetCells()->GetItemInCellCollection(1);
	nestedCell4->AddParagraph()->AppendText(L"$999");
	intrusive_ptr<TableCell> nestedCell5 = nestedRow3->GetCells()->GetItemInCellCollection(0);
	nestedCell5->AddParagraph()->AppendText(L"Developer OEM Subscription");
	intrusive_ptr<TableCell> nestedCell6 = nestedRow3->GetCells()->GetItemInCellCollection(1);
	nestedCell6->AddParagraph()->AppendText(L"$2999");

	//Save the result document
	doc->SaveToFile(L"CreateNestedTable.docx", FileFormat::Docx2013);
	doc->Close();
}

C++: Create Tables in Word Documents

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.

page 90