DEV Community

Liam Anderson
Liam Anderson

Posted on

Insert Watermarks into Word Documents with C++

A watermark is a faint image or text that appears behind the main text of a document. It can serve a variety of purposes, such as indicating the status of the document, deterring unauthorized distribution, or providing a visual reminder of ownership. By inserting a watermark into your document, you can not only add an extra layer of security to protect your content but also enhance its credibility and authenticity. This article will demonstrate how to insert watermarks into Word documents with C++.

Installation

In order to add watermarks to Word documents in C++, this article uses Spire.Doc for C++ library.

NuGet is the easiest way to integrate Spire.Doc for C++ into your application, and there are two ways to install Spire.Doc for C++ through NuGet.
1.Install Spire.Doc for C++ using Manage NuGet Packages.

  • Create or open your project in Visual Studio.
  • Click Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution…
  • Search for “Spire.Doc.Cpp” and click install.
    2.Install Spire.Doc for C++ using Package Manager Console.

  • Create or open your project in Visual Studio.

  • Click Tools -> NuGet Package Manager -> Package Manager Console.

  • Type the command “Install-Package Spire.Doc.Cpp” and press enter.
    Alternatively, you can also download the package from its official site and then manually import it into your project. For more details, you can check the documentation — How to Integrate Spire.Doc for C++ in a C++ Application.

Insert a Text Watermark into a Word Document in C++

You can generate a custom text watermark using the TextWatermark class. After that, you can use the Document->SetWatermark() method to add the text watermark to a Word document.

The following steps describe how to add a text watermark to a Word document:

  • Initialize an instance of the Document class.
  • Load a Word document using Document->LoadFromFile() method.
  • Initialize an instance of the TextWatermark class to create a text watermark.
  • Set text for the text watermark using TextWatermark->SetText() method.
  • Set other properties such as font size, color, and layout for the text watermark using TextWatermark->SetFontSize(), TextWatermark->SetColor(), and TextWatermark->SetLayout() methods.
  • Add the text watermark to the Word document using Document->SetWatermark() method.
  • Save the result document using Document->SaveToFile() method.
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace Spire::Common;

int main()
{
    //Initialize an instance of the Document class
    Document* document = new Document();
    //Load a Word document
    document->LoadFromFile(L"Input.docx");

    //Initialize an instance of the TextWatermark class to create a text watermark
    TextWatermark* txtWatermark = new TextWatermark();
    //Set watermark text
    txtWatermark->SetText(L"Approved");
    //Set font size, color, and layout properties for the text watermark
    txtWatermark->SetFontSize(95);
    txtWatermark->SetColor(Color::GetBlue());
    txtWatermark->SetLayout(WatermarkLayout::Diagonal);

    //Add the text watermark to the document
    document->SetWatermark(txtWatermark);

    //Save the result document
    document->SaveToFile(L"AddTextWatermark.docx", FileFormat::Docx);
    document->Close();
    delete document;
}
Enter fullscreen mode Exit fullscreen mode

Add Text Watermark to Word Document with C++

Insert an Image Watermark into a Word Document in C++

You can generate an image watermark using the PictureWatermark class. After that, you can use the Document->SetWatermark() method to add the image watermark to a Word document.

The following steps describe how to add an image watermark to a Word document:

  • Initialize an instance of the Document class.
  • Load a Word document using Document->LoadFromFile() method.
  • Initialize an instance of the PictureWatermark class to create an image watermark.
  • Set the image for the image watermark using PictureWatermark->SetPicture() method.
  • Set other properties such as scaling and washout for the image watermark using PictureWatermark->SetScaling() and PictureWatermark->SetIsWashout() methods.
  • Add the image watermark to the Word document using Document->SetWatermark() method.
  • Save the result document using Document->SaveToFile() method.
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace Spire::Common;

int main()
{
    //Initialize an instance of the Document class
    Document* document = new Document();
    //Load a Word document
    document->LoadFromFile(L"Input.docx");

    //Initialize an instance of the PictureWatermark class to create an image watermark
    PictureWatermark* pictureWatermark = new PictureWatermark();

    //Set watermark image
    pictureWatermark->SetPicture(L"logo.png");
    //Set scaling and washout properties for the image watermark
    pictureWatermark->SetScaling(100);
    pictureWatermark->SetIsWashout(false);

    //Add the image watermark to the document
    document->SetWatermark(pictureWatermark);

    //Save the result document
    document->SaveToFile(L"AddImageWatermark.docx", FileFormat::Docx);
    document->Close();
    delete document;
}
Enter fullscreen mode Exit fullscreen mode

Add Image Watermark to Word Document with C++

Top comments (0)