How to Read and Write Word Documents on Android in .NET MAUI
.NET MAUI (Multi-platform App UI) lets developers build native apps for Android, iOS, macOS, and Windows using a single C# codebase. This approach simplifies development and delivers native performance on all platforms.
Although IronWord doesn’t have a dedicated Android package, it works smoothly on Android through .NET MAUI. This means you can build apps that read and write Word docs on Android devices with ease.
How to Use IronWord on Android in .NET MAUI
- Create a .NET MAUI App Project
- Download the C# library to manipulate Word documents on Android
- Design the UI in MainPage.xaml
- Implement Document Handling in MainPage.xaml.cs
- Run Your App on Android
Step 1: Create your .NET MAUI App Project
Begin by opening Visual Studio and creating a new .NET MAUI App project. This project type supports building apps for multiple platforms from one codebase.
Choose a clear project name like IronWordMauiAndroid to stay organized. This setup lays the foundation to build a native Android app with minimal platform-specific code.
Step 2: Add the IronWord NuGet Package
IronWord can be seamlessly integrated into your .NET projects through its NuGet package. To add IronWord via NuGet, right-click your project and select Manage NuGet Packages. Search for "IronWord" and install the latest stable version.
Alternatively, you can easily add it using the NuGet Package Manager Console by running this line:
Install-Package IronWord
Step 3: Design the UI in MainPage.xaml
To let users create and save Word documents on Android, you'll design a simple and clean UI using XAML. This interface includes:
- A multi-line text editor where users can type or edit content.
- A "Save as Word Document" button that triggers saving the current text to a .docx file.
- A status label to provide feedback or error messages to the user.
Here’s the XAML markup that defines this layout:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="IronWordAndroid.MainPage">
<VerticalStackLayout Padding="20" Spacing="15">
<Editor x:Name="WordEditor"
Placeholder="Enter your text..."
AutoSize="TextChanges"
HeightRequest="300"/>
<Button Text="Save as Word Document" Clicked="OnSaveWordClicked"/>
<Label x:Name="StatusLabel" FontSize="12" TextColor="Gray"/>
</VerticalStackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="IronWordAndroid.MainPage">
<VerticalStackLayout Padding="20" Spacing="15">
<Editor x:Name="WordEditor"
Placeholder="Enter your text..."
AutoSize="TextChanges"
HeightRequest="300"/>
<Button Text="Save as Word Document" Clicked="OnSaveWordClicked"/>
<Label x:Name="StatusLabel" FontSize="12" TextColor="Gray"/>
</VerticalStackLayout>
</ContentPage>
Step 4: Implement Document Handling in MainPage.xaml.cs
With the UI set up, you can now add logic to handle saving documents. In MainPage.xaml.cs, set your IronWord license key in the constructor, then implement the OnSaveWordClicked method.
When the button is tapped, the app creates a new WordDocument, adds the text from the Editor as a paragraph, and saves the file. On Android, it's saved to the Downloads folder; on other platforms, it uses the app’s cache directory.
A try-catch block ensures any errors are caught and shown in the status label.
Here’s the full working code:
using IronWord;
using IronWord.Models;
using Microsoft.Maui.Storage;
using System.Text;
namespace IronWordAndroid;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Set your IronWord license key
License.LicenseKey = "YOUR-LICENSE-KEY";
}
private async void OnSaveWordClicked(object sender, EventArgs e)
{
try
{
// Create new Word document
var document = new WordDocument();
Paragraph paragraph = new Paragraph(new TextContent(WordEditor.Text));
// Add basic text content
document.AddParagraph(paragraph);
// Generate a filename
string fileName = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx";
#if ANDROID
string filePath = Path.Combine("/storage/emulated/0/Download", fileName);
#else
string filePath = Path.Combine(FileSystem.CacheDirectory, fileName);
#endif
// Save to path
document.SaveAs(filePath);
StatusLabel.Text = $"Saved to: {filePath}";
await DisplayAlert("Success", $"File saved: {filePath}", "OK");
}
catch (Exception ex)
{
StatusLabel.Text = $"Error: {ex.Message}";
}
}
}
using IronWord;
using IronWord.Models;
using Microsoft.Maui.Storage;
using System.Text;
namespace IronWordAndroid;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Set your IronWord license key
License.LicenseKey = "YOUR-LICENSE-KEY";
}
private async void OnSaveWordClicked(object sender, EventArgs e)
{
try
{
// Create new Word document
var document = new WordDocument();
Paragraph paragraph = new Paragraph(new TextContent(WordEditor.Text));
// Add basic text content
document.AddParagraph(paragraph);
// Generate a filename
string fileName = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx";
#if ANDROID
string filePath = Path.Combine("/storage/emulated/0/Download", fileName);
#else
string filePath = Path.Combine(FileSystem.CacheDirectory, fileName);
#endif
// Save to path
document.SaveAs(filePath);
StatusLabel.Text = $"Saved to: {filePath}";
await DisplayAlert("Success", $"File saved: {filePath}", "OK");
}
catch (Exception ex)
{
StatusLabel.Text = $"Error: {ex.Message}";
}
}
}
Imports IronWord
Imports IronWord.Models
Imports Microsoft.Maui.Storage
Imports System.Text
Namespace IronWordAndroid
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
InitializeComponent()
' Set your IronWord license key
License.LicenseKey = "YOUR-LICENSE-KEY"
End Sub
Private Async Sub OnSaveWordClicked(ByVal sender As Object, ByVal e As EventArgs)
Try
' Create new Word document
Dim document = New WordDocument()
Dim paragraph As New Paragraph(New TextContent(WordEditor.Text))
' Add basic text content
document.AddParagraph(paragraph)
' Generate a filename
Dim fileName As String = $"MyWordDoc_{DateTime.Now:yyyyMMddHHmmss}.docx"
#If ANDROID Then
Dim filePath As String = Path.Combine("/storage/emulated/0/Download", fileName)
#Else
Dim filePath As String = Path.Combine(FileSystem.CacheDirectory, fileName)
#End If
' Save to path
document.SaveAs(filePath)
StatusLabel.Text = $"Saved to: {filePath}"
Await DisplayAlert("Success", $"File saved: {filePath}", "OK")
Catch ex As Exception
StatusLabel.Text = $"Error: {ex.Message}"
End Try
End Sub
End Class
End Namespace
Step 5: Run Your App on Android
Deploy your application effortlessly to an Android emulator or physical device straight from Visual Studio. This allows you to quickly test essential features such as opening Word documents, editing their content, and saving any changes.
The integration of the native Android file picker offers a seamless and familiar experience for users, improving overall usability.
Since IronWord runs entirely within the .NET environment, there’s no need for additional SDKs or platform-specific dependencies—making your app simpler to develop, maintain, and distribute.