Skip to main content

Add an image

Add an image

A logo can sit alongside document content, while a background image can carry artwork across a page. Add an image to a PDF while preserving its proportions, or fill the page body with a background image. This example follows AddImageToPdf.cs: ImageExample adds a flowing image, and BackgroundImageExample creates a separate PDF with a background inside the page margins.

Fluent recipe / 04Add an imageAdd an image with preserved proportions or stretch a background within page margins.

What you’ll create​

  • With ImageExample, a PDF containing DPDFLogo.png at the start of the page body, preserving the image's aspect ratio.
  • With BackgroundImageExample, a PDF with DPDFLogo_Watermark.png stretched to fill the area inside the page margins.
  • AddImageToPdf_image_output.pdf and AddImageToPdf_background_image_output.pdf in your chosen output directory.

Run calls both methods. Each method saves a separate file directly in the output directory.

PDF output with the full-color DynamicPDF image.PDF output with the pale DynamicPDF background image.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class reads two images and saves each example to an existing output directory.

Ensure DPDFLogo.png and DPDFLogo_Watermark.png are in the Resources folder.

using System;
using System.IO;
using DynamicPDF.Flow;

namespace FluentExamples.Examples;

internal static class AddImageToPdf
{
public static void Run(string outputRoot)
{
ImageExample(outputRoot);
BackgroundImageExample(outputRoot);
}

public static void ImageExample(string outputRoot)
{
var imagePath = Path.Combine(
AppContext.BaseDirectory, "Resources",
"DPDFLogo.png");
var path = Path.Combine(
outputRoot, "AddImageToPdf_image_output.pdf");

DocumentBuilder.Create()
.Pages(page => page
.Image(imagePath, image => image
.AspectRatio(262, 250)))
.Save(path);

Console.WriteLine($"Created {path}");
}

public static void BackgroundImageExample(string outputRoot)
{
var imagePath = Path.Combine(
AppContext.BaseDirectory, "Resources",
"DPDFLogo_Watermark.png");
var path = Path.Combine(
outputRoot,
"AddImageToPdf_background_image_output.pdf");

DocumentBuilder.Create()
.Pages(page => page
.Background(background => background
.Image(imagePath)
.CoverBody()
.Fit(PageBackgroundFit.Stretch)))
.Save(path);

Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow

Friend Module AddImageToPdf
Public Sub Run(outputRoot As String)
ImageExample(outputRoot)
BackgroundImageExample(outputRoot)
End Sub

Public Sub ImageExample(outputRoot As String)
Dim imagePath = Path.Combine(
AppContext.BaseDirectory, "Resources",
"DPDFLogo.png")
Dim outputPath = Path.Combine(
outputRoot, "AddImageToPdf_image_output.pdf")

DocumentBuilder.Create().
Pages(Sub(page) page.
Image(imagePath, Sub(image) image.
AspectRatio(262, 250))).
Save(outputPath)

Console.WriteLine($"Created {outputPath}")
End Sub

Public Sub BackgroundImageExample(outputRoot As String)
Dim imagePath = Path.Combine(
AppContext.BaseDirectory, "Resources",
"DPDFLogo_Watermark.png")
Dim outputPath = Path.Combine(
outputRoot,
"AddImageToPdf_background_image_output.pdf")

DocumentBuilder.Create().
Pages(Sub(page) page.
Background(Sub(background) background.
Image(imagePath).
CoverBody().
Fit(PageBackgroundFit.Stretch))).
Save(outputPath)

Console.WriteLine($"Created {outputPath}")
End Sub
End Module

How it works​

1. Locate the images and choose the output directory​

Both methods use Path.Combine to locate their input image in Resources and construct an output path under outputRoot. Make sure the output directory exists before calling either method or Run.

2. Add a flowing image with ImageExample​

DocumentBuilder.Create() starts the document. Inside .Pages(...), .Image(imagePath, ...) adds DPDFLogo.png to the normal page content flow. Since it is the first content added, the image starts at the beginning of the page body within the margins.

The configuration callback receives an ImageElementBuilder. .AspectRatio(262, 250) sets the proportions using the original pixel width and height of DPDFLogo.png. These values define the ratio, not the displayed dimensions. This example does not set an explicit width, height, or position.

.Save(path) builds and saves AddImageToPdf_image_output.pdf directly through the fluent builder. Console.WriteLine prints the output path.

3. Add a background with BackgroundImageExample​

This method starts a separate document and uses .Background(...) to configure a page background. .Image(imagePath) selects DPDFLogo_Watermark.png, and .CoverBody() limits the background to the area inside the page margins.

.Fit(PageBackgroundFit.Stretch) stretches the background to fill that area. Unlike the flowing image in ImageExample, this background can change proportions to fit the page body. The method adds no foreground text or other content.

.Save(path) saves AddImageToPdf_background_image_output.pdf, and Console.WriteLine prints its path.

4. Keep both outputs​

Run calls ImageExample followed by BackgroundImageExample. Their distinct filenames keep both results in the same output directory without creating subfolders. Saving overwrites an existing file with the corresponding name.

Next steps​

Replace the resource images with your own and update .AspectRatio(...) to match the flowing image's dimensions, or add text to the page alongside the flowing image. Explore Create a PDF for text and document metadata, Password-protect and restrict permissions to protect your output, or browse all examples.