Add a Tiled Background Image to PDF in C#/VB.NET

2018-07-11 08:29:53 Written by  support iceblue
Rate this item
(0 votes)

A tiled background usually refers to the background that is filled with one or more repetitions of a small image. In this article, you will learn how to tile an image in PDF and make a tile background for your PDFs in C# and VB.NET.

Step 1: Create a PdfDocument object and load a sample PDF document.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

Step 2: Load an image file to PdfImage object.

PdfImage image = PdfImage.FromFile("logo.png");

Step 3: Create a PdfTilingBrush object specifying its size, set the transparency of the brush, and draw an image at the specified position of the brush.

PdfTilingBrush brush = new PdfTilingBrush(new SizeF(pdf.Pages[1].Canvas.Size.Width / 3, pdf.Pages[1].Canvas.Size.Height / 5));
brush.Graphics.SetTransparency(0.2f);
brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));

Step 4: Draw rectangles on the PDF page using the brush.

pdf.Pages[1].Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));

Step 5: Save the file.

pdf.SaveToFile("output.pdf");

Output:

How to Add a Tiled Background Image to PDF in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace Image
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("sample.pdf");

            PdfImage image = PdfImage.FromFile("logo.png");
            foreach (PdfPageBase page in pdf.Pages)
            {
                PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5));
                brush.Graphics.SetTransparency(0.2f);
                brush.Graphics.DrawImage(image, new PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2));
                page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));
            }
            pdf.SaveToFile("output.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace Image
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument()
			pdf.LoadFromFile("sample.pdf")

			Dim image As PdfImage = PdfImage.FromFile("logo.png")
			For Each page As PdfPageBase In pdf.Pages
				Dim brush As New PdfTilingBrush(New SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5))
				brush.Graphics.SetTransparency(0.2F)
				brush.Graphics.DrawImage(image, New PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2))
				page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.Size))
			Next
			pdf.SaveToFile("output.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Add a Tiled Background Image to PDF in C#, VB.NET
Last modified on Sunday, 26 September 2021 01:47