Skip to main content

Add content to an existing PDF

Add content to an existing PDF

An existing PDF can serve as the starting point for a revised document or a branded packet. Add text to an existing PDF page, prepend a cover with a logo, or insert a new page between existing pages. This example follows AddNewContentToExistingPdf.cs and saves three PDFs directly in your chosen output directory.

Fluent recipe / 20Add content to an existing PDFAdd red text to a PDF page, prepend a cover with a centered logo, and insert a new page.

What you’ll create​

Output fileResult
AddNewContentToExistingPdf_content_output.pdfAll four pages of DocumentC.pdf, with red, 24-point text reading “New Content” on page 2.
AddNewContentToExistingPdf_cover_output.pdfA Letter cover with a centered logo above red, 42-point text reading “Cover Page,” followed by both pages of DocumentA.pdf.
AddNewContentToExistingPdf_insert_output.pdfA new Letter page containing “Cover Page” between the two pages of DocumentA.pdf.
New Content in red on Document C page 2, alongside a cover with a centered DynamicPDF logo above red Cover Page text.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. Ensure DocumentA.pdf, DocumentC.pdf, and DPDFLogo.png are in the Resources folder.

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

namespace FluentExamples.Examples;

internal static class AddNewContentToExistingPdf
{
public static void Run(string outputRoot)
{
AddContent(outputRoot);
AddPage(outputRoot);
InsertPage(outputRoot);
}

private static void AddContent(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var documentC = PdfSource.FromFile(
Path.Combine(resources, "DocumentC.pdf"));

var path = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"content_output.pdf");

DocumentBuilder.Create()
.Pdf(documentC, 1, 1)
.Pages(page => page
.Size(PageSize.Letter)
.Margins(0, LengthUnit.Point)
.PdfPage(documentC, 2, bg => bg.Natural())
.Text("New Content", text => text
.FontFamily("Helvetica")
.FontSize(24, LengthUnit.Point)
.Color("red")))
.Pdf(documentC, 3, documentC.PageCount - 2)
.Save(path);

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

private static void AddPage(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var documentA = PdfSource.FromFile(
Path.Combine(resources, "DocumentA.pdf"));

var logo = Path.Combine(resources, "DPDFLogo.png");
const double logoWidth = 262.0 / 3;
const double logoHeight = 250.0 / 3;
const double logoMargin = (612 - logoWidth) / 2;

var path = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"cover_output.pdf");

DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.Letter)
.Margins(0, LengthUnit.Point)
.Spacing(0, LengthUnit.Point)
.Image(logo, image => image
.Width(logoWidth, LengthUnit.Point)
.Height(logoHeight, LengthUnit.Point)
.Margin(372 - logoHeight - 12,
logoMargin, 12, logoMargin,
LengthUnit.Point))
.Text("Cover Page", text => text
.FontFamily("Helvetica", "Arial")
.FontSize(42, LengthUnit.Point)
.Color("red")
.Align(TextAlign.Center)))
.Pdf(documentA)
.Save(path);

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

private static void InsertPage(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var documentA = PdfSource.FromFile(
Path.Combine(resources, "DocumentA.pdf"));

var path = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"insert_output.pdf");

DocumentBuilder.Create()
.Pdf(documentA, 1, 1)
.Pages(page => page
.Size(PageSize.Letter)
.Text("Cover Page"))
.Pdf(documentA, 2, documentA.PageCount - 1)
.Save(path);

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

Friend Module AddNewContentToExistingPdf
Public Sub Run(outputRoot As String)
AddContent(outputRoot)
AddPage(outputRoot)
InsertPage(outputRoot)
End Sub

Private Sub AddContent(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim documentC = PdfSource.FromFile(
Path.Combine(resources, "DocumentC.pdf"))

Dim outputPath = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"content_output.pdf")

DocumentBuilder.Create().
Pdf(documentC, 1, 1).
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(0, unit:=LengthUnit.Point).
PdfPage(documentC, 2, Sub(bg) bg.Natural()).
Text("New Content", Sub(text) text.
FontFamily("Helvetica").
FontSize(24, LengthUnit.Point).
Color("red"))).
Pdf(documentC, 3, documentC.PageCount - 2).
Save(outputPath)

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

Private Sub AddPage(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim documentA = PdfSource.FromFile(
Path.Combine(resources, "DocumentA.pdf"))

Dim logo = Path.Combine(resources, "DPDFLogo.png")
Const logoWidth As double = 262.0 / 3
Const logoHeight As double = 250.0 / 3
Const logoMargin As double = (612 - logoWidth) / 2

Dim outputPath = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"cover_output.pdf")

DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(0, unit:=LengthUnit.Point).
Spacing(0, LengthUnit.Point).
Image(logo, Sub(image) image.
Width(logoWidth, LengthUnit.Point).
Height(logoHeight, LengthUnit.Point).
Margin(372 - logoHeight - 12,
logoMargin, 12, logoMargin,
LengthUnit.Point)).
Text("Cover Page", Sub(text) text.
FontFamily("Helvetica", "Arial").
FontSize(42, LengthUnit.Point).
Color("red").
Align(TextAlign.Center))).
Pdf(documentA).
Save(outputPath)

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

Private Sub InsertPage(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim documentA = PdfSource.FromFile(
Path.Combine(resources, "DocumentA.pdf"))

Dim outputPath = Path.Combine(
outputRoot,
"AddNewContentToExistingPdf_" +
"insert_output.pdf")

DocumentBuilder.Create().
Pdf(documentA, 1, 1).
Pages(Sub(page) page.
Size(PageSize.Letter).
Text("Cover Page")).
Pdf(documentA, 2, documentA.PageCount - 1).
Save(outputPath)

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

How it works​

1. Run the three examples​

Run calls the private AddContent, AddPage, and InsertPage methods. Each creates and saves its own document. PdfSource.FromFile supplies the input PDFs from the Resources folder. PDF page numbers start at 1.

2. Add red text to page 2​

AddContent imports page 1 of DocumentC.pdf, then creates a Letter page with zero margins. .PdfPage(documentC, 2, bg => bg.Natural()) places source page 2 behind the new text at its natural size.

The text builder requests Helvetica, sets the font size to 24 points, and sets the color to red. “New Content” appears at the top left. The final .Pdf(...) call appends the remaining source pages, preserving their order.

AddPage starts with a Letter page measuring 612 by 792 points. Zero margins and zero flow spacing make the placement calculations relative to the full page.

DPDFLogo.png measures 262 by 250 pixels. The example divides both dimensions by three and uses the resulting values as point dimensions: approximately 87.33 by 83.33 points. Equal left and right margins of (612 - logoWidth) / 2 center the image horizontally.

The image's top margin places it above the text, and its 12-point bottom margin provides the gap. The text begins at 372 points from the top, placing the line near the vertical center of this Letter page. .Align(TextAlign.Center) centers “Cover Page” horizontally. It uses red 42-point text with Helvetica requested first and Arial as a fallback.

.Pdf(documentA) appends both pages of DocumentA.pdf after the cover.

4. Insert a new page​

InsertPage imports page 1 of DocumentA.pdf, adds a new Letter page containing “Cover Page” with default text styling, and appends the remaining source page. The fluent call order determines the final page order.

5. Save directly to the output directory​

Each document is saved with .Save(path), then its full output path is printed. All three output files are saved directly under outputRoot, with distinct filenames. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name; the input PDFs remain at their separate resource paths.

Next steps​

Change the text, logo, or selected page ranges. See Combine PDFs to combine entire documents, or Create a PDF to create new page content.