Articles in this section
Category / Section

How to convert a Word document to PDF in Xamarin.Forms

5 mins read

Yes, it is possible to convert a Word document to PDF in Xamarin platforms. From 2018 Volume 1 release (v16.1.0.24) onwards, Syncfusion provides support for Word to PDF conversion in Xamarin platforms.

Step 1: Create a Xamarin Forms application. For .NET Standard Xamarin application, the target framework must be .NET Standard 1.4 or higher version.

Step 2: Add the following NuGet package from nuget.org as reference in your Xamarin application. Kindly refer here to install NuGet from NuGet Package Manager.

  • Syncfusion.Xamarin.DocIORenderer

You can also install this package through package manager console using following commands

Step 3: Kindly add a button in your XAML page.

XAML:

<Button x:Name="btnGenerate" Clicked ="OnButtonClicked" Text ="Convert to PDF" HorizontalOptions="Center" VerticalOptions="Center"></Button>

Add the below code example in the button click event handler to create a simple Word document from scratch and convert it to PDF.

C#:

private void OnButtonClicked(object sender, EventArgs e)
{
//Creates an instance of WordDocument Instance (Empty Word Document)
WordDocument wordDocument = new WordDocument();
//Add a section & 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();
}

The sample illustrating how to convert an existing Word document to PDF can be downloaded from here.


Conclusion

I hope you enjoyed learning about how to convert a Word Document to PDF in Xamarin.Forms.

You can refer to our Xamarin.Forms DoclO feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms DoclO documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (2)
Please  to leave a comment
JH
Juha Hietanen

I copy-pasted your example code and I got error like this: The type 'PdfDocument' is defined in an assembly that is not referenced. You must add a reference to assembly 'Syncfusion.Pdf.Portable, Version=16.1140.0.26, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89'.


While I have a (newer?) Syncfusion.Pdf.Portable:

Is this really something how it should be behaved?

I tried also to downgrade the NuGet library to version 24 without luck. It seems that version 26 is just today released into the NuGet but no Essential Studio is available. Maybe that will solve this issue?

DB
Dilli Babu Nandha Gopal

Hi Juha,

 

We suspect that you are referring the incorrect package to perform Word to PDF conversion in Xamarin application. Syncfusion.Xamarin.DocIORenderer and SkiaSharp are the required NuGet packages which needs to installed. As mentioned in nuget.org for Syncfusion.Xamarin.DocIORenderer , this package needs to be installed in .NET Standard 1.4 compatible or shared Xamarin projects.

 

We have internally packed Syncfusion.Pdf.Portable .NETStandard 1.4v assembly in Syncfusion.Xamarin.DocIORenderer package where as Syncfusion.Xamarin.Pdf package contains Syncfusion.Pdf.Portable .NETStandard 1.0v assembly due to which, this compilation error has occurred. To resolve this error, we strongly recommended you to refer Syncfusion.Xamarin.DocIORenderer package and uninstall Syncfusion.Xamarin.Pdf package from your application.

 

Regards,

Dilli babu.

Access denied
Access denied