Skip to content

Latest commit

 

History

History
186 lines (152 loc) · 8.24 KB

File metadata and controls

186 lines (152 loc) · 8.24 KB
title Convert Word document to PDF in .NET MAUI | Syncfusion
description Convert Word to PDF in .NET MAUI using .NET MAUI Word (DocIO) library without Microsoft Word or interop dependencies.
platform file-formats
control DocIO
documentation UG

Convert Word Document to PDF in .NET MAUI

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.

Prerequisites

To create .NET Multi-platform App UI (.NET MAUI) apps, you need the latest versions of Visual Studio 2022 and .NET 6. For more details, refer here.

Steps to convert Word document to PDF in .NET MAUI

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

N> 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 4: Add a new button to the MainPage.xaml as shown below.

{% tabs %}

{% highlight c# tabtitle="C#" %} {% endhighlight %}

{% endtabs %}

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

{% tabs %}

{% highlight c# tabtitle="C#" %} using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer; using Syncfusion.Pdf; {% endhighlight %}

{% endtabs %}

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

{% tabs %}

{% highlight c# tabtitle="C#" %}

//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);
    }
}

}

{% endhighlight %}

{% endtabs %}

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](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF)'| markdownify }} {{'[SaveService.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/SaveServices/SaveService.cs)'| markdownify }} Represent the base class for save operation.
{{'[Windows](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/Windows)'| markdownify }} {{'[SaveWindows.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/Windows/SaveWindows.cs)'| markdownify }} Save implementation for Windows.
{{'[Android](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/Android)'| markdownify }} {{'[SaveAndroid.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/Android/SaveAndroid.cs)'| markdownify }} Save implementation for Android device.
{{'[Mac Catalyst](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/MacCatalyst)'| markdownify }} {{'[SaveMac.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/MacCatalyst/SaveMac.cs)'| markdownify }} Save implementation for Mac Catalyst device.
{{'[iOS](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/iOS)'| markdownify }} {{'[SaveIOS.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/iOS/SaveIOS.cs)'| markdownify }} Save implementation for iOS device
{{'[PreviewControllerDS.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/iOS/PreviewControllerDS.cs)'| markdownify }}
{{'[QLPreviewItemFileSystem.cs](https://github.com/SyncfusionExamples/DocIO-Examples/blob/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/.NET-MAUI/Convert-Word-Document-to-PDF/Platforms/iOS/QLPreviewItemFileSystem.cs)'| markdownify }}
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.