在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
將 Word 文件轉換為 PDF 是許多商業應用中的常見需求,從自動化報告生成到傳遞專業品質的文件。 PDFs因其一致的格式、安全的特性和易於共享而被普遍認可。
在本教程中,我們將指導您完成轉換 Word 文件的過程(DOCX 文件)使用 C# 將其轉換為 PDFIronPDF圖書館 本逐步指南將幫助您快速且無縫地將Word轉PDF功能整合到您的.NET應用程式中。
在開始本教程之前,請確保您擁有以下內容:
開發環境
Visual Studio 的正常安裝(建議使用 2019 年或更高版本).
IronPDF 程式庫
通過 NuGet 安裝 IronPDF
範例 Word 文件
基礎 C# 知識
由於以下原因,PDF 是文件共享和存檔的黃金標準:
以下是一些 Word 轉 PDF 轉換至關重要的實際情境:
要跟隨這個教學,你需要IronPDF函式庫。
IronPDF可透過 NuGet 獲得。 打開封裝管理器主控台並運行:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
或者,您可以通過 Visual Studio 中的 NuGet 套件管理器安裝它。 只需進入工具 > NuGet 套件管理員 > 管理方案的 NuGet 套件:
然後搜尋IronPDF。 在此,您可以將 IronPDF 函式庫安裝到您的專案中。
IronPDF 提供免費試用,您需要激活許可證密鑰以獲取全部功能。 要啟用,只需在您的程式碼中添加許可密鑰:
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
下面的範例程式碼展示了將 DOCX 文件轉換為 PDF 的簡單過程。
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Convert DOCX to PDF using IronPDF
PdfDocument pdf = renderer.RenderDocxAsPdf("newsletter.docx");
// Save the resulting PDF to a file
pdf.SaveAs("output.pdf");
}
}
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Convert DOCX to PDF using IronPDF
PdfDocument pdf = renderer.RenderDocxAsPdf("newsletter.docx");
// Save the resulting PDF to a file
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer As New DocxToPdfRenderer()
' Convert DOCX to PDF using IronPDF
Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("newsletter.docx")
' Save the resulting PDF to a file
pdf.SaveAs("output.pdf")
End Sub
End Class
為了開始將您的 DOCX 文件轉換為 PDF,我們首先需要實例化DocxToPdfRenderer. 這將處理將您的 Word 文件轉換為 PDF 格式。 接下來的步驟是創建一個新的PdfDocument 實例將保存新創建的 PDF 文檔,並使用 RenderDocxAsPdf 方法將 DOCX 文件轉換為 PDF。 最後,您只需使用SaveAs將 PDF 文件保存到所需位置,完成!! 您只需三行簡單的代碼就將 DOCX 文件轉換為 PDF。
IronPDF 擁有先進的功能,幫助您自訂和豐富您的 PDF 輸出:
水印對於品牌塑造或標記敏感文件非常有用。 您可以添加浮水印轉換 PDF 後:
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)
對 PDF 最常見的自訂之一是添加標頭給每一頁。 頁首可以包括文件標題、頁碼、日期或您希望在 PDF 每頁頂部顯示的任何其他信息。
TextHeaderFooter textHeader = new TextHeaderFooter
{
CenterText = "Weekly Newsletter!",
};
pdf.AddTextHeaders(textHeader);
TextHeaderFooter textHeader = new TextHeaderFooter
{
CenterText = "Weekly Newsletter!",
};
pdf.AddTextHeaders(textHeader);
Dim textHeader As New TextHeaderFooter With {.CenterText = "Weekly Newsletter!"}
pdf.AddTextHeaders(textHeader)
IronPDF 允許您定義頁面設定,例如邊距, 方向,和頁面大小保存 PDF 之前:
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.portrait;
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(20, 20);
renderer.RenderingOptions.MarginTop = 30;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 30;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.portrait;
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(20, 20);
renderer.RenderingOptions.MarginTop = 30;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 30;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.portrait
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(20, 20)
renderer.RenderingOptions.MarginTop = 30
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
renderer.RenderingOptions.MarginBottom = 30
using IronPdf;
using IronPdf.Rendering;
public class Program
{
public static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(15, 15);
renderer.RenderingOptions.MarginTop = 15;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 15;
// Convert DOCX to PDF using IronPDF
PdfDocument pdf = renderer.RenderDocxAsPdf("newsletter.docx");
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
TextHeaderFooter textHeader = new TextHeaderFooter
{
CenterText = "Weekly Newsletter!",
};
pdf.AddTextHeaders(textHeader);
// Save the resulting PDF to a file
pdf.SaveAs("output.pdf");
}
}
using IronPdf;
using IronPdf.Rendering;
public class Program
{
public static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(15, 15);
renderer.RenderingOptions.MarginTop = 15;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 15;
// Convert DOCX to PDF using IronPDF
PdfDocument pdf = renderer.RenderDocxAsPdf("newsletter.docx");
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
TextHeaderFooter textHeader = new TextHeaderFooter
{
CenterText = "Weekly Newsletter!",
};
pdf.AddTextHeaders(textHeader);
// Save the resulting PDF to a file
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Public Class Program
Public Shared Sub Main(ByVal args() As String)
Dim renderer As New DocxToPdfRenderer()
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait
renderer.RenderingOptions.SetCustomPaperSizeinCentimeters(15, 15)
renderer.RenderingOptions.MarginTop = 15
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
renderer.RenderingOptions.MarginBottom = 15
' Convert DOCX to PDF using IronPDF
Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("newsletter.docx")
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE Copy</h2>", 40, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center)
Dim textHeader As New TextHeaderFooter With {.CenterText = "Weekly Newsletter!"}
pdf.AddTextHeaders(textHeader)
' Save the resulting PDF to a file
pdf.SaveAs("output.pdf")
End Sub
End Class
使用此功能,您可以輕鬆地將 DOCX 文件轉換為 PDF 文件,並根據您的需求進行自訂格式。
減小 PDF 檔案大小由壓縮PDF 中的任何圖像,以加快下載速度並更順利地共享:
pdf.CompressImages(40);
pdf.CompressImages(40);
pdf.CompressImages(40)
在處理之前,請務必確保 DOCX 文件存在且有效:
if (!File.Exists(docxPath))
{
throw new FileNotFoundException($"File not found: {docxPath}");
}
if (!File.Exists(docxPath))
{
throw new FileNotFoundException($"File not found: {docxPath}");
}
If Not File.Exists(docxPath) Then
Throw New FileNotFoundException($"File not found: {docxPath}")
End If
使用密碼加密敏感文件:
pdf.SecuritySettings.OwnerPassword = "SecurePassword123";
pdf.SecuritySettings.UserPassword = "Password";
pdf.SecuritySettings.OwnerPassword = "SecurePassword123";
pdf.SecuritySettings.UserPassword = "Password";
pdf.SecuritySettings.OwnerPassword = "SecurePassword123"
pdf.SecuritySettings.UserPassword = "Password"
IronPDF 是一個強大的 .NET 程式庫,允許開發者輕鬆建立、操作和轉換 PDF 文件。 它提供了一系列功能,旨在幫助您在 C# 應用程式中自動化和簡化文件生成和處理。 無論您是將 HTML、DOCX 或圖像檔案轉換為 PDF,或是編輯和提取現有 PDF 文本,IronPDF 都能以最少的編碼簡化這些任務。
HTML 轉換為 PDF:
IronPDF 最強大的功能之一是其轉換能力HTML 內容轉換為 PDFs。 這使其非常適合用於網頁應用程式,使用者需要下載報告、發票或其他文件為 PDF 格式。
圖片轉PDF
PDF 編輯:
表單填寫與 PDF 生成:
文字提取
IronPDF使在C#中以編程方式將Word文件轉換為PDF變得簡單、可靠且功能豐富。 只需幾行代碼,就可以將此功能整合到您的 .NET 應用中,讓用戶能夠從 DOCX 文件生成專業、高品質的 PDF。
透過利用IronPDF,開發人員可以簡化以下工作流程:
建立重要 Word 文件的安全防篡改存檔。
此外,高級功能(如加水印、合併文件和應用自訂版面配置)讓您能夠超越基本轉換。 這些功能非常適合用於合同管理、發票生成和報告分發等領域建立穩健的解決方案。
IronPDF 以其簡單性、多功能性和對開發者友好的 API 脫穎而出,成為在 .NET 中進行文件處理的任何人的必備工具。 試試 IronPDF,自行體驗其功能免費試用,看看它如何改善您的 PDF 專案!