如何在 iOS 上使用 IronWord
.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:
- 前往檔案 > 新建 > 專案。
- 在多平台下,選擇 .NET MAUI 應用程式。
- 為你的專案命名(例如,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">
<!-- 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>
這將建立用於載入/儲存 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();
}
}專案文件概覽
您的專案結構現在應該包括:
IronWordMauiIOS/
│
├── MainPage.xaml ← UI 佈局
├── 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 原生
- 可離線使用
- 非常適合用於建立編輯器、簡歷產生器和文件檢視器
常見問題解答
IronWord是什麼?如何在iOS上使用?
IronWord 是一個功能強大的程式庫,專為在 .NET 應用程式(包括 iOS)中處理 Word 文件而設計。它允許開發人員在其行動應用程式中無縫建立、操作和轉換 Word 文件。
如何將 IronWord 整合到我的 iOS 專案中?
要將 IronWord 整合到您的 iOS 專案中,您需要將 IronWord 程式庫新增至您的 .NET 解決方案中,配置您的專案以引用它,並使用提供的 API 來管理 Word 文件。
在 iOS 系統上使用 IronWord 需要哪些系統設定?
IronWord 需要 iOS 開發環境中相容的 .NET 元件。請確保您的開發環境已更新並支援必要的框架,以實現無縫整合。
IronWord能否在iOS上將Word文件轉換為其他格式?
是的,IronWord 可以將 Word 文件轉換為各種格式,例如 PDF,以便在 iOS 應用程式中使用,從而實現靈活的文件處理和共用。
IronWord是否支援在iOS上進行文件操作?
當然,IronWord 支援多種文件操作,包括直接在 iOS 應用程式中編輯文字、插入圖像和管理文件屬性。
是否可以使用 iOS 上的 IronWord 從頭開始建立 Word 文件?
是的,IronWord 允許您在 iOS 上從頭開始建立新的 Word 文件,並提供一套全面的工具,以程式設計方式添加內容、格式和樣式。
IronWord 如何處理 iOS 應用程式中 Word 文件的安全性問題?
IronWord 包含管理文件安全的功能,例如密碼保護和加密,確保您的 Word 文件在 iOS 應用程式上保持安全。
是否有可在 iOS 上使用 IronWord 的範例項目?
是的,Iron Software 提供範例專案和文檔,幫助開發者快速上手 iOS 上的 IronWord,展示各種功能和用例。
IronWord在iOS部署方面是否需要額外的許可?
IronWord 需要有效的授權才能部署到生產環境,包括 iOS 應用程式。請務必查看許可條款,以遵守使用指南。






