ライブ環境でテストする
ウォーターマークなしで本番環境でテストしてください。
必要な場所でいつでも動作します。
Microsoft Word文書のようなGUIアプリケーションでテキストを表示する場合、テキストの位置合わせは非常に重要です。 実際、テキストを整列させるライブラリはたくさんあります。 それでもIronWordNuGetパッケージは、このプロセスを合理化する強力なツールとして登場し、C#アプリケーションのフォーマットやアライメントを管理するシンプルで効果的な方法を提供します。 開発者は、レポートやコンソールアプリケーションなど、様々な方法でテキストを整列させる必要性にしばしば遭遇します。 それでは、IronWord NuGetパッケージと、このパッケージを使ってテキストや段落を揃える方法を深く掘り下げてみましょう。
Microsoft Visual Studioで新しいプロジェクトを作成します。
IronWordはNuGetパッケージ・マネージャーからインストールしてください。
テキストアライメントを使用してWord文書を作成します。
IronWordは以下のC# NuGetライブラリです。Iron SoftwareMicrosoft Wordファイルをプログラムで作成、操作、管理するために開発されました。 このツールにより、開発者はテキストを整列させたWord文書を生成するプロセスを自動化でき、アプリケーション内でレポート、請求書、手紙、その他の種類の文書を動的に生成することが容易になります。
IronWordは左揃え、右揃え、中央揃え、両端揃えを含む複数の揃えタイプをサポートしています。 この汎用性により、開発者は特定の要件に最も適した形式を選択できます。
このパッケージは、列内のテキストの整列プロセスを簡素化し、レポートの作成や構造化されたフォーマットでのデータ表示に特に役立ちます。
IronWordはテキスト・フィールドの幅をカスタマイズすることができ、整列されたテキストが指定された境界線にきちんと収まるようにします。
Word文書内のテキストを簡単に挿入、置換、削除できます。
このライブラリは、フォント名のスタイル、フォントサイズ、色、配置など、さまざまな書式オプションをサポートしています。
IronWordは文書内に表や画像を挿入し、操作することができます。
Microsoft Wordの異なるバージョンとシームレスに動作し、互換性と使いやすさを保証します。
手紙と通知:顧客や従業員のために、パーソナライズされた手紙や通知を作成します。
IronWordは.NETアプリケーションでのWord文書作業を簡素化し、文書生成や管理作業の自動化を望む開発者にとって価値あるツールとなります。
作業を開始する前に、以下のものをご用意ください:
では、新しいVisual Studioプロジェクトを作成することから始めましょう。
壊れた画像 Pixabayから追加、ファイルから選択、またはここに画像をドラッグアンドドロップしてください。
下の画面でコンソールアプリケーションのテンプレートを選択してください。
プロジェクト名と場所を提供してください。
.NETのバージョン、できればサポート付きの最新のものを選択し、作成をクリックします。
IronWord NuGetパッケージをVisual StudioのNuGetパッケージ・マネージャーからインストールしてください。
または、以下のコマンドを使用してCLIを直接インストールしてください。
dotnet add package IronWord --version 2024.9.1
dotnet add package IronWord --version 2024.9.1
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'dotnet add package IronWord --version 2024.9.1
以下のコード例は、中央揃えテキストでWord文書を生成する方法を示しています。
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Create a new document
var document = new WordDocument();
// Create a paragraph and add text
Paragraph paragraph = new Paragraph();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
paragraph.AddText(text); // add text property
// Set text alignment to left aligned or center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
// Add the first paragraph to the document
document.AddParagraph(paragraph);
// Save the document
document.SaveAs("output.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Create a new document
var document = new WordDocument();
// Create a paragraph and add text
Paragraph paragraph = new Paragraph();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
paragraph.AddText(text); // add text property
// Set text alignment to left aligned or center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
// Add the first paragraph to the document
document.AddParagraph(paragraph);
// Save the document
document.SaveAs("output.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
License.LicenseKey = "your key"
' Create a new document
Dim document = New WordDocument()
' Create a paragraph and add text
Dim paragraph As New Paragraph()
Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
paragraph.AddText(text) ' add text property
' Set text alignment to left aligned or center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center
' Add the first paragraph to the document
document.AddParagraph(paragraph)
' Save the document
document.SaveAs("output.docx")
End Sub
End Class
**コード説明
新しい WordDocument インスタンスを初期化します。
必要に応じて、スタイル付きのテキストを作成してください。
テキストを段落に追加します。
IronWord.Models.Enums.TextAlignment.Centerで段落を揃えてください。
段落をWordDocumentインスタンスに追加し、ドキュメントをoutput.docxに保存します。
出力
それでは、さまざまなオプションを試してみましょう。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Create a new DOCX document
var doc = new WordDocument();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
// Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Center);
// Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Left);
// Create a paragraph and add text right aligned
AddPara(doc, text, TextAlignment.Right);
// Create a paragraph and add text justified aligned
AddPara(doc, text, TextAlignment.Justified);
// Save the document
doc.SaveAs("outputAllOptions.docx");
}
private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
paragraph.Alignment = alignment;
doc.AddParagraph(paragraph);
}
}
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Create a new DOCX document
var doc = new WordDocument();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
// Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Center);
// Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Left);
// Create a paragraph and add text right aligned
AddPara(doc, text, TextAlignment.Right);
// Create a paragraph and add text justified aligned
AddPara(doc, text, TextAlignment.Justified);
// Save the document
doc.SaveAs("outputAllOptions.docx");
}
private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
paragraph.Alignment = alignment;
doc.AddParagraph(paragraph);
}
}
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Friend Class Program
Shared Sub Main()
License.LicenseKey = "your key"
' Create a new DOCX document
Dim doc = New WordDocument()
Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
' Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Center)
' Create a paragraph and add text left aligned
AddPara(doc, text, TextAlignment.Left)
' Create a paragraph and add text right aligned
AddPara(doc, text, TextAlignment.Right)
' Create a paragraph and add text justified aligned
AddPara(doc, text, TextAlignment.Justified)
' Save the document
doc.SaveAs("outputAllOptions.docx")
End Sub
Private Shared Sub AddPara(ByVal doc As WordDocument, ByVal text As Text, ByVal alignment As TextAlignment)
Dim paragraph As New Paragraph()
paragraph.AddText(text)
paragraph.Alignment = alignment
doc.AddParagraph(paragraph)
End Sub
End Class
**コード説明
新しい WordDocument インスタンスを初期化します。
必要に応じて、スタイル付きのテキストを作成してください。
テキストを段落に追加します。 IronWord.Models.Enums.TextAlignment.Centerで段落を揃えてください。
段落に別のテキストを追加します。 IronWord.Models.Enums.TextAlignment.Rightで段落を揃えてください。
テキストを段落に追加します。 IronWord.Models.Enums.TextAlignment.Leftで段落を揃えてください。
テキストを段落に追加します。 IronWord.Models.Enums.TextAlignment.Justifiedで段落を揃えてください。
段落をWordDocumentインスタンスに追加し、ドキュメントをoutput.docxに保存します。
出力
IronWordライブラリはIron SoftwareのIron Suite製品のひとつです。 実行にはライセンスが必要です。 ユーザは、電子メールIDを使用して、以下からトライアルライセンスをダウンロードできます。試用ライセンスのページ. データが入力されると、ユーザーの電子メールIDにライセンスが配信されます。 このライセンスは、以下のようにIronWordライブラリを使用する前に、ユーザーコードの先頭に配置する必要があります。
License.LicenseKey = "your Key Here"
License.LicenseKey = "your Key Here"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "your Key Here"
IronWord NuGetパッケージは、.NETアプリケーションでWord文書を扱うプロセスを簡素化します。 直感的なAPIにより、開発者はテキストの配置やその他のドキュメント要素を簡単に操作できます。 テキストを左揃え、中央揃え、右揃え、両端揃えなど、IronWordはプロフェッショナルで整ったドキュメントを作成するために必要なツールを提供します。
10 の .NET API 製品 オフィス文書用