Convert Word document to PDF in .NET MAUI

19 Feb 202518 minutes to read

Syncfusion® DocIO is a .NET MAUI Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in .NET MAUI.

Steps to convert Word document to PDF in .NET MAUI

Prerequisites:

  • Visual Studio 2022.
  • Install .NET 8 SDK or later.
  • For more details about installation, refer here.

Step 1: Create a new C# .NET MAUI app. Select .NET MAUI App from the template and click the Next button.

Create the MAUI app in Visual Studio

Step 2: Enter the project name and click Create.

Create a project name for your new project

Step 3: Install the Syncfusion.DocIORenderer.NET NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.DocIORenderer.NET 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 “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering a Syncfusion® license key in your application to use our components.

Step 3: Add a new button to the MainPage.xaml as shown below.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Convert_Word_Document_to_PDF.MainPage">
    <ScrollView>
        <Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
            Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">
            <Button 
                Text="Convert Word Document to PDF"
                FontAttributes="Bold"
                Grid.Row="0"
                SemanticProperties.Hint="Convert Word Document to PDF"
                Clicked="ConvertWordtoPDF"
                HorizontalOptions="Center" />
        </Grid>
    </ScrollView>
</ContentPage>

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

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

Step 5: Add a new action method ConvertWordtoPDF in MainPage.xaml.cs and include the below code snippet to convert a Word document to PDF.

//Loading an existing Word document
Assembly assembly = typeof(App).GetTypeInfo().Assembly;      
using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Convert_Word_Document_to_PDF.Assets.Input.docx"), FormatType.Docx))
{
    //Instantiation of DocIORenderer for Word to PDF conversion
    using (DocIORenderer render = new DocIORenderer())
    {
        //Converts Word document into PDF document
        using (PdfDocument pdfDocument = render.ConvertToPDF(document))
        {
            //Saves the PDF document to MemoryStream.
            MemoryStream stream = new MemoryStream();
            pdfDocument.Save(stream);

            //save and Launch the PDF document
            SaveService saveService = new();                 
            saveService.SaveAndView("Sample.pdf", "application/pdf", stream);
        }
    }
}

Step 6: Run the Application.

  1. Select the target framework, device or emulator.
  2. Press F5 to run the application.

Helper files for .NET MAUI

Refer the below helper files and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.

Folder Name File Name Summary

.NET MAUI Project

SaveService.cs

Represent the base class for save operation.

Windows

SaveWindows.cs

Save implementation for Windows.

Android

SaveAndroid.cs

Save implementation for Android device.

Mac Catalyst

SaveMac.cs

Save implementation for Mac Catalyst device.

iOS

SaveIOS.cs

Save implementation for iOS device

PreviewControllerDS.cs


QLPreviewItemFileSystem.cs

Helper classes for viewing the Word document in iOS device

You can download a complete working sample from GitHub.

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

Word to PDF in .NET MAUI

Prerequisites:

Step 1: Create a new C# .NET MAUI app project.

  • Open the command palette by pressing Ctrl+Shift+P and type .NET:New Project and enter.
  • Choose the .NET MAUI App template.

Choose .NET MAUI app from template

  • Select the project location, type the project name and press enter.
  • Then choose Create project.

Step 2: To convert a Word document to PDF in .NET MAUI app, install Syncfusion.DocIORenderer.NET.Core to the MAUI project.

  • Press Ctrl + ` (backtick) to open the integrated terminal in Visual Studio Code.
  • Ensure you’re in the project root directory where your .csproj file is located.
  • Run the command dotnet add package Syncfusion.DocIORenderer.NET.Core to install the NuGet package.

Add Syncfusion.DocIORenderer.NET.Core NuGet package

dotnet add package Syncfusion.DocIORenderer.NET.Core

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 “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering a Syncfusion® license key in your application to use our components.

Step 3: Add a new button to the MainPage.xaml as shown below.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Convert_Word_Document_to_PDF.MainPage">
    <ScrollView>
        <Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
            Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">
            <Button 
                Text="Convert Word Document to PDF"
                FontAttributes="Bold"
                Grid.Row="0"
                SemanticProperties.Hint="Convert Word Document to PDF"
                Clicked="ConvertWordtoPDF"
                HorizontalOptions="Center" />
        </Grid>
    </ScrollView>
</ContentPage>

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

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

Step 5: Add a new action method ConvertWordtoPDF in MainPage.xaml.cs and include the below code snippet to convert a Word document to PDF.

//Loading an existing Word document
Assembly assembly = typeof(App).GetTypeInfo().Assembly;      
using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Convert_Word_Document_to_PDF.Assets.Input.docx"), FormatType.Docx))
{
    //Instantiation of DocIORenderer for Word to PDF conversion
    using (DocIORenderer render = new DocIORenderer())
    {
        //Converts Word document into PDF document
        using (PdfDocument pdfDocument = render.ConvertToPDF(document))
        {
            //Saves the PDF document to MemoryStream.
            MemoryStream stream = new MemoryStream();
            pdfDocument.Save(stream);

            //save and Launch the PDF document
            SaveService saveService = new();                 
            saveService.SaveAndView("Sample.pdf", "application/pdf", stream);
        }
    }
}

Step 6: Run the Application.

  1. Select the target framework, device or emulator.
  2. Press F5 to run the application.

Helper files for .NET MAUI

Refer the below helper files and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.

Folder Name File Name Summary

.NET MAUI Project

SaveService.cs

Represent the base class for save operation.

Windows

SaveWindows.cs

Save implementation for Windows.

Android

SaveAndroid.cs

Save implementation for Android device.

Mac Catalyst

SaveMac.cs

Save implementation for Mac Catalyst device.

iOS

SaveIOS.cs

Save implementation for iOS device

PreviewControllerDS.cs


QLPreviewItemFileSystem.cs

Helper classes for viewing the Word document in iOS device

You can download a complete working sample from GitHub.

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

Word to PDF in .NET MAUI

Prerequisites:

  • JetBrains Rider.
  • Install .NET 8 SDK or later.
  • For more details about installation, refer here.

Step 1. Open JetBrains Rider and create a new .NET MAUI App project.

  • Launch JetBrains Rider.
  • Click new solution on the welcome screen.

Launch JetBrains Rider

  • In the new Solution dialog, select Project Type as MAUI.
  • Select the target framework (e.g., .NET 8.0, .NET 9.0).
  • Choose Type as App.
  • Enter a project name and specify the location.
  • Click create.

Creating a new .NET MAUI App in JetBrains Rider

Step 2: Install the NuGet package from NuGet.org.

  • Click the NuGet icon in the Rider toolbar and type Syncfusion.DocIORenderer.Net.Core in the search bar.
  • Ensure that nuget.org is selected as the package source.
  • Select the latest Syncfusion.DocIORenderer.Net.Core NuGet package from the list.
  • Click the + (Add) button to add the package.

Select the Syncfusion.DocIORenderer.Net.Core NuGet package

  • Click the Install button to complete the installation.

Install the Syncfusion.DocIORenderer.Net.Core 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 “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 3: Add a new button to the MainPage.xaml as shown below.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Convert_Word_Document_to_PDF.MainPage">
    <ScrollView>
        <Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
            Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">
            <Button 
                Text="Convert Word Document to PDF"
                FontAttributes="Bold"
                Grid.Row="0"
                SemanticProperties.Hint="Convert Word Document to PDF"
                Clicked="ConvertWordtoPDF"
                HorizontalOptions="Center" />
        </Grid>
    </ScrollView>
</ContentPage>

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

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

Step 5: Add a new action method ConvertWordtoPDF in MainPage.xaml.cs and include the below code snippet to convert a Word document to PDF.

//Loading an existing Word document
Assembly assembly = typeof(App).GetTypeInfo().Assembly;      
using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Convert_Word_Document_to_PDF.Assets.Input.docx"), FormatType.Docx))
{
    //Instantiation of DocIORenderer for Word to PDF conversion
    using (DocIORenderer render = new DocIORenderer())
    {
        //Converts Word document into PDF document
        using (PdfDocument pdfDocument = render.ConvertToPDF(document))
        {
            //Saves the PDF document to MemoryStream.
            MemoryStream stream = new MemoryStream();
            pdfDocument.Save(stream);

            //save and Launch the PDF document
            SaveService saveService = new();                 
            saveService.SaveAndView("Sample.pdf", "application/pdf", stream);
        }
    }
}

Step 6: Build the project.

Click the Build button in the toolbar or press Ctrl+Shift+B to build the project.

Step 7: Run the project.

Select the target platform in the Run Configuration dropdown, then click Run.

Helper files for .NET MAUI

Refer the below helper files and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.

Folder Name File Name Summary

.NET MAUI Project

SaveService.cs

Represent the base class for save operation.

Windows

SaveWindows.cs

Save implementation for Windows.

Android

SaveAndroid.cs

Save implementation for Android device.

Mac Catalyst

SaveMac.cs

Save implementation for Mac Catalyst device.

iOS

SaveIOS.cs

Save implementation for iOS device

PreviewControllerDS.cs


QLPreviewItemFileSystem.cs

Helper classes for viewing the Word document in iOS device

You can download a complete working sample from GitHub.

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

Word to PDF in .NET MAUI

Click here to explore the rich set of Syncfusion® Word library (DocIO) features.

An online sample link to convert Word document to PDF in ASP.NET Core.