Create or Generate PDF file in WPF

In your WPF application, add the following assemblies to use Essential PDF:

  • Syncfusion.Pdf.Base
  • Syncfusion.Compression.Base

For more details, refer to this Assemblies Required documentation.

Steps to create PDF document in WPF:

Create a new WPF application project.
Creation1

Install the Syncfusion.Pdf.Wpf NuGet package as reference to your .NET Framework applications from NuGet.org.
Creation1

Include the following namespaces in the MainWindow.xaml.cs file.

  • c#
  • using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows;

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

  • c#
  • <TextBlock TextAlignment="Justify" FontFamily="Verdana" FontSize="11" TextWrapping="Wrap" Padding="5,5,5,5" Margin="0,77,0,1" >
    <TextBlock.Background>
    <LinearGradientBrush EndPoint="0.5,-0.04" StartPoint="0.5,1.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" Height="30" BorderBrush="LightBlue" HorizontalAlignment="Right" Width="180">
    <Button.Background>
    <LinearGradientBrush EndPoint="0.5,-0.04" StartPoint="0.5,1.04">
    <GradientStop Color="#FFD9E9F7" Offset="0"/>
    <GradientStop Color="#FFEFF8FF" Offset="1"/>
    </LinearGradientBrush>
    </Button.Background>
    <StackPanel Orientation="Horizontal" Height="23" Margin="0,0,0,-2.52" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100">
    <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>

    Add the following code in btnCreate_Click to create a PDF file with simple text.

  • c#
  • 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");
    }

    A complete working sample can be downloaded from Create-PDF-file.zip

    By executing the program, you will get the PDF document as follows.
    output

    Creating a PDF document with image

    The following code example shows how to create a PDF document with an image.

  • c#
  • //Create a new PDF document.
    PdfDocument doc = new PdfDocument();
    //Add a page to the document.
    PdfPage page = doc.Pages.Add();
    //Create PDF graphics for the page
    PdfGraphics graphics = page.Graphics;
    //Load the image from the disk.
    PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
    //Draw the image
    graphics.DrawImage(image, 0, 0);
    //Save the document.
    doc.Save("Output.pdf");
    //Close the document.
    doc.Close(true);

    Creating a PDF document with table

    The following code example shows how to create a PDF document with a simple table.

  • c#
  • //Create a new PDF document.
    PdfDocument doc = new PdfDocument();
    //Add a page.
    PdfPage page = doc.Pages.Add();
    //Create a PdfGrid.
    PdfGrid pdfGrid = new PdfGrid();
    //Create a DataTable.
    DataTable dataTable = new DataTable();
    //Add columns to the DataTable
    dataTable.Columns.Add("ID");
    dataTable.Columns.Add("Name");
    //Add rows to the DataTable.
    dataTable.Rows.Add(new object[] { "E01", "Clay" });
    dataTable.Rows.Add(new object[] { "E02", "Thomas" });
    dataTable.Rows.Add(new object[] { "E03", "Andrew" });
    dataTable.Rows.Add(new object[] { "E04", "Paul" });
    dataTable.Rows.Add(new object[] { "E05", "Gary" });
    //Assign data source.
    pdfGrid.DataSource = dataTable;
    //Draw grid to the page of PDF document.
    pdfGrid.Draw(page, new PointF(10, 10));
    //Save the document.
    doc.Save("Output.pdf");
    //close the document
    doc.Close(true);

    Creating a simple PDF document with basic elements

    The PdfDocument object represents an entire PDF document that is being created. The following code example shows how to create a PDF document and add a PdfPage to it along with the PdfPageSettings.

  • c#
  • //Creates a new PDF document
    PdfDocument document = new PdfDocument();
    //Adds page settings
    document.PageSettings.Orientation = PdfPageOrientation.Landscape;
    document.PageSettings.Margins.All = 50;
    //Adds a page to the document
    PdfPage page = document.Pages.Add();
    PdfGraphics graphics = page.Graphics;
    1. Essential PDF has APIs similar to the .NET GDI plus which helps to draw elements to the PDF page just like 2D drawing in .NET.
    2. Unlike System.Drawing APIs all the units are measured in point instead of pixel.
    3. In PDF, all the elements are placed in absolute positions and has the possibility for content overlapping if misplaced.
    4. Essential PDF provides the rendered bounds for each and every elements added through PdfLayoutResult objects. This can be used to add successive elements and prevent content overlap.

    The following code example explains how to add an image from disk to a PDF document, by providing the rectangle coordinates.

  • c#
  • //Loads the image from disk
    PdfImage image = PdfImage.FromFile("AdventureCycle.jpg");
    RectangleF bounds = new RectangleF(176, 0, 390, 130);
    //Draws the image to the PDF page
    page.Graphics.DrawImage(image, bounds);

    The following methods can be used to add text to a PDF document.

    1. DrawString() method of the PdfGraphics
    2. PdfTextElement class.

    The PdfTextElement provides the layout result of the added text by using the location of the next element that decides to prevent content overlapping. This is not available in the DrawString method.

    The following code example adds the necessary text such as address, invoice number and date to create a basic invoice application.

  • c#
  • PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(126, 151, 173));
    bounds = new RectangleF(0, bounds.Bottom + 90, graphics.ClientSize.Width, 30);
    //Draws a rectangle to place the heading in that region.
    graphics.DrawRectangle(solidBrush, bounds);
    //Creates a font for adding the heading in the page
    PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14);
    //Creates a text element to add the invoice number
    PdfTextElement element = new PdfTextElement("INVOICE " + id.ToString(), subHeadingFont);
    element.Brush = PdfBrushes.White;
    
    //Draws the heading on the page
    PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8));
    string currentDate = "DATE " + DateTime.Now.ToString("MM/dd/yyyy");
    //Measures the width of the text to place it in the correct location
    SizeF textSize = subHeadingFont.MeasureString(currentDate);
    PointF textPosition = new PointF(graphics.ClientSize.Width - textSize.Width - 10, result.Bounds.Y);
    //Draws the date by using DrawString method
    graphics.DrawString(currentDate, subHeadingFont, element.Brush, textPosition);
    PdfFont timesRoman = new PdfStandardFont(PdfFontFamily.TimesRoman, 10);
    //Creates text elements to add the address and draw it to the page.
    element = new PdfTextElement("BILL TO ", timesRoman);
    element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203));
    result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25));
    PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f);
    PointF startPoint = new PointF(0, result.Bounds.Bottom + 3);
    PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3);
    //Draws a line at the bottom of the address
    graphics.DrawLine(linePen, startPoint, endPoint);

    Essential PDF provides two types of table models. The difference between both the table models can be referred from the link
    Difference between PdfLightTable and PdfGrid

    Since the invoice document requires only simple cell customizations, the given code example explains how to create a simple invoice table by using PdfGrid.

  • c#
  • //Creates the datasource for the table
    DataTable invoiceDetails = GetProductDetailsAsDataTable();
    //Creates a PDF grid
    PdfGrid grid = new PdfGrid();
    //Adds the data source
    grid.DataSource = invoiceDetails;
    //Creates the grid cell styles
    PdfGridCellStyle cellStyle = new PdfGridCellStyle();
    cellStyle.Borders.All = PdfPens.White;
    PdfGridRow header = grid.Headers[0];
    //Creates the header style
    PdfGridCellStyle headerStyle = new PdfGridCellStyle();
    headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173));
    headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173));
    headerStyle.TextBrush = PdfBrushes.White;
    headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular);
    
    //Adds cell customizations
    for (int i = 0; i < header.Cells.Count; i++)
    {
      if (i == 0 || i == 1)
        header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
      else
        header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
    }
    
    //Applies the header style
    header.ApplyStyle(headerStyle);
    cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f);
    cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f);
    cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136));
    //Creates the layout format for grid
    PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
    // Creates layout format settings to allow the table pagination
    layoutFormat.Layout = PdfLayoutType.Paginate;
    //Draws the grid to the PDF page.
    PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100)), layoutFormat);

    The following code example shows how to save the invoice document to disk and dispose the PdfDocument object.

  • c#
  • //Saves and closes the document.
    document.Save("Sample.pdf");
    document.Close(true);

    The following screenshot shows the invoice PDF document created by using Essential PDF.
    invoice

    Filling forms

    An interactive form, sometimes referred to as an AcroForm is a collection of fields for gathering information interactively from the user. A PDF document can contain any number of fields appearing in any combination of pages, all of that make a single, globally interactive form spanning the entire document.

    Essential PDF allows you to create and manipulate existing form in PDF document. To work with existing form documents, the following namespaces are required.

    1. Syncfusion.Pdf
    2. Syncfusion.Pdf.Parsing

    The following guide shows how to fill a sample PDF form as shown.

    Form Fill

    Essential PDF allows you to fill the form fields by using PdfLoadedField class. You can get the form field either by using its field name or field index.

  • c#
  • //Loads the PDF form.
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"JobApplication.pdf");
    //Loads the form
    PdfLoadedForm form = loadedDocument.Form;
    //Fills the textbox field by using index
    (form.Fields[0] as PdfLoadedTextBoxField).Text = "John";
    //Fills the textbox fields by using field name
    (form.Fields["LastName"] as PdfLoadedTextBoxField).Text = "Doe";
    (form.Fields["Address"] as PdfLoadedTextBoxField).Text = " John Doe \n 123 Main St \n Anytown, USA";
    //Loads the radio button group
    PdfLoadedRadioButtonItemCollection radioButtonCollection = (form.Fields["Gender"] as PdfLoadedRadioButtonListField).Items;
    //Checks the 'Male' option
    radioButtonCollection[0].Checked = true;
    //Checks the 'business' checkbox field
    (form.Fields["Business"] as PdfLoadedCheckBoxField).Checked = true;
    //Checks the 'retiree' checkbox field
    (form.Fields["Retiree"] as PdfLoadedCheckBoxField).Checked = true;
    //Saves and closes the document
    loadedDocument.Save("Output.pdf");
    loadedDocument.Close(true);

    The filled form is shown in adobe reader application as follows.
    Form Fill

    Merge PDF Documents

    Essential PDF supports merging multiple PDF documents from disk and stream using Merge method. You can merge the multiple PDF documents from disk by specifying the path of the documents in a string array.

    Refer to the following code example to merge multiple documents from disk.

  • c#
  • //Creates the new PDF document
    PdfDocument finalDoc = new PdfDocument();
    // Creates a string array of source files to be merged.
    string[] source = { "file1.pdf, file2.pdf" };
    // Merges PDFDocument.
    PdfDocument.Merge(finalDoc, source);
    //Saves the final document
    finalDoc.Save("Sample.pdf");
    //closes the document
    finalDoc.Close(true);

    You can merge the PDF document streams by using the following code example.

  • c#
  • //Creates the destination document
    PdfDocument finalDoc = new PdfDocument();
    Stream stream1 = File.OpenRead("file1.pdf");
    Stream stream2 = File.OpenRead("file2.pdf");
    // Creates a PDF stream for merging.
    Stream[] streams = { stream1, stream2 };
    // Merges PDFDocument.
    PdfDocumentBase.Merge(finalDoc, streams);
    //Saves the document
    finalDoc.Save("sample.pdf");
    //closes the document
    finalDoc.Close(true);