跳過到頁腳內容
使用IRONPDF

如何使用 IronPDF 將 Word 轉換為 PDF ASP.NET

在現代 ASP.NET 應用程式中,無論是產生發票、建立報告還是處理辦公室文檔,將 Word 文檔轉換為 PDF 都是至關重要的要求。 與需要 Microsoft Office Interop 依賴項或複雜伺服器配置的傳統方法不同,IronPDF 提供了一個精簡的獨立解決方案,可在任何 .NET 環境中無縫運作。

本文介紹如何使用 IronPDF 強大的 DocxToPdfRenderer 以程式設計方式將 Word DOCX 檔案轉換為 PDF 文件。 無需安裝 Microsoft Word Office,同時保持文件的完美完整性。 我們將跟隨您的腳步,探索使用 IronPDF 將 Word 轉換為 PDF 的簡單步驟,以了解各種轉換方法,並提供範例程式碼。

如何在 ASP.NET 專案中安裝 IronPDF?

使用 IronPDF 只需要在 NuGet 套件管理器控制台中安裝一次。 在 Visual Studio 中開啟程式包管理器控制台並執行:

Install-Package IronPdf
Install-Package IronPdf
SHELL

安裝完成後,您需要將 IronPDF 命名空間新增至您的 C# 檔案。 此程式庫可與 .NET Core、.NET Framework 和最新的 .NET 版本搭配使用,使其適用於任何 ASP.NET 專案。 無需其他微軟辦公軟體元件或第三方轉換器。

如何透過程式設計方式將 Word 文件轉換為 PDF?

使用 IronPDF 將 Word 文件轉換為 PDF 文件的 ASP.NET 流程非常簡單。 以下是轉換 DOCX 檔案的基本方法:

using IronPdf;
// Instantiate the DocxToPdfRenderer
var renderer = new DocxToPdfRenderer();
// Convert the Word DOC to PDF
var pdf = renderer.RenderDocxAsPdf(@"C:\Documents\report.docx");
// Save the PDF file
pdf.SaveAs(@"C:\Documents\report.pdf");
using IronPdf;
// Instantiate the DocxToPdfRenderer
var renderer = new DocxToPdfRenderer();
// Convert the Word DOC to PDF
var pdf = renderer.RenderDocxAsPdf(@"C:\Documents\report.docx");
// Save the PDF file
pdf.SaveAs(@"C:\Documents\report.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸入的 Word 文件已轉換為 PDF 文件

如何使用 IronPDF 將 Word 文件轉換為 PDF(ASP.NET):圖 1 - 輸入 Word 文件與輸出 PDF 文件比較

在這個例子中,DocxToPdfRenderer 類別在內部處理所有複雜的轉換邏輯。 它在將 Word 文件轉換為 PDF 的過程中,保留文件中的格式、圖像、表格和樣式。 RenderDocxAsPdf 方法接受檔案路徑或位元組數組,讓您可以靈活地載入 DOCX 檔案。

對於更高級的應用程式場景,您也可以使用串流:

using var docxStream = new FileStream("document.docx", FileMode.Open);
var renderer = new DocxToPdfRenderer();
var pdfDocument = renderer.RenderDocxAsPdf(docxStream);
using var docxStream = new FileStream("document.docx", FileMode.Open);
var renderer = new DocxToPdfRenderer();
var pdfDocument = renderer.RenderDocxAsPdf(docxStream);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

當處理以二進位資料形式儲存在資料庫中的上傳文件或文件時,這種方法尤其有用。

如何有效率地處理多個 DOCX 檔案?

當您需要批次轉換 Word 檔案時,IronPDF 可簡化批次處理流程。 以下是如何將多個 DOCX 文件轉換為 PDF 文件的方法:

var renderer = new DocxToPdfRenderer();
string[] docxFiles = Directory.GetFiles(@"C:\WordDocuments", "*.docx");
foreach (string docxFile in docxFiles)
{
    var pdf = renderer.RenderDocxAsPdf(docxFile);
    string pdfPath = Path.ChangeExtension(docxFile, ".pdf");
    pdf.SaveAs(pdfPath);
}
var renderer = new DocxToPdfRenderer();
string[] docxFiles = Directory.GetFiles(@"C:\WordDocuments", "*.docx");
foreach (string docxFile in docxFiles)
{
    var pdf = renderer.RenderDocxAsPdf(docxFile);
    string pdfPath = Path.ChangeExtension(docxFile, ".pdf");
    pdf.SaveAs(pdfPath);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出檔案

如何使用 IronPDF 將 Word 文件轉換為 PDF(ASP.NET):圖 2 - 指定目錄中的原始 Word 文件和渲染後的 PDF 文件

這段程式碼片段處理目錄中的所有 MS Word 文檔,保留原始文件名,同時將副檔名更改為 PDF。 DocxToPdfRenderer 實例可以在多次轉換中重複使用,從而提高效率。

如何使用郵件合併建立包含動態內容的PDF?

IronPDF 支援郵件合併功能,可從 Word 範本產生個人化 PDF 文件。 此功能可讓您透過將資料合併到 DOCX 檔案中的預先定義欄位中來建立具有動態內容的 PDF 檔案。 郵件合併功能對於產生發票、合約或個人化報告特別有用,因為在轉換為 PDF 格式之前,您需要使用特定資料填入範本欄位。

var renderer = new DocxToPdfRenderer();
renderer.MailMergeDataSource = yourDataSource;
var pdf = renderer.RenderDocxAsPdf("template.docx");
var renderer = new DocxToPdfRenderer();
renderer.MailMergeDataSource = yourDataSource;
var pdf = renderer.RenderDocxAsPdf("template.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

如何保護轉換後的PDF文件?

將 Word 文件轉換為 PDF 後,您可能需要新增安全功能來保護敏感文件。 IronPDF 讓您可以輕鬆地對 PDF 檔案套用密碼保護並設定權限:

var renderer = new DocxToPdfRenderer();
var pdf = renderer.RenderDocxAsPdf("confidential.docx");
// Add password protection
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.OwnerPassword = "owner456";
// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs("secured_document.pdf");
var renderer = new DocxToPdfRenderer();
var pdf = renderer.RenderDocxAsPdf("confidential.docx");
// Add password protection
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.OwnerPassword = "owner456";
// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs("secured_document.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

PDF 安全設定已套用

如何使用 IronPDF 將 Word 文件轉換為 PDF(ASP.NET):圖 3 - PDF 安全性設定

這些安全設定可確保您轉換後的文件始終受到保護,無論您處理的是財務報告、法律文件或其他機密辦公室文件。

如何在 ASP.NET Core 中將 DOCX 轉換為 PDF?

在 ASP.NET Core 應用程式中整合 Word 文件到 PDF 的轉換功能非常簡單。 以下是一個範例控制器操作,它將 DOCX 檔案轉換為可下載的 PDF 檔案並返回:

[HttpPost]
public IActionResult ConvertWordToPdf(IFormFile wordFile)
{
    if (wordFile != null && wordFile.Length > 0)
    {
        using var stream = new MemoryStream();
        wordFile.CopyTo(stream);
        var renderer = new DocxToPdfRenderer();
        var pdfDocument = renderer.RenderDocxAsPdf(stream.ToArray());
        var pdfBytes = pdfDocument.BinaryData;
        return File(pdfBytes, "application/pdf", "converted.pdf");
    }
    return BadRequest("Please upload a valid Word document");
}
[HttpPost]
public IActionResult ConvertWordToPdf(IFormFile wordFile)
{
    if (wordFile != null && wordFile.Length > 0)
    {
        using var stream = new MemoryStream();
        wordFile.CopyTo(stream);
        var renderer = new DocxToPdfRenderer();
        var pdfDocument = renderer.RenderDocxAsPdf(stream.ToArray());
        var pdfBytes = pdfDocument.BinaryData;
        return File(pdfBytes, "application/pdf", "converted.pdf");
    }
    return BadRequest("Please upload a valid Word document");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此實作方式處理文件上傳,在記憶體中執行轉換,並將產生的 PDF 串流回用戶,而不會在伺服器上建立臨時文件。 此方法在雲端環境中可以無縫運行,無需特殊的伺服器配置。

結論

IronPDF 將 Word 文件轉換為 PDF 的複雜任務轉換為簡單、易於管理的程式碼。 透過消除對 Microsoft Office Interop 的依賴並提供強大的 API,它使開發人員能夠在任何 .NET 環境中輕鬆地將 Word DOCX 檔案轉換為 PDF 格式。 無論您是處理單一文件還是處理批次轉換,IronPDF 的 DocxToPdfRenderer 都能提供一致、高品質的結果。

準備好在 ASP.NET 應用程式中實作 Word 轉 PDF 功能了嗎? 立即下載 IronPDF免費試用版,探索其強大的功能。 或者,購買用於生產用途的許可證。 造訪我們的完整文檔,以了解更多高級功能,例如 IronPDF 將 HTML 轉換為 PDF 的功能,以及 PDF 生成的最佳實踐。

常見問題解答

如何在 ASP.NET 中將 Word 文件轉換為 PDF?

您可以使用 IronPDF 的 DocxToPdfRenderer 在 ASP.NET 中將 Word 文件轉換為 PDF。它提供了一種簡單有效的方式,以程式化的方式處理文件轉換。

使用 IronPDF 進行 Word 至 PDF 的轉換有哪些優點?

IronPDF 提供獨立的解決方案,不需要 Microsoft Office Interop 的依賴,因此非常適合任何 .NET 環境。它簡化了 ASP.NET 應用程式中的轉換過程,並提升了效能。

使用 IronPDF 需要安裝 Microsoft Office 嗎?

不,使用 IronPDF 不需要安裝 Microsoft Office。它獨立運作,不需要額外的軟體依賴。

IronPDF 可以處理大規模的文件轉換嗎?

是的,IronPDF 旨在高效處理大規模的文件轉換,因此適用於在 ASP.NET 應用程式中產生發票或建立報表等情境。

IronPDF 兼容所有 .NET 環境嗎?

IronPDF 可與任何 .NET 環境相容,為開發現代 ASP.NET 應用程式的開發人員提供彈性與易整合性。

什麼是 IronPdf 中的 DocxToPdfRenderer?

DocxToPdfRenderer 是 IronPDF 中的一項功能,它允許開發人員在 C# 應用程式中以程式化方式將 Word 文件轉換為 PDF,簡化了文件處理工作流程。

IronPdf 是否需要複雜的伺服器設定?

不,IronPDF 不需要複雜的伺服器配置。它提供了一種簡化的方法,可以無縫整合到您現有的 ASP.NET 應用程式中。

IronPDF 如何改善 ASP.NET 中的文件處理?

IronPDF 通過提供直接可靠的解決方案將 Word 文件轉換為 PDF,提高了 ASP.NET 應用程式的效率和性能,從而改善了文件處理。

IronPDF 可以將哪些類型的文件轉換為 PDF?

IronPDF for .NET 可以將包括 Word 文件在內的多種文件轉換為 PDF 格式,支援 ASP.NET 應用程式中多樣化的文件處理需求。

為什麼選擇 IronPDF 而不是傳統的轉換方法?

與傳統方法相比,IronPDF 是首選,因為它不需要 Microsoft Office Interop、減少相依性問題,並能在 .NET 環境中提供更順暢、更有效率的轉換流程。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。