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的功能特點
- 強大的文件操控:Xceed.Document.NET讓開發者輕鬆建立、修改和格式化Word文件,得益於其全面的功能和直觀的API。
- 多樣化的內容管理:使用Xceed.Document.NET,開發者可以操作各種文件元素,例如段落、表格、圖片和樣式,建立動態且吸引人的文件。
- 豐富的格式選項:Xceed.Document.NET提供廣泛的格式選項,讓開發者能夠自定義文件外觀,應用字體、顏色、對齊等。
- 跨平台相容性:Xceed.Document.NET相容各種.NET平台,包括Windows Forms、WPF、ASP.NET和.NET Core,適合廣泛的應用。
- 無縫整合:Xceed.Document.NET與其他.NET簽名和框架無縫整合,使開發者能夠輕鬆提升其文件相關工作流。
建立C# Visual Studio 專案
-
打開Visual Studio並建立一個新的C#專案。

-
根據您的需求選擇合適的專案範本(例如,控制台應用程式、Windows Forms應用程式)。

-
指定專案名稱和位置,然後點擊"下一步"。

- 從附加資訊中選擇最新的.NET框架。 點擊"建立"以建立專案。
安裝過程
安裝Xceed.Document.NET是一個簡單的過程:
- 打開Visual Studio或您喜愛的IDE。
- 建立新的.NET專案或打開現有的.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整合
將Xceed.Document.NET與IronPDF整合可讓開發者無縫地將使用Xceed.Document.NET建立的Word文件(DOCX檔案)轉換為PDF格式。
IronPDF
IronPDF是一個強大的.NET程式庫,旨在簡化.NET應用程式中的PDF生成、操控和渲染任務。 憑借其全面的功能和直觀的API,IronPDF讓開發者能夠輕鬆以程式化方式建立、修改和渲染PDF檔案,無需安裝Adobe Acrobat Reader。
IronPDF是一個很棒的工具,能將網頁、URL和HTML轉換為完美再現原始來源的PDF。 這適用於生成如報告和發票等線上內容的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

IronPDF的一些主要功能包括:
- PDF生成:IronPDF讓開發者能從多種來源生成PDF文件,如HTML、圖片和XML資料。 這使得能夠根據具體需求生成動態且可定製的PDF。
- PDF操控:使用IronPDF,開發者可以通過新增、修改或刪除頁面、文字、圖片、註解和其他元素來操控現有的PDF文件。 這種靈活性使得開發者能夠根據需要編輯和定製PDF文件。
- PDF轉換:IronPDF促進了HTML、Word文件(DOCX)、圖片和其他文件格式至PDF格式的轉換。 這提供了不同文件型別間的無縫互操作性,簡化了.NET應用程式中處理PDF的過程。
- PDF渲染:IronPDF提供了高質量的PDF渲染能力,允許開發者使用可定制的檢視器組件在其.NET應用中顯示PDF文件。 這使得使用者能夠直接在應用介面內檢視PDF內容。
- 跨平台相容性:IronPDF提供豐富的.NET支持。 它與多種.NET平台相容,包括Windows Forms、WPF、ASP.NET和.NET Core。 它可在Windows、Linux和macOS上使用。 這確保了開發者無論目標平台是什麼都可以在廣泛應用中使用IronPDF。
- 性能優化:IronPDF進行了性能和可擴展性優化,確保即便是大型文件也能快速高效地生成和渲染PDF。 這讓開發者能夠建立回應快速且高性能的應用,有效地處理與PDF相關的任務。
- 授權選項:IronPDF提供靈活的授權選項,以適應單個開發者、小型團隊和企業級應用的需求。 這提供了與PDF相關任務的成本效益解決方案,允許開發者選擇最符合其需求的授權模式。
將Xceed.Document.NET與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都安裝完畢,您即可實現使用IronPDF將DOCX轉換為PDF的邏輯。
- 實例化
DocxToPdfRenderer類別自IronPDF。 - 使用
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文件,以保留原文件中的格式化文字。
輸出:DOCX至PDF轉換

如需更多IronPDF及其功能的資訊,請存取此IronPDF文件頁面。 探索現成可用的HTML轉PDF範例,並在您的.NET Framework控制台或網路應用中開始進行PDF操控。
結論
總之,Xceed.Document.NET和IronPDF提供了強大的文件生成和轉換解決方案,用於.NET應用程式。 通過利用Xceed.Document.NET的功能,開發者可以輕鬆建立動態且視覺上吸引人的Word文件。 與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將DOCX文件轉換為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提供靈活的授權選項,適合個人開發者、小型團隊和企業,還提供免費試用供評估使用。




