1. Các sản phẩm
  2.   Biên tập viên
  3.   .NET
  4.   FileFormat.Words for .NET
 
  

API .NET nguồn mở để xử lý tài liệu văn bản bằng C# và ASP.NET

Tạo, mở và cập nhật đoạn văn, hình ảnh, bảng và nội dung khác của tài liệu Microsoft Word từ bên trong ứng dụng .NET của bạn bằng FileFormat.Words cho .NET

FileFormat.Words cho .NET API là gì?

FileFormat.Words for .NET là một API .NET mã nguồn mở để làm việc với các tệp Microsoft Word. Nó có thể được sử dụng trong các ứng dụng .NET được phát triển bằng C# và ASP.NET để xử lý các tài liệu Word như tạo, mở và sửa đổi các tài liệu Word. API đã được phát triển bằng cách đóng gói nhiều công cụ mã nguồn mở khác nhau bao gồm Microsoft OpenXML SDK. Bạn có thể xây dựng các ứng dụng .NET để tạo báo cáo, tự động hóa quy trình làm việc của tài liệu và cải thiện các hệ thống quản lý tài liệu. FileFormat.Words for .NET đơn giản hóa việc tích hợp chức năng xử lý tài liệu MS Word.

FileFormat.Words cho API .NET - Các tính năng chính

Sau đây là một số tính năng chính của FileFormat.Words cho API .NET:

  • Xử lý tài liệu toàn diện: Tạo, chỉnh sửa, chuyển đổi và hiển thị tài liệu Word trong các ứng dụng .NET.
  • Hỗ trợ định dạng tệp: Hỗ trợ DOC, DOCX, RTF, HTML, PDF, EPUB, ODT, v.v.
  • Điều chỉnh tài liệu: Thêm hoặc xóa các trang, phần, đầu trang, chân trang, dấu trang và mục lục.
  • Kết xuất độ trung thực cao: Kết xuất tài liệu thành hình ảnh hoặc định dạng bố cục cố định (PDF, XPS) với độ chính xác cao.
  • Xử lý văn bản: Tìm và thay thế văn bản, định dạng văn bản, trích xuất văn bản và áp dụng các kiểu tùy chỉnh.
  • Trộn thư và báo cáo: Hỗ trợ trộn thư để tạo tài liệu từ mẫu và nguồn dữ liệu.
  • Các thành phần tài liệu nâng cao: Chèn bảng, hình ảnh, hình dạng, biểu đồ và đối tượng SmartArt.
  • Chữ ký số và mã hóa: Thêm chữ ký số, bảo vệ bằng mật khẩu và mã hóa tài liệu.
  • Macro và Scripting: Lưu giữ và quản lý macro (VBA), cho phép thực hiện các tác vụ tự động hóa phức tạp.
  • Khả năng chuyển đổi: Chuyển đổi giữa các định dạng Word và nhiều loại tệp khác, như PDF và HTML.
  • Hỗ trợ đám mây: Tích hợp với Aspose Cloud để xử lý tài liệu trên nền tảng đám mây.
  • Hỗ trợ API toàn diện: Tài liệu API mở rộng, ví dụ và hỗ trợ cộng đồng.
GitHub

Thống kê GitHub

Tên: FileFormat.Words-for-.NET
Ngôn ngữ: C#
Ngôi sao: 29
Nĩa: 7
Giấy phép: MIT License
Kho lưu trữ được cập nhật lần cuối lúc 2025-03-18

Bắt đầu với FileFormat.Words cho API .NET

Bạn có thể tải xuống thư viện FileFormat.Words cho .NET từ GitHub hoặc Nugget.

Cài đặt

Việc cài đặt FileFormat.Words cho .NET rất đơn giản và có thể thực hiện từ nugget như minh họa bên dưới:

Cài đặt FileFormat.Words cho .NET API


NuGet\Install-Package FileFormat.Words

Ví dụ mã cho FileFormat.Words cho API .NET

FileFormat.Words for .NET là API mạnh mẽ cung cấp nhiều tính năng để làm việc với các tài liệu Word bao gồm các định dạng tệp DOC và DOCX. Nó cung cấp chức năng Xử lý văn bản phong phú như tạo tài liệu từ đầu, mở tài liệu hiện có và cập nhật nội dung của tài liệu đó, và chuyển đổi tài liệu sang các định dạng khác nhau như PDF. API có thể dễ dàng được sử dụng trong các ứng dụng C# và ASP.NET mà không cần phải đi sâu vào các chi tiết cơ bản của các định dạng tệp.

Làm thế nào để tạo tệp tài liệu Word DOCX trong .NET?

FileFormat.Words for .NET API cho phép bạn tạo một tài liệu DOCX từ đầu trong ứng dụng C# hoặc ASP.NET của bạn chỉ với một vài dòng mã. Bạn có thể sử dụng mã nguồn sau để tạo một tài liệu trống bằng 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);
}
}

Làm thế nào để đọc tệp DOCX hiện có trong C#?

FileFormat.Words cho .NET cho phép bạn đọc một tài liệu Word DOCX hiện có trong ứng dụng C# của bạn. Bạn có thể tải tài liệu trong ứng dụng của mình, sửa đổi nội dung của nó và sau đó lưu lại vào đĩa bằng API như được hiển thị trong mẫu mã bên dưới.

/// <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);
}
}

Làm thế nào để tạo bảng trong tài liệu Word ở .NET?

FileFormat.Words for .NET SDK giúp bạn thêm bảng vào tài liệu Word DOCX rất tiện lợi. Bạn có thể sử dụng API để tạo bảng có nhiều hàng và cột, và thêm nội dung vào tài liệu như trong mẫu mã bên dưới.

/// <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 cho Tài nguyên .NET

Phần kết luận

Sản Phẩm Tương Tự

 Vietnamese