Syncfusion AI Assistant

How can I help you?

Create or Generate PDF file in UWP

18 Jun 20266 minutes to read

The Syncfusion® UWP 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 Syncfusion® UWP PDF library into your UWP application, please refer to the NuGet Package Required or Assemblies Required documentation.

Steps to create PDF document in UWP

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

Step 2: Install the Syncfusion.Pdf.UWP NuGet package as a reference to your UWP applications from NuGet.org.
PDF UWP Nuget package

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: Create button in MainPage.Xaml page using below code example and create Button_Click event.

<Grid>
  <Button Content="CreatePDF" HorizontalAlignment="Center"  VerticalAlignment="Center" Width="150" Height="100" Click="Button_Click" />
</Grid>

Step 4: Include the following namespaces in the MainPage.xaml.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

Step 5: Include the following code example in the click event of the button in MainPage.xaml.cs file to create PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw the text on the PDF page.

//Create a PDF document. 
using (PdfDocument document = new PdfDocument())
{
    //Add a page to the document.
    PdfPage page = document.Pages.Add();
    //Create PDF graphics for the 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));
    //Create memory stream.
    MemoryStream ms = new MemoryStream();
    //Open the document in browser after saving it.
    document.Save(ms);
    //Close the document.
    document.Close(true);
    Save(ms, "Sample.pdf");
}

Step 6: Use the following helper method to save the stream as a physical file and open the file for viewing.

#region Helper Methods
public async void Save(Stream stream, string filename)
{
    stream.Position = 0; 

    StorageFile stFile; 
    if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) 
    { 
        FileSavePicker savePicker = new FileSavePicker(); 
        savePicker.DefaultFileExtension = ".pdf"; 
        savePicker.SuggestedFileName = "Sample"; 
        savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" }); 
        stFile = await savePicker.PickSaveFileAsync(); 
    } 
    else 
    { 
        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; 
        stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); 
    } 
    if (stFile != null) 
    { 
        Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite); 
        Stream st = fileStream.AsStreamForWrite(); 
        st.SetLength(0); 
        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length); 
        st.Flush(); 
        st.Dispose(); 
        fileStream.Dispose(); 
        MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created."); 
        UICommand yesCmd = new UICommand("Yes"); 
        msgDialog.Commands.Add(yesCmd); 
        UICommand noCmd = new UICommand("No"); 
        msgDialog.Commands.Add(noCmd); 
        IUICommand cmd = await msgDialog.ShowAsync(); 
        if (cmd == yesCmd) 
        { 
            //Launch the retrieved file.
            bool success = await Windows.System.Launcher.LaunchFileAsync(stFile); 
        } 
    } 
}
#endregion

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.