1. 製品
  2.   編集者
  3.   .NET
  4.   FileFormat.Words for .NET
 
  

C# および ASP.NET での文書処理用オープンソース .NET API

FileFormat.Words for .NET を使用して、.NET アプリケーション内から Microsoft Word 文書の段落、画像、表、その他のコンテンツを作成、開き、更新します。

FileFormat.Words for .NET API とは何ですか?

FileFormat.Words for .NET は、Microsoft Word ファイルを操作するオープン ソースの .NET API です。C# および ASP.NET で開発された .NET アプリケーションで、Word 文書の作成、オープン、変更などの Word 文書処理に使用できます。この API は、Microsoft OpenXML SDK などのさまざまなオープン ソース エンジンをラップして開発されています。レポートの生成、文書ワークフローの自動化、文書管理システムの強化を行う .NET アプリケーションを構築できます。FileFormat.Words for .NET は、MS Word 文書処理機能の統合を簡素化します。

FileFormat.Words for .NET API - 主な機能

以下は、FileFormat.Words for .NET API の主な機能の一部です。

  • 包括的なドキュメント処理: .NET アプリケーションで Word ドキュメントを作成、編集、変換、レンダリングします。
  • ファイル形式のサポート: DOC、DOCX、RTF、HTML、PDF、EPUB、ODT などをサポートします。
  • ドキュメント操作: ページ、セクション、ヘッダー、フッター、ブックマーク、目次を追加または削除します。
  • 高忠実度レンダリング: ドキュメントを画像または固定レイアウト形式 (PDF、XPS) に高精度でレンダリングします。
  • テキスト操作: テキストの検索と置換、テキストの書式設定、テキストの抽出、カスタム スタイルの適用を行います。
  • 差し込み印刷とレポート作成: テンプレートとデータ ソースからドキュメントを作成するための差し込み印刷をサポートします。
  • 高度なドキュメント要素: 表、画像、図形、グラフ、SmartArt オブジェクトを挿入します。
  • デジタル署名と暗号化: デジタル署名、パスワード保護を追加し、ドキュメントを暗号化します。
  • マクロとスクリプト: マクロ (VBA) を保存および管理し、複雑な自動化タスクを可能にします。
  • 変換機能: Word 形式と、PDF や HTML などのさまざまなファイル形式の間で変換します。
  • クラウド サポート: クラウドベースのドキュメント操作のために Aspose Cloud と統合します。
  • 包括的な API サポート: 広範な API ドキュメント、例、コミュニティ サポート。
GitHub

GitHub の統計

名前: FileFormat.Words-for-.NET
言語: C#
出演者: 29
フォーク: 7
ライセンス: MIT License
リポジトリの最終更新日時 2025-03-18

FileFormat.Words for .NET API の使用を開始する

FileFormat.Words for .NET ライブラリは、GitHub または Nugget からダウンロードできます。

インストール

FileFormat.Words for .NET のインストールは簡単で、以下に示すように Nugget から実行できます。

.NET API 用の FileFormat.Words のインストール


NuGet\Install-Package FileFormat.Words

.NET API の FileFormat.Words のコード例

FileFormat.Words for .NET は、DOC および DOCX ファイル形式を含む Word ドキュメントを操作するための豊富な機能を提供する強力な API です。ドキュメントを最初から作成したり、既存のドキュメントを開いてその内容を更新したり、ドキュメントを PDF などのさまざまな形式に変換したりするなど、豊富なワード プロセッシング機能を提供します。この API は、ファイル形式の基礎となる詳細を調べることなく、C# および ASP.NET アプリケーションで簡単に使用できます。

.NET で Word 文書 DOCX ファイルを作成する方法は?

FileFormat.Words for .NET API を使用すると、わずか数行のコードで、C# または ASP.NET アプリケーション内から DOCX ドキュメントを最初から作成できます。API を使用して空のドキュメントを作成するには、次のソース コードを使用できます。

/// <summary>
/// Creates a new Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Generates paragraphs with heading styles defined by the Word document template.
/// Adds normal paragraphs under each heading paragraph, including text runs with various fonts as per the template.
/// Saves the newly created Word Document.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document will be saved (default is the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file (default is "WordParagraphs.docx") to be saved to the directlry
/// defined by documentDirectory..
/// </param>
public void CreateWordParagraphs(string documentDirectory = docsDirectory, string filename = "WordParagraphs.docx")
{
try
{
// Initialize a new word document with the default template
var doc = new FileFormat.Words.Document();
System.Console.WriteLine("Word Document with default template initialized");
// Initialize the body with the new document
var body = new FileFormat.Words.Body(doc);
System.Console.WriteLine("Body of the Word Document initialized");
// Get all paragraph styles
var paragraphStyles = doc.GetElementStyles().ParagraphStyles;
System.Console.WriteLine("Paragraph styles loaded");
// Get all fonts defined by FontTable and Theme
var fonts = doc.GetElementStyles().TableFonts;
var fontsTheme = doc.GetElementStyles().ThemeFonts;
System.Console.WriteLine("Fonts defined by FontsTable and Theme loaded");
// Merge all fonts
fonts.AddRange(fontsTheme);
System.Console.WriteLine("All Fonts merged");
// Create Headings Paragraph and append to the body.
foreach (var paragraphStyle in paragraphStyles.Where(style => !style.Contains("Normal")))
{
var paragraphWithStyle = new FileFormat.Words.IElements.Paragraph { Style = paragraphStyle };
paragraphWithStyle.AddRun(new Run {
Text = $"Paragraph with {paragraphStyle} Style"
});
System.Console.WriteLine($"Styled Paragraph with {paragraphStyle} Created");
body.AppendChild(paragraphWithStyle);
System.Console.WriteLine($"Styled Paragraph with {paragraphStyle} Appended to Word Document Body");
// Create Normal Paragraph and include text runs with various fonts as per the template.
var paragraphNormal = new FileFormat.Words.IElements.Paragraph();
System.Console.WriteLine("Normal Paragraph Created");
paragraphNormal.AddRun(new FileFormat.Words.IElements.Run
{
Text = $"Text in normal paragraph with default font and size but with bold " +
$"and underlined Gray Color ",
Color = FileFormat.Words.IElements.Colors.Gray,
Bold = true,
Underline = true
});
foreach (var font in fonts)
{
paragraphNormal.AddRun(new Run
{
Text = $"Text in normal paragraph with font {font} and size 10 but with default " +
$"color, bold, and underlines. ",
FontFamily = font,
FontSize = 10
});
}
System.Console.WriteLine("All Runs with all fonts Created for Normal Paragraph");
body.AppendChild(paragraphNormal);
System.Console.WriteLine($"Normal Paragraph Appended to Word Document Body");
}
// Save the newly created Word Document.
doc.Save($"{documentDirectory}/{filename}");
System.Console.WriteLine($"Word Document {filename} Created. Please check directory: "+
$"{System.IO.Path.GetFullPath(documentDirectory)}");
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}

C# で既存の DOCX ファイルを読み取る方法は?

FileFormat.Words for .NET を使用すると、C# アプリケーションで既存の Word ドキュメント DOCX を読み取ることができます。以下のコード サンプルに示すように、アプリケーションでドキュメントを読み込み、その内容を変更し、API を使用してディスクに保存し直すことができます。

/// <summary>
/// Loads a Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Traverses paragraphs and displays associated styles as defined by the Word document template.
/// Traverses through each run (text fragment) within each paragraph and displays fragment values.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document to load is present
/// (default is the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file to load (default is "WordParagraphs.docx" - please replace it
/// with your word document present at the directory defined by documentDirectory).
/// </param>
public void ReadWordParagraphs(string documentDirectory = docsDirectory, string filename = "WordParagraphs.docx")
{
try
{
// Load the Word Document
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}");
var body = new FileFormat.Words.Body(doc);
var num = 0;
System.Console.WriteLine("Paragraphs Plain Text");
// Traverse and display paragraphs with plain text
foreach (var paragraph in body.Paragraphs)
{
num++;
System.Console.WriteLine($" Paragraph Number: {num}");
System.Console.WriteLine($" Paragraph Style: {paragraph.Style}");
System.Console.WriteLine($" Paragraph Text: {paragraph.Text}");
}
num = 0;
var runnum = 0;
System.Console.WriteLine("Paragraphs with formatting");
// Traverse and display paragraphs with formatting details
foreach (var paragraph in body.Paragraphs)
{
num++;
System.Console.WriteLine($" Paragraph Number: {num}");
System.Console.WriteLine($" Paragraph Style: {paragraph.Style}");
// Traverse and display runs within each paragraph
foreach (var run in paragraph.Runs)
{
runnum++;
System.Console.WriteLine($" Text fragment ({num} - {runnum}): {run.Text}");
System.Console.WriteLine($" Font fragment ({num} - {runnum}): {run.FontFamily}");
System.Console.WriteLine($" Color fragment ({num} - {runnum}): {run.Color}");
System.Console.WriteLine($" Size fragment ({num} - {runnum}): {run.FontSize}");
System.Console.WriteLine($" Bold fragment ({num} - {runnum}): {run.Bold}");
System.Console.WriteLine($" Italic fragment ({num} - {runnum}): {run.Italic}");
System.Console.WriteLine($" Underline fragment ({num} - {runnum}): {run.Underline}");
}
runnum = 0;
}
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}

.NET で Word 文書に表を作成する方法は?

FileFormat.Words for .NET SDK を使用すると、Word DOCX ドキュメントに表を簡単に追加できます。API を使用して複数の行と列を持つ表を作成し、以下のコード サンプルに示すようにドキュメントにコンテンツを追加できます。

/// <summary>
/// Creates a new Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Generates 5(rows) x 3(cols) tables with table styles defined by the Word document template.
/// Appends each table to the body of the word document.
/// Saves the newly created word document.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document will be saved (default is root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file (default is "WordTables.docx").
/// </param>
public void CreateWordDocumentWithTables(string documentDirectory = "../../../",
string filename = "WordTables.docx")
{
try
{
// Initialize a new word document with the default template
var doc = new FileFormat.Words.Document();
System.Console.WriteLine("Word Document with default template initialized");
// Initialize the body with the new document
var body = new FileFormat.Words.Body(doc);
System.Console.WriteLine("Body of the Word Document initialized");
// Get all table styles
var tableStyles = doc.GetElementStyles().TableStyles;
System.Console.WriteLine("Table styles loaded");
// Create Headings Paragraph and append to the body.
foreach (var tableStyle in tableStyles)
{
var table = new FileFormat.Words.IElements.Table(5,3);
table.Style = tableStyle;
table.Column.Width = 2500;
var rowNumber = 0;
var columnNumber = 0;
var para = new FileFormat.Words.IElements.Paragraph();
para.Style = FileFormat.Words.IElements.Headings.Heading1;
para.AddRun(new FileFormat.Words.IElements.Run {
Text = $"Table With Style '{tableStyle}' : "
});
body.AppendChild(para);
foreach (var row in table.Rows)
{
rowNumber++;
foreach(var cell in row.Cells)
{
columnNumber++;
para = new FileFormat.Words.IElements.Paragraph();
para.AddRun(new FileFormat.Words.IElements.Run {
Text = $"Row {rowNumber} Column {columnNumber}"
});
cell.Paragraphs.Add(para);
}
columnNumber = 0;
}
body.AppendChild(table);
System.Console.WriteLine($"Table with style {tableStyle} created and appended");
}
// Save the newly created Word Document.
doc.Save($"{documentDirectory}/{filename}");
System.Console.WriteLine($"Word Document {filename} Created. Please check directory: "+
$"{System.IO.Path.GetFullPath(documentDirectory)}");
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}

FileFormat.Words for .NET リソース

結論

類似製品

 日本語