如何在 iOS 上使用 IronWord
.NET MAUI(多平台應用程式使用者介面)讓您能夠使用單一 .NET 程式碼庫,在 Android、iOS、Windows 和 macOS 平台上建置原生應用程式。 透過 IronWord,.NET 開發人員可輕鬆建立、讀取、編輯及儲存 Microsoft Word (.docx) 文件——完全跨平台,且無需 Microsoft Office。
IronWord 透過標準的 IronWord NuGet 套件,利用共享的 .NET MAUI 程式碼庫,可在 iOS 上無縫運作——無需平台專屬版本。
安裝 IronWord NuGet 套件
IronWord 提供標準的跨平台 NuGet 套件,並支援所有主要的 .NET MAUI 目標平台,包括 iOS。
Install-Package IronWord
建立 .NET MAUI 專案
在 Visual Studio 中:
- 前往"檔案">"新增">"專案"。
- 在"多平台"下,選擇 .NET MAUI App。
- 為您的專案命名(例如:IronWordMauiIOS),然後點擊"建立"。
將 IronWord 加入您的專案
您可以透過 NuGet 套件管理員,或編輯您的 .csproj 檔案來安裝此套件:
<ItemGroup>
<PackageReference Include="IronWord" Version="2025.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IronWord" Version="2025.5.0" />
</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">
<Label Text="IronWord iOS Demo"
FontSize="24"
FontAttributes="Bold"
HorizontalOptions="Center"
TextColor="#222"/>
<Button Text=" Open Word Document"
Clicked="OpenDocx"
BackgroundColor="#007AFF"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<Editor x:Name="docEditor"
Placeholder="Start editing..."
AutoSize="TextChanges"
HeightRequest="250"
FontSize="16"
TextColor="#333"
BackgroundColor="#F9F9F9"
CornerRadius="10"
Margin="0,10,0,0"/>
<Button Text=" Save as .docx"
Clicked="SaveDocx"
BackgroundColor="#34C759"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<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">
<Label Text="IronWord iOS Demo"
FontSize="24"
FontAttributes="Bold"
HorizontalOptions="Center"
TextColor="#222"/>
<Button Text=" Open Word Document"
Clicked="OpenDocx"
BackgroundColor="#007AFF"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<Editor x:Name="docEditor"
Placeholder="Start editing..."
AutoSize="TextChanges"
HeightRequest="250"
FontSize="16"
TextColor="#333"
BackgroundColor="#F9F9F9"
CornerRadius="10"
Margin="0,10,0,0"/>
<Button Text=" Save as .docx"
Clicked="SaveDocx"
BackgroundColor="#34C759"
TextColor="White"
CornerRadius="10"
HeightRequest="50"/>
<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();
}
}
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
專案檔案概覽
您的專案結構現應包含:
IronWordMauiIOS/
│
├── MainPage.xaml ← 介面佈局
├── MainPage.xaml.cs ← 使用者介面邏輯(WORD 文件操作)
├── IronWordMauiIOS.csproj ← 引用 IronWord NuGet 套件
├── Platforms/ios/ ← iOS 專用設定(此處無需修改)
└── ...
執行專案
- 將目標設定為 iOS 模擬器。
- 按下"執行"。
- 直接在模擬的 iOS 裝置上測試讀取與寫入 .docx 文件。
最終說明
- 完全跨平台(iOS、Android、Windows、macOS)
- 無需 Microsoft Office 或 Interop
- 100% C# / .NET MAUI 原生
- 支援離線運作
- 非常適合用於建立編輯器、履歷Builder及文件檢視器
常見問題
什麼是 IronWord,以及如何在 iOS 上使用它?
IronWord 是一款強大的程式庫,專為在 .NET 應用程式(包括 iOS)中處理 Word 文件而設計。它讓開發人員能夠在行動應用程式內無縫地建立、操作及轉換 Word 文件。
如何將 IronWord 整合到我的 iOS 專案中?
若要將 IronWord 整合至您的 iOS 專案中,您需要將 IronWord程式庫新增至您的 .NET 解決方案中,設定專案以引用該程式庫,並使用提供的 API 來管理 Word 文件。
在 iOS 上使用 IronWord 的系統需求為何?
IronWord 需要您的 iOS 開發環境具備相容的 .NET Framework 環境。請確保您的開發環境已更新,並支援必要的框架,以實現無縫整合。
IronWord 能否在 iOS 上將 WORD 文件轉換為其他格式?
是的,IronWord 可在您的 iOS 應用程式中將 WORD 文件轉換為各種格式(例如 PDF),實現多樣化的文件處理與分享功能。
IronWord 是否支援在 iOS 上進行文件處理?
沒問題,IronWord 支援廣泛的文件處理功能,包括直接在 iOS 應用程式中編輯文字、插入圖片以及管理文件屬性。
是否可以透過 iOS 版的 IronWord 從頭開始建立 Word 文件?
是的,IronWord 允許您在 iOS 上從頭開始建立新的 WORD 文件,並提供一套完整的 Suite,可透過程式設計方式新增內容、格式及樣式。
IronWord 在 iOS 應用程式中如何處理 WORD 文件的安全性?
IronWord 具備管理文件安全性的功能,例如密碼保護與加密,確保您的 WORD 文件在 iOS 應用程式中保持安全。
是否有適用於 iOS 系統的 IronWord 範例專案?
是的,Iron Software 提供範例專案與文件,協助開發者快速在 iOS 上開始使用 IronWord,並展示各項功能與應用情境。
IronWord 在 iOS 部署時是否需要額外的授權?
IronWord 需具備有效授權方可部署於生產環境,包括 iOS 應用程式。請務必查閱授權條款,以符合使用規範。

