Xceed.Document .NET(开发人员如何使用)
在.NET开发的强大环境中,文档生成和转换任务的无缝处理对于各种应用程序至关重要。 无论是生成动态报告、创建发票,还是在不同格式之间转换文档,拥有合适的工具可以显著简化工作流程并提高生产力。
在本文中,我们将探讨Xceed.Document.NET和IronPDF — 两个强大的.NET文档操作库 — 如何简化这些过程,并帮助开发人员创建多功能且高效的应用程序。
介绍Xceed.Document.NET
Xceed.Document.NET,由Xceed开发,是一个全面的.NET程序集,能以编程方式创建、操作和格式化Word文档(DOCX文件),无需安装Microsoft Word或Office InterOp。Xceed.Document.NET的设计注重简单性和多样性,为开发人员提供了一个直观的API,以无缝集成文档生成功能到其.NET应用程序中。

利用Xceed.Document.NET的强大功能,开发人员可以轻松操作Word文档的各种元素,包括段落、表格、图像和样式,以创建符合特定需求的丰富和可定制文档。 您还可以设置文档属性、进行数字签名,并确保MS Word文档的数字签名完整性。
Xceed提供了各种针对不同文档格式量身定制的库,例如文本文档、不同的文本格式、.NET文档到PDF等。
Xceed.Document.NET的特性
1.强大的文档操作: Xceed.Document.NET 凭借其全面的功能集和直观的 API,使开发人员能够轻松创建、修改和格式化 Word 文档。 2.多功能内容管理:借助 Xceed.Document.NET,开发人员可以操作各种文档元素,例如段落、表格、图像和样式,以创建动态且具有视觉吸引力的文档。 3.丰富的格式选项: Xceed.Document.NET 提供丰富的格式选项,允许开发人员通过应用字体、颜色、对齐方式等来自定义文档的外观。 4.跨平台兼容性: Xceed.Document.NET 与各种 .NET 平台兼容,包括 Windows Forms、WPF、ASP.NET 和 .NET Core,使其适用于各种应用程序。 5.无缝集成: Xceed.Document.NET 与其他 .NET 库和框架无缝集成,使开发人员能够轻松增强其与文档相关的工作流程。
创建C# Visual Studio项目
1.打开 Visual Studio 并创建一个新的 C# 项目。

2.根据您的要求选择合适的项目模板(例如,控制台应用程序、Windows 窗体应用程序)。

3.指定项目名称和位置,然后单击 "下一步"。

4.从 "附加信息 "中选择最新的 .NET Framework。 点击"创建"以创建项目。
安装过程
安装Xceed.Document.NET是一个简单明了的过程:
- 打开Visual Studio或您喜欢的IDE。
- 创建一个新的或打开已存在的.NET项目。
- 导航到NuGet包管理控制台。
-
运行以下命令以安装Xceed.Document.NET包:
Install-Package Xceed.Document.NET
- 安装完成后,即可在项目中使用Xceed.Document.NET。
注意:了解Xceed.Words.NET对于创建DOCX格式是必要的,利用Xceed.Document.NET来将元素纳入其中。
Install-Package Xceed.Words.NET

使用Xceed.Document.NET创建DOCX文件
使用 Xceed.Document.NET 创建 DOCX 文件简单且直观。以下是如何以编程方式创建 Word 文档的基本示例:
using Xceed.Words.NET;
using System;
class Program
{
static void Main(string[] args)
{
// Create a new document
using (DocX document = DocX.Create("SampleDocument.docx"))
{
// Add a paragraph with text, styling it with font size, boldness and centered alignment
document.InsertParagraph("Hello, World!")
.FontSize(12) // Set the font size
.Bold() // Make the text bold
.Alignment = Alignment.center; // Center align the text
// Save the document
document.Save();
Console.WriteLine("Document created successfully!");
}
}
}
using Xceed.Words.NET;
using System;
class Program
{
static void Main(string[] args)
{
// Create a new document
using (DocX document = DocX.Create("SampleDocument.docx"))
{
// Add a paragraph with text, styling it with font size, boldness and centered alignment
document.InsertParagraph("Hello, World!")
.FontSize(12) // Set the font size
.Bold() // Make the text bold
.Alignment = Alignment.center; // Center align the text
// Save the document
document.Save();
Console.WriteLine("Document created successfully!");
}
}
}
Imports Xceed.Words.NET
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a new document
Using document As DocX = DocX.Create("SampleDocument.docx")
' Add a paragraph with text, styling it with font size, boldness and centered alignment
document.InsertParagraph("Hello, World!").FontSize(12).Bold().Alignment = Alignment.center ' Center align the text
' Save the document
document.Save()
Console.WriteLine("Document created successfully!")
End Using
End Sub
End Class
以下是上述代码的输出。 创建的 DOCX 文件可以使用 Microsoft Office Word 应用程序打开。

将 Xceed.Document.NET 与 IronPDF 集成
IronPDF
IronPDF 是一个强大的 .NET 库,旨在简化 .NET 应用程序中的 PDF 生成、处理和渲染任务。 IronPDF 是将网页、URL 和 HTML 转换为 PDF 的绝佳工具,完美地再现原始源。 如果您需要从网页创建 PDF,IronPDF 是您的解决方案!
using IronPdf;
using System;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
Console.WriteLine("HTML string converted to PDF successfully.");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
Console.WriteLine("HTML file converted to PDF successfully.");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
Console.WriteLine("URL converted to PDF successfully.");
}
}
using IronPdf;
using System;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
Console.WriteLine("HTML string converted to PDF successfully.");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
Console.WriteLine("HTML file converted to PDF successfully.");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
Console.WriteLine("URL converted to PDF successfully.");
}
}
Imports IronPdf
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer = New ChromePdfRenderer()
' 1. Convert HTML String to PDF
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")
Console.WriteLine("HTML string converted to PDF successfully.")
' 2. Convert HTML File to PDF
Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")
Console.WriteLine("HTML file converted to PDF successfully.")
' 3. Convert URL to PDF
Dim url = "http://ironpdf.com" ' Specify the URL
Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
pdfFromUrl.SaveAs("URLToPDF.pdf")
Console.WriteLine("URL converted to PDF successfully.")
End Sub
End Class

- PDF 生成: IronPDF 允许开发人员从各种来源(例如 HTML、图像和 XML 数据)创建 PDF 文档。
- PDF 操作:借助 IronPDF,开发人员可以通过添加、修改或删除页面、文本、图像、注释和其他元素来操作现有的 PDF 文档。
- PDF 转换: IronPDF 可将 HTML、Word 文档 (DOCX)、图像和其他文件格式转换为 PDF 格式。
- PDF 渲染: IronPDF 提供高质量的 PDF 渲染功能,允许开发人员使用可自定义的查看器组件在其 .NET 应用程序中显示 PDF 文档。
- 跨平台兼容性: IronPDF 提供广泛的 .NET 支持,兼容 Windows Forms、WPF、ASP.NET 和 .NET Core,可在 Windows、Linux 和 macOS 上运行。
- 性能优化: IronPDF 针对性能和可扩展性进行了优化,即使对于大型文档,也能确保快速高效地生成和渲染 PDF。
- 许可选项: IronPDF 提供灵活的许可选项,以满足个人开发者、小型团队和企业级应用程序的需求。
将 Xceed.Document.NET 与 IronPDF 库集成的步骤
1. 安装 IronPDF
- 打开 Visual Studio 或您喜欢的 IDE。
- 导航到NuGet包管理控制台。
-
运行以下命令以安装 IronPDF 包:
Install-Package IronPdf - 或者,您也可以通过解决方案的NuGet包管理器安装。
-
从NuGet浏览选项卡选择IronPDF并点击安装。

2. 使用 IronPDF 实现 DOCX 到 PDF 的转换逻辑
- 一旦安装了 Xceed.Document.NET 和 IronPDF,您就可以实现将 DOCX 转换为 PDF 的逻辑,使用 IronPDF。
- 从 IronPDF 实例化
DocxToPdfRenderer类。 - 使用
RenderDocxAsPdf方法将使用 Xceed.Document.NET 生成的 DOCX 文件渲染为 PDF 文档。 - 使用 IronPDF 保存 PDF 文档。
以下是演示如何使用 IronPDF 将 DOCX 文件转换为 PDF 的示例代码片段:
using IronPdf;
using System;
class Program
{
static void Main(string[] args)
{
// Instantiate the DOCX to PDF renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Render from a DOCX file to produce a PDF document
PdfDocument pdf = renderer.RenderDocxAsPdf("SampleDocument.docx");
// Save the PDF document
pdf.SaveAs("SampleDocument.pdf");
Console.WriteLine("DOCX file converted to PDF successfully.");
}
}
using IronPdf;
using System;
class Program
{
static void Main(string[] args)
{
// Instantiate the DOCX to PDF renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Render from a DOCX file to produce a PDF document
PdfDocument pdf = renderer.RenderDocxAsPdf("SampleDocument.docx");
// Save the PDF document
pdf.SaveAs("SampleDocument.pdf");
Console.WriteLine("DOCX file converted to PDF successfully.");
}
}
Imports IronPdf
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Instantiate the DOCX to PDF renderer
Dim renderer As New DocxToPdfRenderer()
' Render from a DOCX file to produce a PDF document
Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("SampleDocument.docx")
' Save the PDF document
pdf.SaveAs("SampleDocument.pdf")
Console.WriteLine("DOCX file converted to PDF successfully.")
End Sub
End Class
输出:DOCX到PDF转换

有关IronPDF及其功能的更多信息,请访问IronPDF文档页面。 探索现成的HTML到PDF示例,并开始在您的.NET Framework控制台或Web应用程序中进行PDF操作。
结论
总之,Xceed.Document.NET 和 IronPDF 为 .NET 应用程序中的文档生成和转换提供了强大的解决方案。 通过与IronPDF的无缝集成,开发人员可以轻松将这些Word文档转换为PDF格式,提高其应用程序的多功能性和可访问性。 无论是创建报告、生成发票,还是在格式之间转换文档,Xceed.Document.NET和IronPDF帮助开发人员简化文档相关工作流,并在.NET应用程序中提供出色的结果。
Iron Software推出的IronPDF提供免费试用,且在商业项目中部署时必需。 从IronPDF下载页面下载库并试用。
常见问题解答
如何在 .NET 中将 HTML 转换为 PDF?
在 .NET 中,您可以使用 IronPDF 将 HTML 转换为 PDF。该库提供了 RenderHtmlAsPdf 方法用于转换 HTML 字符串,以及 RenderHtmlFileAsPdf 方法用于 HTML 文件。
使用 Xceed.Document.NET 进行 Word 文档操作的优点是什么?
Xceed.Document.NET 提供优点,如强大的文档操作、多功能的内容管理、丰富的格式选项以及无需 Microsoft Word 的无缝整合其他 .NET 库。
我如何在 C# 项目中设置 Xceed.Document.NET?
要在 C# 项目中设置 Xceed.Document.NET,打开 Visual Studio,导航到 NuGet 包管理器控制台,并运行命令 Install-Package Xceed.Document.NET。
在 .NET 中将 DOCX 文件转换为 PDF 的过程是什么?
要在 .NET 中将 DOCX 文件转换为 PDF,首先使用 Xceed.Document.NET 创建或操作 DOCX,然后使用 IronPDF 将其转换为 PDF 格式。
PDF 库支持哪些 .NET 平台?
PDF 库支持多个 .NET 平台,包括 Windows Forms、WPF、ASP.NET 和 .NET Core,并兼容 Windows、Linux 和 macOS 环境。
IronPDF 如何处理图像到 PDF 的转换?
IronPDF 可以使用 RenderImageAsPdf 等方法将图像转换为 PDF,允许在保持高性能和质量的同时将图像集成到 PDF 文档中。
Xceed.Document.NET 和 IronPDF 能否集成到 .NET 应用程序中以增强文档工作流程?
是的,将 Xceed.Document.NET 和 IronPDF 集成到 .NET 应用程序中可以显著增强文档工作流程,简化文档生成、操作和格式转换等任务。
IronPDF 用于 PDF 生成和操作的关键功能是什么?
IronPDF 的功能包括从 HTML、图像或 XML 数据生成 PDF,转换 DOCX 为 PDF,以及跨平台执行高性能渲染和操作任务。
IronPDF 提供哪些许可选项?
是的,IronPDF 提供灵活的许可选项,适合个体开发者、小团队和企业,并提供免费试用以便评估。




