在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
尋找適用於 .NET Framework 的完美 PDF 工具?
在本文中,我們將探討如何在 C# 中處理 PDF 文件 (最受歡迎的程式語言之一)我們將討論兩個在.NET框架下的C#行業領先的PDF工具,並通過代碼示例來比較它們的性能和特性。最後,我們將檢查它們的授權細節並學習如何獲取授權密鑰。
這兩個PDF庫是:
IronPDF 是由 Iron Software 开发的 C# .NET 库,旨在为 C# .NET 开发者解决所有 PDF 问题,仅需一个库。IronPDF 由 Chromium 引擎驱动,具备多种有用且强大的功能,如将 HTML5、JavaScript、CSS 和图像文件转换为 PDF,创建自定义页眉和页脚,并精确显示 PDF,如同在浏览器中一样。IronPDF 允许您从 PDF 文件中读取数据,并将其自动插入到您的 .NET 应用程序和数据存储系统中。您可以将旧 PDF 存储中的内容导入、移动和索引到您的文档管理和业务应用程序中。
此功能使您可以從 HTML、MVC 視圖、Web 表單、HTML 字串和 URL 等多種格式生成 PDF 檔案。
在許多情況下,您可以輕鬆從 PDF 提取嵌入的文本。然而,如果這失敗了,您的文本很可能嵌入在圖像中。要掃描文檔中的視覺文本而不是純文本,請使用 IronOCR 圖書館
在創建 PDF 或現有的 PDF 上可以添加頁首和頁尾。使用 Print Options 屬性,您可以為每個文檔頁面創建頁首和頁尾。這些參數在 ChromePdfRenderer 對象上可用。此範例在 .NET Core 控制台應用程式中運行。
這讓使用者能夠從 PDF 生成照片,以及從照片生成 PDF。它支持圖像提取、各種圖像擴展以及 PDF 打印。
IronPDF 也包含 128 位元 PDF 加密、密碼保護的 PDF 鎖定和 PDF 的數位簽名。
IronPDF 工具允許您向 PDF 添加水印、添加頁面、刪除頁面、背景、前景等。
IronPDF 支援幾乎所有與 C# 相容的操作系統和框架,例如:
PDFNet .NET SDK by PDFTron 為任何網絡、移動、桌面或服務器上的框架或應用程式帶來精確的 PDF 處理、註釋、編輯和創建功能。它管理整個文檔和信息管理流程,無需第三方服務器依賴,全部在您的基礎建設內部實現。PDFTron 的網絡平台配備了支持平台的 JS 版本以及所有 PDF SDK 的演示許可密鑰。
PDFNet 最顯著的好處之一是它真正具有跨平台功能,允許我們的用戶簡單地將 PDF 相容性和相關的業務功能添加到他們的工作流程和應用程式中,無論是在不同的移動和桌面環境中使用,幾乎使用相同的接口。
PDFNet SDK 與許多平台和框架兼容。為了確保平台的一致性,JavaScript API 以與 PDFNet API 相同的方式使用。SDK 的 Node.js 和 Electron 版本也可用。此軟件包需要非受控外掛二進制文件。
為使用者在線上、行動和桌面平台上提供安全、可擴展和高保真度的PDF閱讀和編輯功能。
建立自訂的簽署程序。數位簽署的文件可以認證、驗證和封存。
從文件中刪除敏感數據,例如個人信息或個人健康信息,同時保持文件可搜索。
頁面可以新增、刪除或重新排列。文件可以組合、合併或分割為多個部分。在同一個閱讀器中或並排查看多個文件。
在本文中,我們將使用控制台應用程式來生成 PDF 文件。
開啟 Visual Studio 軟體並進入“文件”選單。選擇“新專案”,然後選擇控制台應用程式。
在適當的文本框中輸入專案名稱並選擇路徑。然後,點擊“建立”按鈕。選擇所需的 .NET 框架,如下圖所示:
Visual Studio 專案現在將生成控制台應用程式的結構。完成後,它將打開 program.cs 文件,您可以在其中編寫和執行源代碼。
現在我們可以添加庫並測試程序。
IronPDF 函式庫可以透過四種不同的方式下載和安裝。這些方式分別是:
Visual Studio 軟體提供 NuGet 套件管理器選項,可以將套件直接安裝到解決方案中。以下螢幕截圖顯示了如何打開 NuGet 套件管理器。
在打開的套件管理 GUI 中,點擊「瀏覽」標籤並搜索「IronPDF:」
在上面的圖片中,我們可以看到搜索結果中的相關套件列表。選擇IronPDF選項並將該套件安裝到我們的解決方案中。
Install-Package IronPdf
現在將在專案中安裝 IronPDF 套件。
第三種方式是直接從網頁下載 NuGet 套件。
點擊 這裡 從網頁直接下載最新套件。下載完成後,按照以下步驟將套件添加到專案中。
PDFNet SDK 可以通過四種不同的方式下載和安裝:
如前所述,前往 Visual Studio 的工具並開啟 NuGet 套件管理器。會出現一個列表——點擊管理解決方案的 NuGet 套件。
將會出現一個帶有搜尋欄的視窗。搜尋 PDFNet 並將其安裝到專案中。
Install-Package PDFNet ---Version 9.2.0
第三種方式是直接從網頁下載 NuGet 套件。
您也可以使用 PDFNetLoader.dll 文件安装 PDFNet API。從這裡下載: GitHub下載套件,將它放在專案的 bin 文件夾中,然後運行它。在您的 Visual Studio 專案中,將 PDFNetLoader.dll 添加為參考。
列印 PDF 檔案是現代應用程式中處理 PDF 時必備的功能。本文將探討如何使用兩種工具來列印 PDF 檔案。
IronPDF 透過將 PDF 發送到電腦的預設打印機來列印。這將打開 Windows 打印介面——只需點擊打印便可完成操作。
using IronPdf;
// Create a new PDF and print it
var Renderer = new IronPdf.ChromePdfRenderer();
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
// Send the PDF to the default printer to print
Pdf.Print(300, false);
using IronPdf;
// Create a new PDF and print it
var Renderer = new IronPdf.ChromePdfRenderer();
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
// Send the PDF to the default printer to print
Pdf.Print(300, false);
Imports IronPdf
' Create a new PDF and print it
Private Renderer = New IronPdf.ChromePdfRenderer()
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
' Send the PDF to the default printer to print
Pdf.Print(300, False)
PDFTron 的 PDFNet SDK 也提供打印功能。以下是使用 PDFNet 打印 PDF 檔案的源代码片段。
using System;
using System. Drawing;
using System.Drawing.Printing;
using pdftron;
using pdftron.PDF;
using pdftron.Common;
using pdftron.Filters;
namespace PDFPrintTestCS
{
Console.WriteLine("Opening the input file...");
using (pdfdoc = new PDFDoc(input_path + "tiger.pdf"))
{
pdfdoc.InitSecurityHandler();
Console.WriteLine("Printing the input file using PDF.Print.StartPrintJob...");
PrinterMode printerMode = new PrinterMode();
printerMode.SetAutoCenter(true);
printerMode.SetAutoRotate(true);
printerMode.SetCollation(true);
printerMode.SetCopyCount(1);
printerMode.SetDPI(300); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
printerMode.SetDuplexing(PrinterMode.DuplexMode.e_Duplex_Auto);
printerMode.SetNUp(PrinterMode.NUp.e_NUp_1_1, PrinterMode.NUpPageOrder.e_PageOrder_LeftToRightThenTopToBottom);
printerMode.SetOrientation(PrinterMode.Orientation.e_Orientation_Portrait);
printerMode.SetOutputAnnot(PrinterMode.PrintContentTypes.e_PrintContent_DocumentAndAnnotations);
printerMode.SetOutputColor(PrinterMode.OutputColor.e_OutputColor_Grayscale);
printerMode.SetOutputPageBorder(false);
printerMode.SetOutputQuality(PrinterMode.OutputQuality.e_OutputQuality_Medium);
printerMode.SetPaperSize(new Rect(0, 0, 612, 792));
PageSet pagesToPrint = new PageSet(1, pdfdoc.GetPageCount(), PageSet.Filter.e_all);
Print.StartPrintJob(pdfdoc, "", pdfdoc.GetFileName(), "", pagesToPrint, printerMode, null);
}
}
using System;
using System. Drawing;
using System.Drawing.Printing;
using pdftron;
using pdftron.PDF;
using pdftron.Common;
using pdftron.Filters;
namespace PDFPrintTestCS
{
Console.WriteLine("Opening the input file...");
using (pdfdoc = new PDFDoc(input_path + "tiger.pdf"))
{
pdfdoc.InitSecurityHandler();
Console.WriteLine("Printing the input file using PDF.Print.StartPrintJob...");
PrinterMode printerMode = new PrinterMode();
printerMode.SetAutoCenter(true);
printerMode.SetAutoRotate(true);
printerMode.SetCollation(true);
printerMode.SetCopyCount(1);
printerMode.SetDPI(300); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
printerMode.SetDuplexing(PrinterMode.DuplexMode.e_Duplex_Auto);
printerMode.SetNUp(PrinterMode.NUp.e_NUp_1_1, PrinterMode.NUpPageOrder.e_PageOrder_LeftToRightThenTopToBottom);
printerMode.SetOrientation(PrinterMode.Orientation.e_Orientation_Portrait);
printerMode.SetOutputAnnot(PrinterMode.PrintContentTypes.e_PrintContent_DocumentAndAnnotations);
printerMode.SetOutputColor(PrinterMode.OutputColor.e_OutputColor_Grayscale);
printerMode.SetOutputPageBorder(false);
printerMode.SetOutputQuality(PrinterMode.OutputQuality.e_OutputQuality_Medium);
printerMode.SetPaperSize(new Rect(0, 0, 612, 792));
PageSet pagesToPrint = new PageSet(1, pdfdoc.GetPageCount(), PageSet.Filter.e_all);
Print.StartPrintJob(pdfdoc, "", pdfdoc.GetFileName(), "", pagesToPrint, printerMode, null);
}
}
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports pdftron
Imports pdftron.PDF
Imports pdftron.Common
Imports pdftron.Filters
Namespace PDFPrintTestCS
Console.WriteLine("Opening the input file...")
pdfdoc = New PDFDoc(input_path & "tiger.pdf")
Using pdfdoc
pdfdoc.InitSecurityHandler()
Console.WriteLine("Printing the input file using PDF.Print.StartPrintJob...")
Dim printerMode As New PrinterMode()
printerMode.SetAutoCenter(True)
printerMode.SetAutoRotate(True)
printerMode.SetCollation(True)
printerMode.SetCopyCount(1)
printerMode.SetDPI(300) ' regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
printerMode.SetDuplexing(PrinterMode.DuplexMode.e_Duplex_Auto)
printerMode.SetNUp(PrinterMode.NUp.e_NUp_1_1, PrinterMode.NUpPageOrder.e_PageOrder_LeftToRightThenTopToBottom)
printerMode.SetOrientation(PrinterMode.Orientation.e_Orientation_Portrait)
printerMode.SetOutputAnnot(PrinterMode.PrintContentTypes.e_PrintContent_DocumentAndAnnotations)
printerMode.SetOutputColor(PrinterMode.OutputColor.e_OutputColor_Grayscale)
printerMode.SetOutputPageBorder(False)
printerMode.SetOutputQuality(PrinterMode.OutputQuality.e_OutputQuality_Medium)
printerMode.SetPaperSize(New Rect(0, 0, 612, 792))
Dim pagesToPrint As New PageSet(1, pdfdoc.GetPageCount(), PageSet.Filter.e_all)
Print.StartPrintJob(pdfdoc, "", pdfdoc.GetFileName(), "", pagesToPrint, printerMode, Nothing)
End Using
End Namespace
這兩個庫都具有 HTML 到 PDF 的轉換功能,但方法略有不同。
IronPDF 不需要使用 JSON 或加密機制。IronPDF 支援多種方法。例如:
using IronPdf;
var Renderer = new IronPdf.ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>").SaveAs("pixel-perfect.pdf");
using IronPdf;
var Renderer = new IronPdf.ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>").SaveAs("pixel-perfect.pdf");
Imports IronPdf
Private Renderer = New IronPdf.ChromePdfRenderer()
Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>").SaveAs("pixel-perfect.pdf")
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
var Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Pdf.SaveAs("url.pdf");
using IronPdf;
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
var Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Pdf.SaveAs("url.pdf");
Imports IronPdf
Private Renderer As New IronPdf.ChromePdfRenderer()
Private Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Pdf.SaveAs("url.pdf")
PDFNet 也提供使用頁面轉換和 URL 轉換的 HTML 到 PDF 轉換功能。由於代碼較長,我們這裡僅給出一個示例。
class HTML2PDFSample
{
private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
static HTML2PDFSample() {}
static void Main(string [] args)
{
string output_path = "../../../../TestFiles/Output/html2pdf_example";
string host = "https://www.pdftron.com";
string page0 = "/";
string page1 = "/support";
string page2 = "/blog";
HTML2PDF.SetModulePath("../../../../../Lib");
if (!HTML2PDF.IsModuleAvailable())
{
Console.WriteLine();
Console.WriteLine("Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.");
Console.WriteLine("-------------------------------");
Console.WriteLine("The HTML2PDF module is an optional add-on, available for download");
Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
Console.WriteLine("module, ensure that the SDK is able to find the required files");
Console.WriteLine("using the HTML2PDF.SetModulePath() function.");
Console.WriteLine();
return;
}
try
{
using (PDFDoc doc = new PDFDoc())
{
if ( HTML2PDF.Convert(doc, host + page0) )
doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized);
else
Console.WriteLine("Conversion failed.");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
}
class HTML2PDFSample
{
private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
static HTML2PDFSample() {}
static void Main(string [] args)
{
string output_path = "../../../../TestFiles/Output/html2pdf_example";
string host = "https://www.pdftron.com";
string page0 = "/";
string page1 = "/support";
string page2 = "/blog";
HTML2PDF.SetModulePath("../../../../../Lib");
if (!HTML2PDF.IsModuleAvailable())
{
Console.WriteLine();
Console.WriteLine("Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.");
Console.WriteLine("-------------------------------");
Console.WriteLine("The HTML2PDF module is an optional add-on, available for download");
Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
Console.WriteLine("module, ensure that the SDK is able to find the required files");
Console.WriteLine("using the HTML2PDF.SetModulePath() function.");
Console.WriteLine();
return;
}
try
{
using (PDFDoc doc = new PDFDoc())
{
if ( HTML2PDF.Convert(doc, host + page0) )
doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized);
else
Console.WriteLine("Conversion failed.");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
}
Friend Class HTML2PDFSample
Private Shared pdfNetLoader As pdftron.PDFNetLoader = pdftron.PDFNetLoader.Instance()
Shared Sub New()
End Sub
Shared Sub Main(ByVal args() As String)
Dim output_path As String = "../../../../TestFiles/Output/html2pdf_example"
Dim host As String = "https://www.pdftron.com"
Dim page0 As String = "/"
Dim page1 As String = "/support"
Dim page2 As String = "/blog"
HTML2PDF.SetModulePath("../../../../../Lib")
If Not HTML2PDF.IsModuleAvailable() Then
Console.WriteLine()
Console.WriteLine("Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.")
Console.WriteLine("-------------------------------")
Console.WriteLine("The HTML2PDF module is an optional add-on, available for download")
Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this")
Console.WriteLine("module, ensure that the SDK is able to find the required files")
Console.WriteLine("using the HTML2PDF.SetModulePath() function.")
Console.WriteLine()
Return
End If
Try
Using doc As New PDFDoc()
If HTML2PDF.Convert(doc, host & page0) Then
doc.Save(output_path & "_01.pdf", SDFDoc.SaveOptions.e_linearized)
Else
Console.WriteLine("Conversion failed.")
End If
End Using
Catch e As PDFNetException
Console.WriteLine(e.Message)
End Try
End Sub
End Class
IronPDF 是一個提供免費開發者授權的函式庫。IronPDF 還提供特殊的價格結構:輕量包從 $749 起步,沒有隱藏費用。此外,還可以重新分發 SaaS 和 OEM 產品。所有授權都包含30天的退款保證、一年的軟體支援和升級、開發/測試/預備/生產有效期,以及永久授權 (一次性購買). IronPDF 授權提供用戶所需的最佳效能。前往這個 連結 查看 IronPDF 的完整定價結構和授權。
PDFTron PDFNet SDK 提供多種定制於您業務需求的授權選項。三種類型的授權包含企業和 OEM 再發行。企業授權僅供組織使用,不能用於應用程式發行。授權中也包含需要檔案系統存取的額外 API。所有授權都附有授權金鑰。三種類型的授權是:
若需進一步了解他們的授權結構,請訪問此網站 連結.
IronPDF 提供了豐富的功能和功能來處理 PDF 文件,並將它們轉換為不同的格式。從 HTML 到 PDF 的轉換以矢量格式生成,適合高品質的商業印刷。因此,您將獲得清晰、高品質的 PDF。授權和定價信息可以在網站上找到。
與 IronPDF 相似,PDFNet 也幫助進行 PDF 文件的轉換、註釋、簽名、填寫、轉換和編輯。它支持多個平台,包括 Windows、IOS、Android、WEB 和 Linux。對於生產使用,PDFNet SDK 需要產品密鑰。
IronPDF 的授權以開發者為中心,這意味著您需要根據將使用該產品的開發者數量獲取授權。另一方面,PDFNet 授權的工作方式不同,它是基於功能的授權。這意味著您需要獲取支持所需功能的授權。IronPDF 授權包括所有功能。而對於 PDFNet,有些功能(如文件轉換)需要作為附加功能購買。
IronPDF 庫以有組織的方式提供功能,使得使用幾行代碼就能完成大型任務代碼的編寫變得容易。目前,Iron Software 提供五合一工具包,只需支付兩個的價格。包含在 IronSuite 中的工具有:
請訪問此鏈接以探索 IronSuite