As a powerful PDF component, Spire.PDF supports to work with page setting for PDF well, such as set PDF properties, view preference, and set background color etc. This article will focus on show you how to add image as page background to an existing PDF file in C#.
Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
Firstly, check the original PDF file without background image.
The following code snippet shows you how to add image as background for PDF in C#.
Step 1: Create a PDF document and load from file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf");
Step 2: Get the first page in PDF file
PdfPageBase page = doc.Pages[0];
Step 3: Load the image from file and set it as background image.
Image backgroundImage = Image.FromFile("background.png"); page.BackgroundImage = backgroundImage;
Step 4: Save the document to file and launch it.
doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
Effective Screenshot:
Full Codes:
using Spire.Pdf; using System.Drawing; namespace Addimagebackground { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); PdfPageBase page = doc.Pages[0]; Image backgroundImage = Image.FromFile("background.png"); page.BackgroundImage = backgroundImage; doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf"); } } }