如何在 iOS 上使用 IronWord

This article was translated from English: Does it need improvement?
Translated
View the article in English

.NET MAUI(多平台应用程序 UI)使您能够使用单个 .NET 代码库构建跨 Android、iOS、Windows 和 macOS 的原生应用程序。 借助 IronWord,.NET 开发人员可以轻松创建、读取、编辑和保存 Microsoft Word (.docx) 文档——完全跨平台,无需 Microsoft Office。

IronWord 通过共享的 .NET MAUI 代码库,使用标准的 IronWord NuGet 包,可以在 iOS 上无缝运行——无需特定于平台的版本。

安装 IronWord NuGet 包

IronWord 是一个标准的跨平台 NuGet 包,支持所有主要的 .NET MAUI 目标平台,包括 iOS。

Install-Package IronWord

创建一个.NET MAUI项目

在Visual Studio中:

  1. 转到文件 > 新建 > 项目。
  2. 在多平台下,选择 .NET MAUI 应用程序。
  3. 给你的项目命名(例如,IronWordMauiIOS),然后点击创建。

将 IronWord 添加到您的项目中

您可以通过 NuGet 包管理器添加包,也可以通过编辑 .csproj 文件添加:

<ItemGroup>
  <PackageReference Include="IronWord" Version="2025.5.0" />
</ItemGroup>
<ItemGroup>
  <PackageReference Include="IronWord" Version="2025.5.0" />
</ItemGroup>
XML

您无需设置平台条件——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>
<?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>
XML

这将创建用于加载/保存 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();
    }
}
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 Namespace
$vbLabelText   $csharpLabel

项目文件概览

您的项目结构现在应该包括:

IronWordMauiIOS/
│
├── MainPage.xaml ← UI 布局
├── MainPage.xaml.cs ← 用户界面逻辑(Word 文档操作)
├── IronWordMauiIOS.csproj ← 引用 IronWord NuGet 包
├── Platforms/ios/ ← iOS 特定配置(此处无需更改)
└── ...

运行项目

  1. 将目标设置为 iOS 模拟器。
  2. 按下运行键。
  3. 在模拟 iOS 设备上直接测试读取和写入 .docx 文档。

最后说明

  • 完全跨平台(iOS、Android、Windows、macOS) 无需 Microsoft Office 或 Interop
  • 100% C# / .NET MAUI 原生
  • 可离线使用
  • 非常适合用于构建编辑器、简历生成器和文档查看器

常见问题解答

什么是IronWord,它如何在iOS上使用?

IronWord是一个强大的库,旨在在.NET应用程序中处理Word文档,包括iOS。它允许开发人员在移动应用中无缝创建、操作和转换Word文档。

如何将IronWord集成到我的iOS项目中?

要将IronWord集成到您的iOS项目中,您需要将IronWord库添加到您的.NET解决方案中,配置您的项目以引用它,并使用提供的API来管理Word文档。

在iOS上使用IronWord的系统要求是什么?

IronWord需要在您的iOS开发设置中兼容的.NET环境。确保您的开发环境已更新并支持必要的框架以实现无缝集成。

IronWord能否在iOS上将Word文档转换为其他格式?

是的,IronWord可以在您的iOS应用中将Word文档转换为各种格式,例如PDF,实现多种文档处理和共享。

IronWord是否支持在iOS上操作文档?

当然,IronWord支持多种文档操作,包括在iOS应用中直接编辑文本、插入图像和管理文档属性。

是否可以使用IronWord在iOS上从头创建Word文档?

是的,IronWord允许您在iOS上从头创建新的Word文档,提供一整套工具以编程方式添加内容、格式和样式。

IronWord如何在iOS应用中处理Word文档的安全性?

IronWord包含管理文档安全的功能,如密码保护和加密,确保您的Word文档在iOS应用中保持安全。

是否有可用于在iOS上使用IronWord的示例项目?

是的,Iron Software提供示例项目和文档,帮助开发人员快速开始在iOS上使用IronWord,展示各种功能和用例。

在iOS部署中IronWord是否需要额外的许可?

IronWord在包括iOS应用程序在内的生产环境中部署需要有效的许可证。请确保检查许可条款以遵守使用指南。

Kye Stuart
技术作家

Kye Stuart 在 Iron Software 中将编码热情与写作技能结合在一起。他在 Yoobee 学院接受软件部署教育,现在将复杂的技术概念转化为清晰的教育内容。Kye 重视终身学习,接受新的技术挑战。

工作之余,他们喜欢 PC 游戏、Twitch 上的直播,以及户外活动如园艺和带狗 Jaiya 散步。Kye 的直截了当的方法使他们成为 Iron Software 使命的关键,即为全球开发者解密技术。

准备开始了吗?
Nuget 下载 27,129 | Version: 2025.11 刚刚发布