如何在iOS上使用IronWord

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

.NET MAUI(多平台應用介面)使您能夠使用單一 .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。

Install-Package IronWord

建立一個.NET MAUI專案

在Visual Studio中:

  1. 前往 File > New > Project。
  2. 在多平台下,選擇 .NET MAUI App。
  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中建立應用介面

新增一個簡單的UI來載入、編輯和儲存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>
XML

這將建立按鈕和一個編輯器UI,用於載入/儲存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 Layout
├── MainPage.xaml.cs          ← UI邏輯(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能否將Word文件轉換為其他格式在iOS上?

是的,IronWord可以在您的iOS應用程式中將Word文件轉換為各種格式,例如PDF,從而實現多功能的文件處理和共享。

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應用程式。請確保檢查授權條款以遵循使用指南。

Kye Stuart
技術作家

Kye Stuart在Iron Software結合了編碼熱情與寫作技能。就讀於Yoobee學院,專攻軟體部署,現在將複雜的技術概念轉化為清晰的教育內容。Kye重視終身學習,樂於迎接新的技術挑戰。

在工作之外,他喜歡PC遊戲、在Twitch上直播,以及戶外活動如園藝和帶他的狗Jaiya散步。Kye的直率方法使他成為Iron Software全球開發者解謎技術使命的關鍵。

準備開始了嗎?
Nuget 下載 49,323 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明嗎? PM > Install-Package IronWord
運行範例觀看您的資料變成Word檔。