News Category

C#/VB.NET: Add Background Color or Background Image to PDF

2023-07-17 06:14:00 Written by  support iceblue
Rate this item
(4 votes)

In a PDF document, the background refers to the overall visual appearance behind the content of the pages. The background can be a simple solid color or an image of your choice. Adding backgrounds to PDFs can help you add visual interest to your documents and also enhance readability. In this article, you will learn how to programmatically set the background color or image for PDF using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Add Background Color to PDF Documents in C# and VB.NET

The PdfPageBase.BackgroundColor property offered by Spire.PDF for .NET allows you to set a solid color as the PDF background. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background color to each page using PdfPageBase.BackgroundColor property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the background color for each page
                page.BackgroundColor = Color.Yellow;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.1f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundColor.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to PDF

Add Background Images to PDF Documents C# and VB.NET

If you want to add an image as a background to match the document theme, you can use the PdfPageBase.BackgroundImage property. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background picture to each page using PdfPageBase.BackgroundImage property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Load an image
            Image background = Image.FromFile("background.png");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the loaded image as the background image for each page
                page.BackgroundImage = background;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.2f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundImage.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to PDF

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.

Additional Info

  • tutorial_title:
Last modified on Monday, 17 July 2023 01:09