如何在 iOS 上使用 IronWord
.NET MAUI(多平台應用程式 UI)可讓您使用單一 .NET 程式碼庫建立跨 Android、iOS、Windows 和 macOS 的原生應用程式。 通過 IronWord,.NET 開發者可以輕鬆地創建、讀取、編輯和保存 Microsoft Word (.docx) 文件——完全跨平台且不需要 Microsoft Office。
IronWord 在 iOS 上無縫運作,通過共享的 .NET MAUI 程式碼基礎使用標準的 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 套件,無需特定平臺版本。
我如何安裝 IronWord 到我的 iOS 專案中?
您可以將 IronWord NuGet 套件新增到您的 .NET MAUI 專案中來安裝 IronWord,它支援包括 iOS 在內的所有主要目標。
將 IronWord 加入 .NET MAUI 專案的步驟是什麼?
您可以透過 NuGet 套件管理器或編輯 .csproj 檔案,將 IronWord 作為套件引用來將其加入您的 .NET MAUI 專案。
iOS 應用程式中的 IronWord 的使用者介面如何創建?
iOS 應用程式中的 IronWord 使用者介面可以使用 XAML 創建,包含按鈕和文本編輯器等元素來進行文件互動,如在 MainPage.xml 程式碼範例中所示。
IronWord 如何在 MAUI 專案中管理文件輸入和輸出?
IronWord 透過類似 OpenDocx 和 SaveDocx 的方法管理文件輸入/輸出,這些方法在 MainPage.xaml.cs 程式碼中處理讀取和寫入 .docx 檔案。
在 iOS 中使用 IronWord 需要安裝 Microsoft Office 嗎?
不需要。使用 IronWord 不需安裝 Microsoft Office,IronWord 是一個獨立於 Office 的原生 .NET MAUI 解決方案。
IronWord 可以在 iOS 應用程式中離線運行嗎?
可以。IronWord 能夠在 iOS 應用程式中離線運行,因為它是完全 C#/.NET MAUI 原生的。
在 iOS 模擬器上運行 IronWord 專案需要哪些要求?
要在 iOS 模擬器上運行 IronWord 專案,請在您的開發環境中將目標設定為 iOS Simulator,然後按執行以測試 .docx 文件操作。
Kye Stuart 在 Iron Software 將編碼熱情與寫作技能相結合。接受過 Yoobee 學院的軟體部署教育,他現在將複雜的技術概念轉化為清晰的教育內容。Kye 重視終身學習,並接受新技術挑戰。