)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert Word document to PDF in UWP

Platform: UWP |
Control: DocIO |
Published Date: March 7, 2019 |
Last Revised Date: April 25, 2019

Syncfusion Essential DocIO Renderer is a .NET Core Word to PDF conversion library allows you to convert Word document to PDF in UWP application [C#].

Steps to convert Word document to PDF programmatically in UWP:

  1. Create a new C# Blank App (Universal Windows) project.
    Create UWP application in Visual Studio
  2. Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your UWP application from NuGet.org.

Add DocIO NuGet package reference to the project

  1. Add a new button in the MainPage.xaml as shown below.

XAML

<Page
    x:Class="WordToPDF.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WordToPDF"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="button" Content="Generate PDF" Click="OnButtonClicked" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Page>
  1. Include the following namespaces in the Mainpage.xaml.cs file.

C#

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

 

  1. Include the below code snippet in the click event of the button in MainPage.xaml.cs, to convert a Word document to PDF and save the PDF document as a physical file and open the file for viewing.

C#

private async void OnButtonClicked(object sender, RoutedEventArgs e)
{
    //Creates an instance of WordDocument Instance (Empty Word Document)
    WordDocument wordDocument = new WordDocument();
    //Add a section and a paragraph in the empty document
    wordDocument.EnsureMinimal();
    //Append text to the last paragraph of the document
    wordDocument.LastParagraph.Text = "Adventure Works Cycles, the fictitious company on which the" +
" AdventureWorks sample databases are based, is a large, multinational manufacturing company. ";
    //Instantiation of DocIORenderer for Word to PDF conversion
    DocIORenderer render = new DocIORenderer();
    //Converts Word document into PDF document
    PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
    //Releases all resources used by the Word document and DocIO Renderer objects
    render.Dispose();
    wordDocument.Dispose();
    //Saves the PDF file
    MemoryStream outputStream = new MemoryStream();
    pdfDocument.Save(outputStream);
    //Closes the instance of PDF document object
    pdfDocument.Close();
    outputStream.Position = 0;
    //Save the PDF file
    SavePDF(outputStream);
}
 
private async void SavePDF(Stream outputStream)
{
    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("Sample.pdf", CreationCollisionOption.ReplaceExisting);
    }
    if (stFile != null)
    {
      Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
      Stream st = fileStream.AsStreamForWrite();
      st.SetLength(0);
      st.Write((outputStream as MemoryStream).ToArray(), 0, (int)outputStream.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);
      }
    }
}

 

A complete working example of how to convert Word document to PDF in UWP using .NET Core Word to PDF conversion library can be downloaded from Word to PDF in UWP.zip.

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document and protect the Word documents with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

See Also:

Create Word document in UWP

Mail merge Word document in UWP

Note:
  1. As per MSDN announcement, the minimum version of UWP project must be Fall Creators Update (FCU).
  2. Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate UWP UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon