Knowledgebase (2345)
Children categories
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;
}

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;
}

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.
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();
}

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();
}

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.
Converting Excel to HTML makes it easier to embed your spreadsheet data on a website and also ensures that other people can view the document online in their browsers without opening Excel. In this article, you will learn how to convert Excel to HTML using Spire.XLS for C++.
- Convert an Entire Excel Workbook to HTML in C++
- Convert a Specific Worksheet to HTML with Images Embedded in C++
- Convert a Specific Worksheet to HTML Stream in C++
Install Spire.XLS for C++
There are two ways to integrate Spire.XLS 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.XLS for C++ in a C++ Application
Convert an Entire Excel Workbook to HTML in C++
The Workbook->SaveToHtml() method provided by Spire.XLS for C++ allows you to convert the whole workbook to HTML. The following are the detailed steps.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Save the entire Excel workbook as an HTML file using Workbook->SaveToHtml() method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
int main()
{
//Specify input file path and name
std::wstring data_path = L"Data\\";
std::wstring inputFile = data_path + L"input.xlsx";
//Specify output file path and name
std::wstring outputPath = L"Output\\";
std::wstring outputFile = outputPath + L"ToHtml.html";
//Create a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load an Excel document from disk
workbook->LoadFromFile(inputFile.c_str());
//Save the whole Excel workbook to Html
workbook->SaveToHtml(outputFile.c_str());
workbook->Dispose();
}

Convert a Specified Worksheet to HTML with Images Embedded in C++
If you want the images in a worksheet to be embedded into the HTML code while conversion, you can set the parameter of HTMLOptions->SetImageEmbedded() method to true. The following are the detailed steps.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Create an HTMLOptions instance and enable image embedding using HTMLOptions->SetImageEmbedded(true) method.
- Save the worksheet to HTML with image embedded using Worksheet->SaveToHtml(LPCWSTR_S fileName, HTMLOptions* saveOption) method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
int main()
{
//Specify input file path and name
std::wstring data_path = L"Data\\";
std::wstring inputFile = data_path + L"sample.xlsx";
//Specify output file path and name
std::wstring outputPath = L"Output\\";
std::wstring outputFile = outputPath + L"ExcelToHtml.html";
//Create a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load an Excel document from disk
workbook->LoadFromFile(inputFile.c_str());
//Get the first worksheet
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//Set embedded image as true
intrusive_ptr<HTMLOptions> options = new HTMLOptions();
options->SetImageEmbedded(true);
//Save the worksheet to HTML
sheet->SaveToHtml(outputFile.c_str(), options);
workbook->Dispose();
}

Convert a Specified Worksheet to HTML Stream in C++
Spire.XLS for C++ also allows you to convert a worksheet to HTML stream using Worksheet->SaveToHtml(Stream* stream, HTMLOptions* saveOption) method. The following are the detailed steps.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Create an HTMLOptions instance and enable image embedding using HTMLOptions->SetImageEmbedded(true) method.
- Create a stream and save the worksheet to HTML stream with image embedded using Worksheet->SaveToHtml(Stream* stream, HTMLOptions* saveOption) method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
int main()
{
//Specify input file path and name
std::wstring data_path = L"Data\\";
std::wstring inputFile = data_path + L"sample.xlsx";
//Specify output file path and name
std::wstring outputPath = L"Output\\";
std::wstring outputFile = outputPath + L"ToHtmlStream.html";
//Create a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load an Excel document from disk
workbook->LoadFromFile(inputFile.c_str());
//Get the first worksheet
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//Set embedded image as true
intrusive_ptr<HTMLOptions> options = new HTMLOptions();
options->SetImageEmbedded(true);
//Create a stream
intrusive_ptr<Stream> stream = new Stream();
//Save worksheet to html stream
sheet->SaveToHtml(stream, options);
workbook->Dispose();
stream->Save(outputFile.c_str());
}
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.