如何在 iOS 上使用 IronWord
.NET MAUI(多平台应用程序 UI)使您能够使用单个 .NET 代码库构建跨 Android、iOS、Windows 和 macOS 的原生应用程序。 通过IronWord,.NET开发人员可以轻松创建、读取、编辑和保存Microsoft Word (.docx)文档——完全跨平台且无需Microsoft Office。
IronWord通过共享的.NET MAUI代码库在iOS上无缝工作,使用标准的IronWord NuGet包 - 无需特定平台版本。
安装 IronWord NuGet 包
IronWord 是一个标准的跨平台 NuGet 包,支持所有主要的 .NET MAUI 目标平台,包括 iOS。
创建一个.NET MAUI项目
在Visual Studio中:
- 转到文件 > 新建 > 项目。
- 在多平台下,选择 .NET MAUI 应用程序。
- 给你的项目命名(例如,IronWordMauiIOS),然后点击创建。
将 IronWord 添加到您的项目中
您可以通过 NuGet 包管理器添加包,也可以通过编辑 .csproj 文件添加:
<ItemGroup>
<PackageReference Include="IronWord" Version="2026.7.2" />
</ItemGroup>
您不需要平台条件 - IronWord自动在所有目标上工作。
在 XAML 中创建应用程序界面
添加一个简单的用户界面,用于加载、编辑和保存 Word 文档。 要实现此目的,请首先将此代码添加到MainPage.xml代码中:
<?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="IronWordMauiIOS.MainPage"
BackgroundColor="White">
<VerticalStackLayout Padding="20"
Spacing="15"
VerticalOptions="Center">
<!-- Header -->
<Label Text="IronWord iOS Demo"
FontSize="24"
FontAttributes="Bold"
HorizontalOptions="Center"
TextColor="#222"/>
<!-- Open .docx -->
<Button Text=" Open Word Document"
Clicked="OpenDocx"
BackgroundColor="#007AFF"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<!-- Editable content -->
<Editor x:Name="docEditor"
Placeholder="Start editing..."
AutoSize="TextChanges"
HeightRequest="250"
FontSize="16"
TextColor="#333"
BackgroundColor="#F9F9F9"
CornerRadius="10"
Margin="0,10,0,0"/>
<!-- Save Button -->
<Button Text=" Save as .docx"
Clicked="SaveDocx"
BackgroundColor="#34C759"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<!-- Status message -->
<Label x:Name="statusLabel"
FontSize="14"
TextColor="Gray"
HorizontalOptions="Center"/>
</VerticalStackLayout>
</ContentPage>
这将创建用于加载/保存 Word 内容的按钮和编辑器用户界面。
在共享代码中使用 IronWord
在 MainPage.xaml.cs 文件中,使用 IronWord 实现 DOCX 文档的读写操作:
using IronWord;
using IronWord.Models;
using Microsoft.Maui.Storage;
using System.Text;
namespace IronWordMauiIOS;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
License.LicenseKey = "YOUR-LICENSE-KEY";
}
private async void OpenDocx(object sender, EventArgs e)
{
try
{
var file = await FilePicker.PickAsync();
if (file == null) return;
var path = Path.Combine(FileSystem.CacheDirectory, file.FileName);
using (var source = await file.OpenReadAsync())
using (var target = File.Create(path))
await source.CopyToAsync(target);
var doc = new WordDocument(path);
docEditor.Text = ExtractText(doc);
statusLabel.Text = "Document loaded successfully.";
}
catch (Exception ex)
{
statusLabel.Text = $"Error: {ex.Message}";
}
}
private async void SaveDocx(object sender, EventArgs e)
{
try
{
var document = new WordDocument();
var paragraph = new Paragraph();
paragraph.Texts.Add(new TextContent(docEditor.Text));
document.Paragraphs.Add(paragraph);
var fileName = $"ExportedDoc_{DateTime.Now:yyyyMMddHHmmss}.docx";
var path = Path.Combine(FileSystem.AppDataDirectory, fileName);
document.SaveAs(path);
statusLabel.Text = $"Saved to: {fileName}";
}
catch (Exception ex)
{
statusLabel.Text = $"Save error: {ex.Message}";
}
}
private string ExtractText(WordDocument doc)
{
var sb = new StringBuilder();
foreach (var para in doc.Paragraphs)
{
foreach (var element in para.Texts)
{
if (element is TextContent text)
sb.AppendLine(text.Text);
}
}
return sb.ToString();
}
}Imports IronWord
Imports IronWord.Models
Imports Microsoft.Maui.Storage
Imports System.Text
Namespace IronWordMauiIOS
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
InitializeComponent()
License.LicenseKey = "YOUR-LICENSE-KEY"
End Sub
Private Async Sub OpenDocx(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim file = Await FilePicker.PickAsync()
If file Is Nothing Then
Return
End If
Dim path As System.String = System.IO.Path.Combine(FileSystem.CacheDirectory, file.FileName)
Using source = Await file.OpenReadAsync()
Using target = System.IO.File.Create(path)
Await source.CopyToAsync(target)
End Using
End Using
Dim doc = New WordDocument(path)
docEditor.Text = ExtractText(doc)
statusLabel.Text = "Document loaded successfully."
Catch ex As Exception
statusLabel.Text = $"Error: {ex.Message}"
End Try
End Sub
Private Async Sub SaveDocx(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim document = New WordDocument()
Dim paragraph As New Paragraph()
paragraph.Texts.Add(New TextContent(docEditor.Text))
document.Paragraphs.Add(paragraph)
Dim fileName = $"ExportedDoc_{DateTime.Now:yyyyMMddHHmmss}.docx"
Dim path As System.String = System.IO.Path.Combine(FileSystem.AppDataDirectory, fileName)
document.SaveAs(path)
statusLabel.Text = $"Saved to: {fileName}"
Catch ex As Exception
statusLabel.Text = $"Save error: {ex.Message}"
End Try
End Sub
Private Function ExtractText(ByVal doc As WordDocument) As String
Dim sb = New StringBuilder()
For Each para In doc.Paragraphs
For Each element In para.Texts
Dim tempVar As Boolean = TypeOf element Is TextContent
Dim text As TextContent = If(tempVar, CType(element, TextContent), Nothing)
If tempVar Then
sb.AppendLine(text.Text)
End If
Next element
Next para
Return sb.ToString()
End Function
End Class
End NamespaceImports IronWord
Imports IronWord.Models
Imports Microsoft.Maui.Storage
Imports System.Text
Namespace IronWordMauiIOS
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
InitializeComponent()
License.LicenseKey = "YOUR-LICENSE-KEY"
End Sub
Private Async Sub OpenDocx(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim file = Await FilePicker.PickAsync()
If file Is Nothing Then
Return
End If
Dim path As System.String = System.IO.Path.Combine(FileSystem.CacheDirectory, file.FileName)
Using source = Await file.OpenReadAsync()
Using target = System.IO.File.Create(path)
Await source.CopyToAsync(target)
End Using
End Using
Dim doc = New WordDocument(path)
docEditor.Text = ExtractText(doc)
statusLabel.Text = "Document loaded successfully."
Catch ex As Exception
statusLabel.Text = $"Error: {ex.Message}"
End Try
End Sub
Private Async Sub SaveDocx(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim document = New WordDocument()
Dim paragraph As New Paragraph()
paragraph.Texts.Add(New TextContent(docEditor.Text))
document.Paragraphs.Add(paragraph)
Dim fileName = $"ExportedDoc_{DateTime.Now:yyyyMMddHHmmss}.docx"
Dim path As System.String = System.IO.Path.Combine(FileSystem.AppDataDirectory, fileName)
document.SaveAs(path)
statusLabel.Text = $"Saved to: {fileName}"
Catch ex As Exception
statusLabel.Text = $"Save error: {ex.Message}"
End Try
End Sub
Private Function ExtractText(ByVal doc As WordDocument) As String
Dim sb = New StringBuilder()
For Each para In doc.Paragraphs
For Each element In para.Texts
Dim tempVar As Boolean = TypeOf element Is TextContent
Dim text As TextContent = If(tempVar, CType(element, TextContent), Nothing)
If tempVar Then
sb.AppendLine(text.Text)
End If
Next element
Next para
Return sb.ToString()
End Function
End Class
End Namespace项目文件概览
您的项目结构现在应该包括:
IronWordMauiIOS/
│
├── MainPage.xaml ← UI Layout
├── MainPage.xaml.cs ← Logic for UI (Word document actions)
├── IronWordMauiIOS.csproj ← References IronWord NuGet package
├── Platforms/ios/ ← iOS-specific config (no changes needed here)
└── ...
运行项目
- 将目标设置为 iOS 模拟器。
- 按下运行键。
- 在模拟 iOS 设备上直接测试读取和写入 .docx 文档。
最后说明
- 完全跨平台(iOS、Android、Windows、macOS)
- 无需Microsoft Office或Interop
- 100% C# / .NET MAUI 原生
- 可离线使用
- 非常适合用于构建编辑器、简历生成器和文档查看器
常见问题
IronWord在iOS应用程序中的用途是什么?
IronWord允许开发者在iOS应用程序中使用.NET代码库创建、读取、编辑和保存Microsoft Word (.docx)文档,而无需Microsoft Office。
iOS是否需要IronWord的特定平台版本?
不需要,IronWord通过共享的.NET MAUI代码库在iOS上无缝工作,使用标准的IronWord NuGet包,无需特定于平台的版本。
如何为我的iOS项目安装IronWord?
您可以通过将IronWord NuGet包添加到您的.NET MAUI项目中来安装IronWord,该项目支持包括iOS在内的所有主要目标。
将IronWord添加到.NET MAUI项目的步骤是什么?
您可以通过NuGet包管理器或编辑.csproj文件以将IronWord作为包引用,来将IronWord添加到您的.NET MAUI项目中。
如何在iOS应用程序中创建IronWord的用户界面?
iOS应用程序中IronWord的用户界面可以使用XAML创建,包含用于文档交互的按钮和文本编辑器等元素,如MainPage.xml代码示例中所示。
IronWord如何在MAUI项目中管理文档输入和输出?
IronWord通过如OpenDocx和SaveDocx等方法管理文档输入/输出,这些方法在MainPage.xaml.cs代码中处理.docx文件的读取和写入。
在iOS上使用IronWord是否需要安装Microsoft Office?
不需要,Microsoft Office不需要安装即可使用IronWord,后者是一种本地.NET MAUI解决方案,独立于Office运行。
IronWord能否在iOS应用程序中脱机运行?
是的,IronWord能够在iOS应用程序中脱机运行,因为它完全是C#/.NET MAUI本地的。
在iOS模拟器上运行IronWord项目需要什么?
要在iOS模拟器上运行IronWord项目,请在您的开发环境中将目标设置为iOS Simulator并按下运行,以测试.docx文档操作。
Kye Stuart 在 Iron Software 中将编码热情与写作技能结合在一起。他在 Yoobee 学院接受软件部署教育,现在将复杂的技术概念转化为清晰的教育内容。Kye 重视终身学习,接受新的技术挑战。