Skip to main content

Create a PDF

Create a PDF

A short chain of fluent calls is enough to turn text and styling into a PDF. Create your first PDF with a single line of text and a document title. The C# and VB examples save the result as CreateAPdf_output.pdf.

Fluent recipe / 01Create a PDFCreate a PDF with a document title and centered, CSS-styled text.

What you’ll create​

  • A PDF using the default page settings.
  • The text “DynamicPDF Flow API,” centered horizontally within the page margins.
  • Document-wide CSS that sets the text to green, 32-point Helvetica.
  • The document title My PDF.
  • A file named CreateAPdf_output.pdf in your chosen output directory.
Generated PDF with large green text centered near the top of the page.
tip

For more advanced text formatting, see Text & formatted text and Styling text using CSS.

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 sets the title, adds text, and saves the document to an existing output directory.

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

namespace FluentExamples.Examples;

internal static class CreateAPdf
{
public static void Run(string outputRoot)
{
var path = Path.Combine(
outputRoot, "CreateAPdf_output.pdf");

DocumentBuilder.Create()
.Title("My PDF")
.Style("""
* {
font-family: Helvetica;
font-size: 32pt;
color: green;
}
""")
.Pages(page => page
.Text("DynamicPDF Flow API",
text => text.Align(TextAlign.Center)))
.Save(path);

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

Friend Module CreateAPdf
Public Sub Run(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "CreateAPdf_output.pdf")

DocumentBuilder.Create().
Title("My PDF").
Style("* {
font-family: Helvetica;
font-size: 32pt;
color: green;
}").
Pages(Sub(page) page.
Text("DynamicPDF Flow API",
Sub(text) text.Align(TextAlign.Center))).
Save(outputPath)

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

How it works​

1. Choose where to save the PDF​

Path.Combine joins outputRoot with the filename CreateAPdf_output.pdf. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name.

2. Set the document title​

DocumentBuilder.Create() starts the document. .Title("My PDF") sets its title directly in the fluent chain, before adding pages. The title is metadata; it does not add visible text to the page. You can inspect it in your PDF viewer's document properties panel.

3. Apply a document-wide CSS style​

The document-level .Style(...) adds CSS rules before the pages are created. The * selector sets the text to green, 32-point Helvetica.

note

Use page.Style(...) inside a .Pages(...) callback to apply CSS to an individual page and override document-wide styles.

4. Add the text​

Inside Pages, Text adds “DynamicPDF Flow API” using the default page settings and the document-wide CSS style.

text.Align(TextAlign.Center) centers the line horizontally within the available content width between the page margins. The text stays in the normal top-to-bottom flow; this setting does not center it vertically on the page.

5. Save the PDF​

.Save(path) builds and saves the document directly through the fluent builder, including its title. The final line prints the output path so you can find and open the file.

Next steps​

Change the text and title to make the example your own. Then explore tables and flowing content to build a document with more structure, or browse all examples.

tip

See StreamByteArrayOutput.cs for examples of writing a PDF to a byte array or a stream.