Skip to main content

Create a page with columns

Create a page with columns

A column layout places related passages side by side and gives each its own reading width. Place text in two columns on a single PDF page. This example follows CreatePageWithColumns.cs, adds a heading above the columns, and uses a column break to start the right-hand column.

Fluent recipe / 10Create a page with columnsArrange text in two page columns with a gap and an explicit column break.

What you’ll create​

  • A Letter-sized page with 50-point margins.
  • A heading above two equal-width text columns.
  • A 24-point gap between the columns, with height determined by the content.
  • Bold column headings followed by text that wraps within each column.
  • CreatePageWithColumns_output.pdf in your chosen output directory.
Generated PDF with a heading above two text columns.

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 creates a page with two text columns and saves the document to an existing output directory.

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

namespace FluentExamples.Examples;

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

DocumentBuilder.Create()
.Title("A page with two columns")
.DefaultUnit(LengthUnit.Point)
.Style("""
text {
font-family: 'Helvetica';
font-size: 12pt;
}
""")
.Pages(page => page
.Size(PageSize.Letter)
.Margins(50)
.Text("A page with two columns", text => text
.FontSize(20))
.Spacing(15)
.Columns(2, columns => columns
.ColumnGap(24, LengthUnit.Point)
.Text("<b>First column</b><br/>"
+ "This text is in the left column. "
+ "The available width is divided into "
+ "two equal columns with a gap between "
+ "them. Text wraps within each column.")
.ColumnBreak()
.Text("<b>Second column</b><br/>"
+ "A column break moves this text to "
+ "the right column. Both columns are "
+ "part of the same page layout.")))
.Save(path);

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

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

DocumentBuilder.Create().
Title("A page with two columns").
DefaultUnit(LengthUnit.Point).
Style("text {
font-family: 'Helvetica';
font-size: 12pt;
}").
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(50).
Text("A page with two columns", Sub(text) text.
FontSize(20)).
Spacing(15).
Columns(2, Sub(columns) columns.
ColumnGap(24, LengthUnit.Point).
Text("<b>First column</b><br/>" &
"This text is in the left column. " &
"The available width is divided into " &
"two equal columns with a gap between " &
"them. Text wraps within each column.").
ColumnBreak().
Text("<b>Second column</b><br/>" &
"A column break moves this text to " &
"the right column. Both columns are " &
"part of the same page layout."))).
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 CreatePageWithColumns_output.pdf. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name.

2. Set up the document and page​

DocumentBuilder.Create() starts the document. .Title(...) sets its metadata title, and .DefaultUnit(LengthUnit.Point) makes unqualified measurements use points. The document stylesheet defines text defaults.

.Size(PageSize.Letter) selects Letter paper, and .Margins(50) reserves 50 points on every side. A separate text element adds the 20-point page heading, followed by 15 points of spacing.

3. Create two columns​

.Columns(2, ...) divides the available content width into two equal columns. ColumnGap(24, LengthUnit.Point) sets the gap between the columns. Flow sizes the column block automatically based on its content.

4. Move content to the next column​

The first .Text(...) adds the left column's content. Its HTML uses <b> for the heading and <br/> to begin the paragraph on the next line.

.ColumnBreak() moves subsequent content to the right column. The second .Text(...) adds its heading and paragraph there. Text wraps within each column's width.

5. Save the PDF​

.Save(path) builds and saves the document through the fluent builder. Console.WriteLine prints its output path.

Next steps​

Change the column count, gap, or text to explore the layout. See Text & formatted text for HTML formatting and Styling text using CSS for more styling examples, or browse all examples.