Usually, when you open a PDF document, the blank background looks a little drab. You might want to create background for your PDF document to make them more visually appealing through the using of a background image.
In the following sections, I will demonstrate how to insert PDF background image in WPF.
Here is the original PDF document without background image.
The code snippets are as followed:
Step 1: Initialize a new instance of PdfDocument class and load the PDF document from the file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("To a Skylark.pdf");
Step 2: In this example, we choose the first page of PDF file to insert the background image.
PdfPageBase page = doc.Pages[0];
Step 3: Load the image from file and set it as background image.
System.Drawing.Image backgroundImage = System.Drawing.Image.FromFile("Sky.jpg"); page.BackgroundImage = backgroundImage;
Step 4: Save the PDF document and launch the file.
doc.SaveToFile("With Background Image.pdf"); System.Diagnostics.Process.Start("With Background Image.pdf");
Effective screenshot:
Full Codes:
[C#]
using System.Windows; using Spire.Pdf; namespace Poetic_Works { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button2_Click(object sender, RoutedEventArgs e) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("To a Skylark.pdf"); PdfPageBase page = doc.Pages[0]; System.Drawing.Image backgroundImage = System.Drawing.Image.FromFile("Sky.jpg"); page.BackgroundImage = backgroundImage; doc.SaveToFile("With Background Image.pdf"); System.Diagnostics.Process.Start("With Background Image.pdf"); } } }
[VB.NET]
Imports System.Windows Imports Spire.Pdf Namespace Poetic_Works ''' ''' Interaction logic for MainWindow.xaml ''' Public Partial Class MainWindow Inherits Window Public Sub New() InitializeComponent() End Sub Private Sub button2_Click(sender As Object, e As RoutedEventArgs) Dim doc As New PdfDocument() doc.LoadFromFile("To a Skylark.pdf") Dim page As PdfPageBase = doc.Pages(0) Dim backgroundImage As System.Drawing.Image = System.Drawing.Image.FromFile("Sky.jpg") page.BackgroundImage = backgroundImage doc.SaveToFile("With Background Image.pdf") System.Diagnostics.Process.Start("With Background Image.pdf") End Sub End Class End Namespace