Create or Generate a PDF file in WPF
24 Jul 20267 minutes to read
The .NET PDF library is used to create, read, and edit PDF documents. This library also offers functionality to merge, split, stamp, work with forms, and secure PDF files.
To include the .NET PDF library into your WPF application, please refer to the NuGet Package Required or Assemblies Required documentation.
Steps to create a PDF document in WPF
Step 1: Create a new WPF Application project in Visual Studio, targeting .NET Framework 4.6.2 (or later) or .NET 8.0/9.0/10.0 (Windows).

Step 2: Install the Syncfusion.Pdf.Wpf NuGet package as a reference to your application from NuGet.org.

NOTE
The Syncfusion.Pdf.Wpf NuGet package targets .NET Framework 4.6.2 and later. as well as .NET 8.0, 9.0, and 10.0 (Windows) WPF applications — all from the same package. Select the project template that matches your target framework in Visual Studio.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add the
Syncfusion.Licensingassembly reference and include a license key in your projects. Please refer to this link to learn about registering the Syncfusion® license key in your application to use our components.
Step 3: Register the Syncfusion® license key in the App.xaml.cs file before any Syncfusion component is used, to remove the evaluation watermark. Replace "YOUR LICENSE KEY" with the actual key from your Syncfusion® account.
using System.Windows;
namespace CreatePdfWpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
//Register the Syncfusion license key to remove the evaluation watermark.
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
}
}
}NOTE
The license must be registered once during application startup, before instantiating any Syncfusion® component (for example, before creating a
PdfDocument).
Step 4: Include the following namespaces in the MainWindow.xaml.cs file.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Drawing;
using System.Windows;Step 5: Add a new button in MainWindow.xaml to create a PDF document as follows. Place this snippet inside the default <Grid> of the existing MainWindow.xaml file.
<TextBlock TextAlignment="Justify"
FontFamily="Verdana"
FontSize="11"
TextWrapping="Wrap"
Padding="5,5,5,5"
Margin="0,77,0,1">
<TextBlock.Background>
<LinearGradientBrush StartPoint="0.5,1.04"
EndPoint="0.5,-0.04">
<GradientStop Color="#FFD9E9F7" Offset="0" />
<GradientStop Color="#FFEFF8FF" Offset="1" />
</LinearGradientBrush>
</TextBlock.Background>
<TextBlock.Text>
Click the button to view a PDF file generated by Essential PDF.
</TextBlock.Text>
</TextBlock>
<Button Click="btnCreate_Click"
Margin="0,0,10,12"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Height="30"
Width="180"
BorderBrush="LightBlue">
<Button.Background>
<LinearGradientBrush StartPoint="0.5,1.04"
EndPoint="0.5,-0.04">
<GradientStop Color="#FFD9E9F7" Offset="0" />
<GradientStop Color="#FFEFF8FF" Offset="1" />
</LinearGradientBrush>
</Button.Background>
<StackPanel Orientation="Horizontal"
Height="23"
Width="100"
Margin="0,0,0,-2.52"
VerticalAlignment="Bottom"
HorizontalAlignment="Right">
<TextBlock Text="Create PDF"
Height="15.96"
Width="126"
Margin="0,4,0,3"
VerticalAlignment="Center" />
</StackPanel>
</Button>Step 6: In MainWindow.xaml.cs, add the btnCreate_Click event handler to create or generate a PDF document using the PdfDocument class. The DrawString method of the PdfGraphics object is used to draw text on the PDF page and save the output in your application.
private void btnCreate_Click(object sender, RoutedEventArgs e)
{
//Create a new PDF document.
using (PdfDocument document = new PdfDocument())
{
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for a page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
}
}You can download a complete working sample from GitHub.
By executing the program, you will get the PDF document as follows.

An online sample link to create a PDF document.
NOTE
If a valid license key is not registered, an evaluation watermark is applied to the generated PDF document. To remove the watermark, register the license key in App.xaml.cs at application startup (as shown in Step 3). Refer to the licensing overview for details.