Syncfusion AI Assistant

How can I help you?

Create or Generate PDF file in WPF

18 Jun 20265 minutes to read

The Syncfusion® .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 PDF document in WPF

Step 1: Create a new WPF application project.
WPF sample creation

Step 2: Install the Syncfusion.Pdf.Wpf NuGet package as a reference to your .NET Framework applications from NuGet.org.
PDF WPF NuGet package installation

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.Licensing assembly 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: Include the following namespaces in the MainWindow.xaml.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;

Step 4: Add a new button in MainWindow.xaml to create a PDF document as follows.

<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 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" />
        </Button.Background>
    <StackPanel Orientation="Horizontal"
                Height="23"
                Width="100"
                Margin="0,0,0,-2.52"
                VerticalAlignment="Bottom"
                HorizontalAlignment="Right">
        <Image Name="image2"
               Margin="2"
               HorizontalAlignment="Center"
               VerticalAlignment="Center" />
        <TextBlock Text="Create PDF"
                   Height="15.96"
                   Width="126"
                   Margin="0,4,0,3" />
    </StackPanel>
</Button>

Step 5: Add the following code sample in btnCreate_Click to create or generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw text on the PDF page and save or open the output in your application.

//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.
PDF generation output

Click here to explore the rich set of Syncfusion® PDF library features.

An online sample link to create PDF document.