C++: Insert Text or Image Watermarks to Word

In Word, a watermark is a semi-transparent text or image that you can place in the background. Typically, watermarks are used to emphasize something important about a document. For example, you can use it to remind the user that the content is confidential or a draft. Or other times, you may want to add a washout mark to include the company logo in the document. In this article, you will learn how to insert text or image watermarks to 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

Insert a Text Watermark to Word in C++

Spire.Doc for C++ offers the TextWatermark class to represent a text watermark. The content and appearance of the watermark can be set using the methods under it. Once a text watermark is created, you can apply it to the whole document by using Document->SetWatermark() method. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a TextWatermark object.
  • Se the content and appearance of the watermark using the methods under the TextWatermark object.
  • Apply the text watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

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

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a TextWatermark object
    TextWatermark* txtWatermark = new TextWatermark();

    //Set the content and format of the text watermark
    txtWatermark->SetText(L"CONFIDENTIAL");
    txtWatermark->SetFontSize(40);
    txtWatermark->SetSemitransparent(90);
    txtWatermark->SetFontName(L"Arial Black");
    txtWatermark->SetColor(Color::GetBlue());
    txtWatermark->SetLayout(WatermarkLayout::Diagonal);

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

    //Save the document 
    document->SaveToFile(L"Output/TextWatermark.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Insert Text or Image Watermarks to Word

Insert an Image Watermark to Word in C++

Likewise, an image watermark can be created using PictureWatermark class. Once created, you can apply it to a Word document using Document->SetWatermark() method. Here are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a PictureWatermark object.
  • Set the image and appearance of the watermark using the methods under the PictureWatermark object.
  • Apply the image watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

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

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a PictureWatermark object
    PictureWatermark* pictureWatermark = new PictureWatermark();

    //Specify image for the watermark
    pictureWatermark->SetPicture(Image::FromFile(L"C:\\Users\\Administrator\\Desktop\\company-png.png"));
    pictureWatermark->SetScaling(100);
    pictureWatermark->SetIsWashout(false);

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

    //Save the document 
    document->SaveToFile(L"Output/ImageWatermark.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Insert Text or Image Watermarks to Word

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.