SYNCFUSION BLOG

Word to PDF conversion in Xamarin and .NET Core

Syncfusion is excited to share that Word-to-PDF conversion is now available in Xamarin and .NET Core platforms.

 

Cross-platform development is the future of mobile, desktop, and web application development. It provides benefits like quick deployment, lower costs, wider reach, and quick turnaround time. The adoption of cross-platform application development is increasing among enterprises and small businesses as well. The Xamarin and .NET Core platforms are being used by large numbers of developers to develop applications that target multiple platforms.

 

Syncfusion already provided the ability to convert Word documents to PDF in Windows Forms, WPF, ASP.NET, and ASP.NET MVC applications, but it was not extended to Xamarin and .NET Core due to the lack of a graphical API.

 

From the 2018 Volume 1 release (version 16.1.0.24) onwards, Syncfusion has extended the support for converting Word documents to PDF in Xamarin and .NET Core with the help of the SkiaSharp graphics library. 

An illustration of the Word-to-PDF conversion process in .NET Core and Xamarin.


Currently, the following features are available in Word-to-PDF conversion:

  • Text with formatting.
  • Table.
  • Images (PNG, JPEG, BMP and GIF).
  • Headers and footers.
  • Hyperlinks.
  • Fields like page number, date time, IF, and more.
  • Form fields and content control.
  • Textbox and Shapes.
  • Text wrapping for floating images, textbox, and shapes.
  • Footnote and endnote.
  • Line numbering.
  • Bookmark navigation.

Converting Word to PDF in Xamarin

Let's learn how to convert a Word document to PDF in the Xamarin platform.

Prerequisites

For Windows

  • Windows 10 (recommended)
  • Visual Studio 2015 or Visual Studio 2017 (recommended)
  • Windows 10 SDK

For macOS

  • OS X El Capitan (10.11) or newer
  • Visual Studio for Mac
  •  iOS 10 SDK and Xcode 8

Procedure

1.   Create a Cross-Platform App (Xamarin.Forms) and name it WordtoPDFdemo.


2.   Select the Blank App template and the .NET Standard option under Code Sharing Strategy.


3.   Right-click the .NET Standard project, select Properties, and set the Target framework of the application as .NET Standard 1.4 or higher.



4.   Add the Syncfusion.Xamarin.DocIORenderer NuGet package as a reference to the .NET Standard project in your Xamarin application.

  

Note: Syncfusion Xamarin components are available on nuget.org.



5.   Add the input Word document to the asset folder in the .NET Standard project. Right-click the Word document, select Properties, and set its build action as Embedded resource.


6.      Add a button in the MainPage.xaml file.




    
        
        
        
    


7.  Add the following code in the onButtonClicked method in the MainPage.xaml.cs file. Use the ConvertToPDF method of the DocIORenderer class to convert the WordDocument instance to a PdfDocument instance and save the PDF document to the device.

Assembly assembly = typeof(App).GetTypeInfo().Assembly;
// Retrieves the document stream from embedded Word document
Stream inputStream = assembly.GetManifestResourceStream("WordToPDF.Assets.GettingStarted.docx");
string fileName = "GettingStarted.pdf";
// Creates new instance of WordDocument
WordDocument wordDocument = new WordDocument(inputStream, Syncfusion.DocIO.FormatType.Automatic);
// Creates new instance 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 DocIORenderer and WordDocument instance
render.Dispose();
wordDocument.Close();
// Saves the converted PDF file
MemoryStream outputStream = new MemoryStream();
pdfDocument.Save(outputStream);
// Releases all resources used by the PdfDocument instance
pdfDocument.Close();

// Saves the PDF document to the device through the dependency service with the help of platform-specific file system API
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
    DependencyService.Get().Save(fileName, "application/pdf", outputStream);
else
    DependencyService.Get().Save(fileName, "application/pdf", outputStream);