Skip to content
ComPDF
DemoSampleAPI ReferenceFAQ
New Release

Open-Source PDF SDK & AI Document Processing

Get the full self-hosted SDK and AI document processing on GitHub. One-click deploy to quickly build your document workflows.

Background

The background refers to the underlying layer or pattern on the document pages, used to present the fundamental visual effect of the document. Adding a background can alter the document's appearance, making it more personalized or professional. It can be used to emphasize a brand, protect copyright, or enhance the reading experience of the document.

In a PDF document, only one background can exist, and adding a new background to pages containing an existing background will overwrite the old background.

Set Color Background

The steps to set a color background are as follows:

  1. Obtain the document's background object.

  2. Set the background type to color.

  3. Configure the properties of the background.

  4. Update the background of the document.

This example shows how to set the color background:

C#
CPDFBackground background = document.GetBackground();
background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
background.SetColor(new byte[] { 255, 0, 0 });
background.SetOpacity(255);// 0-255
background.SetScale(1);// 1 == 100%
background.SetRotation(0);// Units: Radians
background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
background.SetXOffset(0);
background.SetYOffset(0);
background.SetPages("0-2");
background.Update();

Set Image Background

The steps to set an image background are as follows:

  1. Obtain the document background object.

  2. Set the background type to an image.

  3. Specify the background properties.

  4. Update the background on the document.

This example shows how to set the image background:

C#
CPDFBackground background = document.GetBackground();
background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
byte[] imageData = new byte[500 * 500];
background.SetImage(imageData, 500, 500, ComPDFKit.Import.C_Scale_Type.fitCenter);
background.SetOpacity(128);// 0-255
background.SetScale(1);// 1 == 100%
background.SetRotation(1f);// Units: Radians
background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
background.SetXOffset(0);
background.SetYOffset(0);
background.SetPages("0-2");
background.Update();

Remove Background

The steps to remove the background are as follows:

  1. Obtain the document background object.

  2. Delete the document background.

This example shows how to remove background:

C#
CPDFBackground background = document.GetBackground();
background.Clear();