Add an Image as a Background in PDF Using C# and VB.NET
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. Using this library, you can programmatically add an image as a background in a PDF document using C# and VB.NET to enhance visual presentation and branding.
Steps to add an image as a background in a PDF document
1.Create a new project: Start a new console application in .NET core.
2.Install required packages: Add the Syncfusion.Pdf.Imaging.NET NuGet package to your project.
3.Include namespaces: In your Program.cs file, include the essential namespaces.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;Imports Syncfusion.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics4.Use the following code sample to add an image as background in a PDF document.
// Create the new PDF document
using (PdfDocument document = new PdfDocument())
{
//Set margin
document.PageSettings.Margins.All = 0;
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Get the client size
SizeF clientSize = page.GetClientSize();
// Load the background image from disk
using FileStream imageStream = new FileStream("Image.jpg"),
FileMode.Open,
FileAccess.Read
);
PdfBitmap image = new PdfBitmap(imageStream);
// Save the current graphics state, apply transparency, draw the image to cover the page, then restore.
PdfGraphicsState state = page.Graphics.Save();
page.Graphics.SetTransparency(0.2f); // 20% opacity for the background image
page.Graphics.DrawImage(
image,
new PointF(0, 0),
new SizeF(clientSize.Width, clientSize.Height)
);
page.Graphics.Restore(state);
// Define a margin for the text content.
const float margin = 40f;
// Text bounds: fill the page within margins.
RectangleF textBounds = new RectangleF(
margin,
margin,
clientSize.Width - margin * 2,
clientSize.Height - margin * 2
);
// Body font for the paragraph.
PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Regular);
// Sample paragraph text.
string paragraphText =
"Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases " +
"are based, is a large, multinational manufacturing company. The company manufactures and sells " +
"metal and composite bicycles to North American, European and Asian commercial markets. While " +
"its base operation is located in Washington with 290 employees, several regional sales teams " +
"are located throughout their market base.";
// Create a text element and configure layout to paginate if content exceeds current page.
PdfTextElement textElement = new PdfTextElement(paragraphText, bodyFont, PdfBrushes.Black);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
{
Break = PdfLayoutBreakType.FitPage, // Fit within page bounds
Layout = PdfLayoutType.Paginate // Continue to subsequent pages if needed
};
// Draw the text paragraph in the defined bounds.
textElement.Draw(page, textBounds, layoutFormat);
// Save the PDF document to disk.
document.Save("Output.pdf");
}' Create the new PDF document
Using document As New PdfDocument()
'Set margin
document.PageSettings.Margins.All = 0;
' Add a new page to the document
Dim page As PdfPage = document.Pages.Add()
' Get the client size
Dim clientSize As SizeF = page.GetClientSize()
' Load the background image from disk
Using imageStream As New FileStream("Image.jpg", FileMode.Open, FileAccess.Read)
Dim image As New PdfBitmap(imageStream)
' Save the current graphics state, apply transparency, draw the image to cover the page, then restore.
Dim state As PdfGraphicsState = page.Graphics.Save()
page.Graphics.SetTransparency(CSng(0.2)) ' 20% opacity for the background image
page.Graphics.DrawImage(
image,
New PointF(0, 0),
New SizeF(clientSize.Width, clientSize.Height)
)
page.Graphics.Restore(state)
' Define a margin for the text content.
Const margin As Single = 40.0F
' Text bounds: fill the page within margins.
Dim textBounds As New RectangleF(
margin,
margin,
clientSize.Width - margin * 2,
clientSize.Height - margin * 2
)
' Body font for the paragraph.
Dim bodyFont As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Regular)
' Sample paragraph text.
Dim paragraphText As String = _
"Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases " & _
"are based, is a large, multinational manufacturing company. The company manufactures and sells " & _
"metal and composite bicycles to North American, European and Asian commercial markets. While " & _
"its base operation is located in Washington with 290 employees, several regional sales teams " & _
"are located throughout their market base."
' Create a text element and configure layout to paginate if content exceeds current page.
Dim textElement As New PdfTextElement(paragraphText, bodyFont, PdfBrushes.Black)
Dim layoutFormat As New PdfLayoutFormat() With {
.Break = PdfLayoutBreakType.FitPage, ' Fit within page bounds
.Layout = PdfLayoutType.Paginate ' Continue to subsequent pages if needed
}
' Draw the text paragraph in the defined bounds.
textElement.Draw(page, textBounds, layoutFormat)
' Save the PDF document to disk.
document.Save("Output.pdf")
End UsingA complete working sample can be download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation. You’ll find additional options such as inserting images into new or existing PDF documents, working with vector images, applying image masking, replacing images in an existing PDF, implementing image pagination, and converting multi-page TIFF files to PDF.
Conclusion:
I hope you found this guide helpful in learning how to add an image as a background in a PDF document using C# and VB.NET.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!