Add PDF backgrounds in C++

Use BackgroundArtifact to add an image or solid color behind page content. All three examples modify page 1. Keep the image stream alive until the document is saved.

The input PDF must contain at least one page. Configure the Aspose.PDF for C++ SDK and use a writable output path for examples that save a PDF.

Add a background image

Open the image as a stream and use it as the background of the first page.

  1. Open the input PDF with Document.
  2. Open imageFile as a file stream.
  3. Create a BackgroundArtifact.
  4. Assign the stream with set_BackgroundImage().
  5. Add the artifact to the first page with ArtifactCollection::Add().
  6. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>

using namespace System;
using namespace Aspose::Pdf;

#include <Aspose.PDF.Cpp/Artifacts/BackgroundArtifact.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>

void AddBackgroundImageToPdf(System::String inputFile, System::String imageFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto imageStream = System::IO::File::OpenRead(imageFile);
    auto artifact = MakeObject<BackgroundArtifact>();
    artifact->set_BackgroundImage(imageStream);
    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);
    document->Save(outputFile);
    imageStream->Close();
}

Set background image opacity

Add the image background with an opacity of 0.5.

  1. Open the input PDF with Document.
  2. Open imageFile as a file stream and create a BackgroundArtifact.
  3. Set its image with set_BackgroundImage().
  4. Set opacity to 0.5 with Artifact::set_Opacity().
  5. Add the artifact to the first page with ArtifactCollection::Add().
  6. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/BackgroundArtifact.h>

#include <system/io/file.h>
#include <system/io/file_stream.h>

using namespace System;
using namespace Aspose::Pdf;

void AddBackgroundImageWithOpacityToPdf(System::String inputFile, System::String imageFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto imageStream = System::IO::File::OpenRead(imageFile);
    auto artifact = MakeObject<BackgroundArtifact>();
    artifact->set_BackgroundImage(imageStream);
    artifact->set_Opacity(0.5);
    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);
    document->Save(outputFile);
}

Add a background color

Create a DarkKhaki background using a PDF Color directly.

  1. Open the input PDF with Document.
  2. Create a BackgroundArtifact.
  3. Set DarkKhaki through set_BackgroundColor() using a Color.
  4. Add the artifact to the first page with ArtifactCollection::Add().
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/BackgroundArtifact.h>
#include <Aspose.PDF.Cpp/Color.h>

using namespace System;
using namespace Aspose::Pdf;

void AddBackgroundColorToPdf(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto artifact = MakeObject<BackgroundArtifact>();
    artifact->set_BackgroundColor(Color::get_DarkKhaki());
    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);
    document->Save(outputFile);
}

Moved inspection and removal guides

See Remove Backgrounds for this operation.