產品比較

ActivePDF DocConverter 教程及與 IronPDF 的比較

Chipego
奇佩戈·卡林达
2021年9月23日
分享:

ActivePDF Toolkit 是一個軟體元件,用於處理 PDF 檔案(包括從不同來源產生 PDF 檔案)並設置其屬性(例如頁首、頁尾、邊距或浮水印)。

IronPDF 是一個具競爭力定價的C# PDF 函式庫,還提供這些功能。

在這裡,我們將介紹如何在 .NET Visual Studio 專案中使用這兩個軟體元件的功能、程式碼範例和逐步操作,讓您能夠自行決定哪個最適合您的應用程式。


概述

關於 IronPDF C# 庫

Iron Software 是一家市場領先的元件提供商,提供 IronPDF 以用於處理 PDF 文件。 一種全面的方法,可以輕鬆從不同格式生成 PDF 文件並以程式設計方式設置所有屬性。 由於只需幾行程式碼即可生成一致、可靠和準確的 PDF 文件,因此受到開發人員的青睞。

IronPDF 專為 C#、.NET、VB、ASPX、ASP.NET、MVC 和 .NET Core 設計。 它支援 Visual Studio、NuGet、Linux、Azure、Docker 等。

關於ActivePDF Toolkit

ActivePDF 是一家提供多種組件來處理 PDF 文件的軟體公司。 與單一組件IronPDF不同,ActivePDF提供不同的PDF文件解決方案。 例如,要減小 PDF 文件的大小,您可以使用ActivePDF Compressor。 要從 HTML 資源創建 PDF,請使用ActivePDF WebGrabber

在本文中,我們將使用ActivePDF WebGrabber來與IronPDF進行比較,一起來看看吧:

ActivePDF WebGrabber 用於創建PDF

ActivePDF WebGrabber 是 ActivePDF 的一個獨立組件,專門用於從 HTML 資源(如 URL、HTML 文件或 HTML 字串)生成 PDF 文件。 它還提供設置頁面屬性功能,例如頁首、頁尾、邊距、水印或書籤,以根據我們的需求創建 PDF 文件。


比較

1. ActivePDF 與 IronPDF 比較表

讓我們來看看兩個組件的對比。

IronPDFActivePDF
IronPDF converts HTML sources to PDF files.ActivePDF converts HTML sources to PDF files.
IronPDF supports .NET Core.ActivePDF does not support .NET Core.
IronPDF supports .NET 4.0 or higher.ActivePDF supports .NET 4.6.2 or higher.
IronPDF supports macOS.ActivePDF does not support macOS.
IronPDF can apply CSS to set WaterMark properties.ActivePDF does not support CSS to set WaterMark properties.
IronPDF can set Paper Orientation of PDF files.ActivePDF can set Paper Orientation of PDF files.
IronPDF provides the RenderDelay function to delay the PDF conversion.ActivePDF provides the TimeoutSpan function to delay the PDF conversion.
IronPDF provides predefined functions to set Header or Footer.ActivePDF requires setting Header and Footer by raw HTML and CSS.
IronPDF provides a predefined function to draw a horizontal line to separate content.ActivePDF does not provide a line to separate headers and footers.
To save the PDF file, we can set the directory and file name in one line.We have to set file directory and file name separately.
Need to write fewer lines of code with a simple programming structure.Need to write many lines of code.
License starts from $749.License starts from $1180.

步驟 1:安裝

2. 如何安裝 IronPDF

您可以透過兩種方式將 IronPDF 程式庫加入專案,採用哪一種方式都沒有差別。

NuGet 套件管理器

  • 在您的 Visual Studio 專案中打開 NuGet 套件管理器。
  • 瀏覽 IronPDF,然後安裝它。

    或者:

  • 前往 tools
  • 選擇封裝管理器主控台
  • 運行以下命令:
Install-Package IronPdf

手動下載 IronPDF.dll

我們也可以下載 IronPDF.dll,然後在項目中添加其引用。

如果您可以通過撰寫using IronPdf;命名空間來訪問IronPDF,這意味著IronPDF已成功導入到您的專案中,並準備好使用。


如何安裝WebGrabber

下載 WebGrabber-install.exe,然後選擇下載文件。下載完成後,雙擊下載的文件。然後從 ActivePDF 請求激活密鑰以使用以下15天評估密鑰:001-AEALX-LC6Z5-7YD95-S3D8J-3LR25。

安裝成功後,前往以下目錄:

C:\Program Files\ActivePDF\WebGrabber\bin\

在此目錄中,您會獲得APWebGrabber.Net45.dll文件。在您的 Visual Studio 專案中添加它的引用。

現在,如果您可以通過撰寫using APWebGrabber;命名空間來訪問WebGrabber,這表示ActivePDF WebGrabber已成功導入到您的專案中,您可以使用它。

ActivePDF文檔提供了有關ActivePDF WebGrabber安裝的更多資訊。


教學課程

使用 IronPDF 和 WebGrabber

我們已經介紹了這兩個組件及其安裝過程,現在我們將開始通過執行不同任務來比較它們。 這將讓我們了解兩者的程式結構,並決定哪一個最適合我們的專案。 為了更好地理解,我們將在每個任務中執行一個具體的用例並提供用來實現的程式碼。


3. 將 HTML 字串轉換為 PDF 文件

在第一個比較中,我們將使用一個情境,該情境需要通過 HTML 字串創建一個 PDF 文件並將其保存到目標位置。 首先,我們開始使用IronPDF來實現這個使用案例:

3.1. 使用 IronPDF 的 HTML 字符串

/**
HTML String to PDF
anchor-html-string-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //HTML Source
    string html = "<h1>Hello World!</h1> <h2>Welcome to IronPDF</h2> ";
    //convert HTML string to PDF file
    using var PDF = converter.RenderHtmlAsPdf(html);
    //Save the file
    PDF.SaveAs("E:/sample.pdf");
}
/**
HTML String to PDF
anchor-html-string-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //HTML Source
    string html = "<h1>Hello World!</h1> <h2>Welcome to IronPDF</h2> ";
    //convert HTML string to PDF file
    using var PDF = converter.RenderHtmlAsPdf(html);
    //Save the file
    PDF.SaveAs("E:/sample.pdf");
}
'''
'''HTML String to PDF
'''anchor-html-string-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create rendering converter
	Dim converter = New ChromePdfRenderer()
	'HTML Source
	Dim html As String = "<h1>Hello World!</h1> <h2>Welcome to IronPDF</h2> "
	'convert HTML string to PDF file
	Dim PDF = converter.RenderHtmlAsPdf(html)
	'Save the file
	PDF.SaveAs("E:/sample.pdf")
End Sub
$vbLabelText   $csharpLabel

輸出:

上述程式碼將在Local Disk E:中建立一個名為sample.pdf的 PDF 檔案,其截圖如下:

Iron1 related to 3.1. 使用 IronPDF 的 HTML 字符串

3.2. 使用ActivePDF的HTML字符串

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //HTML Source
    string html = "<h1>Hello World!</h1> <h2>Welcome to ActivePDF WebGrabber</h2>";
    //assign source html to WebGrabber
    wg.CreateFromHTMLText = html;
    //specify file directory
    wg.OutputDirectory = "E:/";
    // file name
    wg.NewDocumentName = "sample.pdf";
    //convert source HTML to PDF file
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //HTML Source
    string html = "<h1>Hello World!</h1> <h2>Welcome to ActivePDF WebGrabber</h2>";
    //assign source html to WebGrabber
    wg.CreateFromHTMLText = html;
    //specify file directory
    wg.OutputDirectory = "E:/";
    // file name
    wg.NewDocumentName = "sample.pdf";
    //convert source HTML to PDF file
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
	Dim wg As New WebGrabber()
	'HTML Source
	Dim html As String = "<h1>Hello World!</h1> <h2>Welcome to ActivePDF WebGrabber</h2>"
	'assign source html to WebGrabber
	wg.CreateFromHTMLText = html
	'specify file directory
	wg.OutputDirectory = "E:/"
	' file name
	wg.NewDocumentName = "sample.pdf"
	'convert source HTML to PDF file
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下螢幕截圖是此代碼新生成的sample.pdf文件:

Active1 related to 3.2. 使用ActivePDF的HTML字符串

3.3. IronPDF 與 ActivePDF 的區別

  • 使用 IronPDF 減少代碼行數
  • IronPDF 生成的文件因預設邊距而更加易讀。

4. 將 HTML 文件轉換為 PDF 文件

在此比較中,我們使用的案例是需要從名為myHtmlFile.html的HTML文件生成PDF文件,該文件位於E:/目錄中,並且具有以下HTML和CSS代碼:

<html>
  <style>
        li{
            font-size:x-large;
            color: magenta;
            font-style: italic;
            }
  </style>
<body>
    <h1>I am Heading</h1>
    <h2>Items List:</h2>
    <ul>
        <li>Item1</li>
        <li>Item2</li>
        <li>Item3</li>
        <li>Item4</li>
    </ul>
</body>
</html>
<html>
  <style>
        li{
            font-size:x-large;
            color: magenta;
            font-style: italic;
            }
  </style>
<body>
    <h1>I am Heading</h1>
    <h2>Items List:</h2>
    <ul>
        <li>Item1</li>
        <li>Item2</li>
        <li>Item3</li>
        <li>Item4</li>
    </ul>
</body>
</html>
HTML

現在,我們將使用這兩個組件將 myHtmlFile.html 文件轉換為 PDF 文件。 讓我們從IronPDF開始。

4.1. 使用 IronPDF 的 HTML 檔案

/**
HTML File to PDF
anchor-html-file-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new IronPdf.ChromePdfRenderer();
    //render html file to pdf
    using var PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html");
    //save to target location
    PDF.SaveAs("E:/Sample.pdf");
}
/**
HTML File to PDF
anchor-html-file-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new IronPdf.ChromePdfRenderer();
    //render html file to pdf
    using var PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html");
    //save to target location
    PDF.SaveAs("E:/Sample.pdf");
}
'''
'''HTML File to PDF
'''anchor-html-file-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create rendering converter
	Dim converter = New IronPdf.ChromePdfRenderer()
	'render html file to pdf
	Dim PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html")
	'save to target location
	PDF.SaveAs("E:/Sample.pdf")
End Sub
$vbLabelText   $csharpLabel

以下螢幕截圖是使用上述代碼生成的新Sample.pdf文件:

Iron2 related to 4.1. 使用 IronPDF 的 HTML 檔案

我們可以看到 HTML 頁面 myHtmlFile.html 成功轉換為 PDF 文件 Sample.pdf,並且 CSS 樣式也已應用。

閱讀 IronPDF 文件 以了解更多關於如何在我們的 .NET 專案中使用 IronPDF。

讓我們使用 ActivePDF WebGrabber 執行相同的任務。

4.2. 使用ActivePDF的HTML文件

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //specify file path to be converted
    wg.URL = "E:/myHtmlFile.html";
    //specify the directory for newly generated file
    wg.OutputDirectory = "E:/";
    //newly generated file name
    wg.NewDocumentName = "Sample.pdf";
    //convert HTML file to PDF
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //specify file path to be converted
    wg.URL = "E:/myHtmlFile.html";
    //specify the directory for newly generated file
    wg.OutputDirectory = "E:/";
    //newly generated file name
    wg.NewDocumentName = "Sample.pdf";
    //convert HTML file to PDF
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
	Dim wg As New WebGrabber()
	'specify file path to be converted
	wg.URL = "E:/myHtmlFile.html"
	'specify the directory for newly generated file
	wg.OutputDirectory = "E:/"
	'newly generated file name
	wg.NewDocumentName = "Sample.pdf"
	'convert HTML file to PDF
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下截圖是使用上述代碼新生成的Sample.pdf文件:

Active2 related to 4.2. 使用ActivePDF的HTML文件

4.3. IronPDF 與 ActivePDF 之間的差異

  • 僅需 3 行代碼即可使用 IronPDF
  • IronPDF 文件更整潔/更吸引人。

5. 將 URL 轉換為 PDF 文件

假設我們有一個網址 https://yandex.com/ 且我們想要生成該網頁的PDF文件。 為此,兩個組件都提供了一個功能。 首先,我們將看看如何通過IronPDF來完成這項工作。

5.1. 使用 IronPDF 的 URL

/**
URL to PDF
anchor-url-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //Specify URL
    using var PDF = converter.RenderUrlAsPdf("https://yandex.com/");
    //Save the file
    PDF.SaveAs("E:/Sample.pdf");
}
/**
URL to PDF
anchor-url-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //Specify URL
    using var PDF = converter.RenderUrlAsPdf("https://yandex.com/");
    //Save the file
    PDF.SaveAs("E:/Sample.pdf");
}
'''
'''URL to PDF
'''anchor-url-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create rendering converter
	Dim converter = New ChromePdfRenderer()
	'Specify URL
	Dim PDF = converter.RenderUrlAsPdf("https://yandex.com/")
	'Save the file
	PDF.SaveAs("E:/Sample.pdf")
End Sub
$vbLabelText   $csharpLabel

以下截圖是由上述程式碼新生成的Sample.pdf文件

Iron3 related to 5.1. 使用 IronPDF 的 URL

您可以造訪URL 範例的網頁,來比較和查看 IronPDF 文件的匹配準確度。

現在,我們將使用 ActivePDF WebGrabber 執行相同的任務。

5.2. 使用ActivePDF的URL

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //specify URL 
    wg.URL = "https://yandex.com/";
    //specify the directory for newly generated file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert specified URL webpage to PDF
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //specify URL 
    wg.URL = "https://yandex.com/";
    //specify the directory for newly generated file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert specified URL webpage to PDF
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
	Dim wg As New WebGrabber()
	'specify URL 
	wg.URL = "https://yandex.com/"
	'specify the directory for newly generated file
	wg.OutputDirectory = "E:/"
	'specify file name
	wg.NewDocumentName = "Sample.pdf"
	'convert specified URL webpage to PDF
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下截圖顯示了由上述代碼新生成的Sample.pdf文件:

Active3 related to 5.2. 使用ActivePDF的URL

5.3. IronPDF 與 ActivePDF 的區別

  • IronPDF 擁有更簡單的結構來生成 PDF 文件。
  • 僅需三行代碼
  • IronPDF 更貼近網站匹配

6. 在 PDF 上創建浮水印

在此比較中,我們將使用 HTML 字串創建 PDF 文件,然後在頁面中央添加水印。 讓我們從IronPDF開始。

6.1. 使用 IronPDF 添加水印

IronPDF 提供以下功能以添加水印:

WatermarkPage(WaterMark HTML 字串, PageIndexToWaterMark, WaterMarkLocation, Opacity, Rotation, Hyperlink)

我們可以使用WaterMarkLocation來在以下位置設置水印:

  • 左上角

  • 頂部中心

  • 右上角

  • 中左

  • 中間偏中

  • 中右

  • BottomLeft

  • 底部中心

  • 右下角

    讓我們看看如何使用上述功能來設置水印:

/**
Watermark PDF
anchor-watermark-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //source html string
    string html = "<h1 style='text-align:center'>WaterMark Example</h1>";
    //add above string as PDF file content
    using var PDF = converter.RenderHtmlAsPdf(html);
    //HTML string for WaterMark
    string WMStr = "<h1 style='color:red'>WaterMark</h1>";
    //add WaterMark
    PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter, 100, -45, "");
    //save the document
    PDF.SaveAs("E:/Sample.pdf");
}
/**
Watermark PDF
anchor-watermark-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create rendering converter
    var converter = new ChromePdfRenderer();
    //source html string
    string html = "<h1 style='text-align:center'>WaterMark Example</h1>";
    //add above string as PDF file content
    using var PDF = converter.RenderHtmlAsPdf(html);
    //HTML string for WaterMark
    string WMStr = "<h1 style='color:red'>WaterMark</h1>";
    //add WaterMark
    PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter, 100, -45, "");
    //save the document
    PDF.SaveAs("E:/Sample.pdf");
}
'''
'''Watermark PDF
'''anchor-watermark-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create rendering converter
	Dim converter = New ChromePdfRenderer()
	'source html string
	Dim html As String = "<h1 style='text-align:center'>WaterMark Example</h1>"
	'add above string as PDF file content
	Dim PDF = converter.RenderHtmlAsPdf(html)
	'HTML string for WaterMark
	Dim WMStr As String = "<h1 style='color:red'>WaterMark</h1>"
	'add WaterMark
	PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter, 100, -45, "")
	'save the document
	PDF.SaveAs("E:/Sample.pdf")
End Sub
$vbLabelText   $csharpLabel

以下截圖顯示了由上述代碼新生成的Sample.pdf文件:

Iron4 related to 6.1. 使用 IronPDF 添加水印

我們可以添加任何類型的浮水印,並通過 CSS 設置其屬性。 現在,我們將使用 ActivePDF WebGrabber 執行相同的任務。

6.2. 使用 ActivePDF 水印

ActivePDF WebGrabber 不像 IronPDF 提供專門的浮水印功能。 但我們可以使用AddStampText()函數作為解決此問題的替代方法:

AddStampText(float x, float y, string stampText);

  • float x 用於設定新 TextStamp 起始點的 x 座標。
  • float y 設定新 TextStamp 原點的 y 座標。
  • stampText 是 TextStamp 的實際文本。

    注意:ActivePDF WebGrabber 不支援 TextStamp 的 CSS 樣式。我們必須透過其他提供的函數設置,如下所示:

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //HTML source for Page content
    string html = "<h1 style='text-align:center'>WaterMark Example</h1>";
    //assign page content source
    wg.CreateFromHTMLText = html;
    //add text stamp as WaterMark
    wg.AddStampText(270.0f, 350.0f, "WaterMark");
    //specify WaterMark's font size
    wg.StampFontSize = 20;
    //specify WaterMark's font family
    wg.StampFont = "Times New Roman";
    //specify WaterMark's opacity
    wg.StampFontTransparency = 1f;
    //specify WaterMark's rotation
    wg.StampRotation = 45.0f;
    //specify WaterMark's color
    wg.StampColorNET = new ADK.PDF.Color() { Red = 255, Green = 0, Blue = 0, Gray = 0 };
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert above sources to PDF file
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //HTML source for Page content
    string html = "<h1 style='text-align:center'>WaterMark Example</h1>";
    //assign page content source
    wg.CreateFromHTMLText = html;
    //add text stamp as WaterMark
    wg.AddStampText(270.0f, 350.0f, "WaterMark");
    //specify WaterMark's font size
    wg.StampFontSize = 20;
    //specify WaterMark's font family
    wg.StampFont = "Times New Roman";
    //specify WaterMark's opacity
    wg.StampFontTransparency = 1f;
    //specify WaterMark's rotation
    wg.StampRotation = 45.0f;
    //specify WaterMark's color
    wg.StampColorNET = new ADK.PDF.Color() { Red = 255, Green = 0, Blue = 0, Gray = 0 };
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert above sources to PDF file
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
	Dim wg As New WebGrabber()
	'HTML source for Page content
	Dim html As String = "<h1 style='text-align:center'>WaterMark Example</h1>"
	'assign page content source
	wg.CreateFromHTMLText = html
	'add text stamp as WaterMark
	wg.AddStampText(270.0F, 350.0F, "WaterMark")
	'specify WaterMark's font size
	wg.StampFontSize = 20
	'specify WaterMark's font family
	wg.StampFont = "Times New Roman"
	'specify WaterMark's opacity
	wg.StampFontTransparency = 1F
	'specify WaterMark's rotation
	wg.StampRotation = 45.0F
	'specify WaterMark's color
	wg.StampColorNET = New ADK.PDF.Color() With {
		.Red = 255,
		.Green = 0,
		.Blue = 0,
		.Gray = 0
	}
	'specify directory for newly created file
	wg.OutputDirectory = "E:/"
	'specify file name
	wg.NewDocumentName = "Sample.pdf"
	'convert above sources to PDF file
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下螢幕截圖是新生成的Sample.pdf文件。

Active4 related to 6.2. 使用 ActivePDF 水印

6.3. IronPDF 與 ActivePDF 的區別

  • IronPDF 讓添加水印變得非常簡單
  • IronPDF 提供直接設定浮水印屬性的功能
  • ActivePDF WebGrabber 擁有複雜的程式結構,需要許多代碼行以進行處理。

7. 設定 PDF 頁面的邊距

假設我們在本地磁碟 E中有一個名為myHtmlFile.html的簡單網頁,寬度為100%,並設有black顏色的邊框。 我們將從中生成 PDF 文件並設置頁邊距。 讓我們從IronPDF開始。

7.1. 使用 IronPDF 的邊距設置

要設定邊距,IronPDF 提供了 ChromePdfRenderOptions 類別,具有以下屬性:

  • MarginLeft 用於設置頁面左側的邊距。
  • MarginRight 用於設置頁面右側的邊距。
  • MarginTop 設定頁面頂部的邊距。
  • MarginBottom 用來設定頁面底部的邊距。

    注意:預設情況下,IronPDF 會在左、上、右、下各設置20mm的邊距,以提高頁面的可讀性。 如果不需要,我們可以將其設為0mm

/**
Set Margins
anchor-margins-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create html to PDF converter
    var converter = new ChromePdfRenderer();
    //specify left Margin
    converter.RenderingOptions.MarginLeft = 50;
    //specify top Margin
    converter.RenderingOptions.MarginTop = 40;
    //render html file to PDF
    using var PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html");
    //save to the target location
    PDF.SaveAs("E:/Sample.pdf");
}
/**
Set Margins
anchor-margins-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create html to PDF converter
    var converter = new ChromePdfRenderer();
    //specify left Margin
    converter.RenderingOptions.MarginLeft = 50;
    //specify top Margin
    converter.RenderingOptions.MarginTop = 40;
    //render html file to PDF
    using var PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html");
    //save to the target location
    PDF.SaveAs("E:/Sample.pdf");
}
'''
'''Set Margins
'''anchor-margins-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create html to PDF converter
	Dim converter = New ChromePdfRenderer()
	'specify left Margin
	converter.RenderingOptions.MarginLeft = 50
	'specify top Margin
	converter.RenderingOptions.MarginTop = 40
	'render html file to PDF
	Dim PDF = converter.RenderHTMLFileAsPdf("E:/myHtmlFile.html")
	'save to the target location
	PDF.SaveAs("E:/Sample.pdf")
End Sub
$vbLabelText   $csharpLabel

以下截圖顯示了由上述代碼新生成的Sample.pdf文件:

Iron5 related to 7.1. 使用 IronPDF 的邊距設置

可以看出,PDF 頁面距離左側是 50mm,距離頂部是 40,左邊距是 20mm,這是默認的。 我們可以看到,使用 IronPDF 的 ChromePdfRenderOptions 類來設置任何一邊的邊距是多麼簡單。

閱讀更多關於PDF 生成設置的詳細資訊:如何處理 PDF 文件的邊距和其他屬性。

現在,我們將使用 ActivePDF WebGrabber 設定頁面邊距。

7.2. 使用 ActivePDF 設定邊距

要設置頁面邊距,ActivePDF WebGrabber 提供了SetMargins()函式,我們可以按照以下方式使用它:

設定邊距(上邊距, 下邊距, 左邊距, 右邊距)

我們將使用此函數來設置頁面邊距:

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber()
    //specify source HTML file path
    wg.URL = "E:/myHtmlFile.html";
    //Margins
    wg.SetMargins(1, 0, 1.5f, 0);
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert HTML file to PDF
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber()
    //specify source HTML file path
    wg.URL = "E:/myHtmlFile.html";
    //Margins
    wg.SetMargins(1, 0, 1.5f, 0);
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert HTML file to PDF
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: WebGrabber wg = new WebGrabber() wg.URL = "E:/myHtmlFile.html";
	New WebGrabber() wg.URL = "E:/myHtmlFile.html"
	Dim wg As New WebGrabber() wg.URL
	'Margins
	wg.SetMargins(1, 0, 1.5F, 0)
	'specify directory for newly created file
	wg.OutputDirectory = "E:/"
	'specify file name
	wg.NewDocumentName = "Sample.pdf"
	'convert HTML file to PDF
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下截圖顯示了由上述代碼新生成的Sample.pdf文件:

Active5 related to 7.2. 使用 ActivePDF 設定邊距

我們可以看到 PDF 頁面從左側有 1.5f 的邊距,從頂部有 1f 的邊距。使用這兩個組件,我們可以輕鬆地根據需要設置頁面邊距。

閱讀更多關於如何使用ActivePDF 設置邊距的信息。


8. 為 PDF 設定頁首和頁尾

在此比較中,我們將了解如何設置 PDF 文件的頁眉和頁腳。我們將使用兩個組件提供的功能和技術,通過這些方法我們可以以程式方式在 PDF 頁面上打印自定義的頁眉和頁腳。

8.1. 使用 IronPDF 處理頁首和頁尾

IronPDF 提供以下屬性,可用於設定頁首和頁尾:

  • LeftText:設置頁首或頁腳左側的文字。
  • CenterText:在中心打印頁眉或頁腳文本。
  • RightText:設置左側的頁眉或頁腳文字。
  • FontFamily : 設定標頭或頁腳文字的字型系列。
  • FontSize : 設定頁首或頁尾文字的字體大小。
  • 間距:設定頁面內容與頁眉或頁腳之間的空間。
  • DrawDividerLine: 繪製一條水平線,以將頁面內容與頁眉或頁腳分隔開來。

    我們可以在大括號 {} 中使用 IronPDF 的以下預定義函數作為頁首或頁尾:

  • {page} 會列印目前的頁碼。
  • {total-pages} 用於列印 PDF 的總頁數。
  • {url} 用於打印渲染 PDF 的 URL。
  • {date} 用於印出今天的日期。
  • {time} 打印當前時間。
  • {html-title} 用於列印已呈現的 HTML 檔案標題。
  • {pdf-title} 設定文件標題。

    讓我們來看看以下示例,其中我們將使用上述功能設置頁眉和頁腳:

/**
Set Header Footers
anchor-headers-and-footers-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create html to PDF converter
    var converter = new IronPdf.ChromePdfRenderer();
    //Page Content source
    string html = "<h1 style='text-align:center;'>Page Content</h2>";
    //Assign source to converter
    using var PDF = converter.RenderHtmlAsPdf(html);
    //Add Header settings
    converter.RenderingOptions.TextHeader = new TextHeaderFooter()
    {
        LeftText = "Header Text",
        RightText = "{date} {time}",
        DrawDividerLine=true,
        FontSize=13
    };
    //Add Footer settings
    converter.RenderingOptions.TextFooter = new TextHeaderFooter()
    {
        RightText = "Page {page} of {total-pages}",
        FontSize = 12
    };
    //save to target location
    PDF.SaveAs("E:/Sample.pdf");
}
/**
Set Header Footers
anchor-headers-and-footers-with-ironpdf
**/
using IronPdf;
static void Main(string [] args)
{
    //create html to PDF converter
    var converter = new IronPdf.ChromePdfRenderer();
    //Page Content source
    string html = "<h1 style='text-align:center;'>Page Content</h2>";
    //Assign source to converter
    using var PDF = converter.RenderHtmlAsPdf(html);
    //Add Header settings
    converter.RenderingOptions.TextHeader = new TextHeaderFooter()
    {
        LeftText = "Header Text",
        RightText = "{date} {time}",
        DrawDividerLine=true,
        FontSize=13
    };
    //Add Footer settings
    converter.RenderingOptions.TextFooter = new TextHeaderFooter()
    {
        RightText = "Page {page} of {total-pages}",
        FontSize = 12
    };
    //save to target location
    PDF.SaveAs("E:/Sample.pdf");
}
'''
'''Set Header Footers
'''anchor-headers-and-footers-with-ironpdf
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
	'create html to PDF converter
	Dim converter = New IronPdf.ChromePdfRenderer()
	'Page Content source
	Dim html As String = "<h1 style='text-align:center;'>Page Content</h2>"
	'Assign source to converter
	Dim PDF = converter.RenderHtmlAsPdf(html)
	'Add Header settings
	converter.RenderingOptions.TextHeader = New TextHeaderFooter() With {
		.LeftText = "Header Text",
		.RightText = "{date} {time}",
		.DrawDividerLine=True,
		.FontSize=13
	}
	'Add Footer settings
	converter.RenderingOptions.TextFooter = New TextHeaderFooter() With {
		.RightText = "Page {page} of {total-pages}",
		.FontSize = 12
	}
	'save to target location
	PDF.SaveAs("E:/Sample.pdf")
End Sub
$vbLabelText   $csharpLabel

以下截圖顯示了由上述代碼新生成的Sample.pdf文件:

Iron6 related to 8.1. 使用 IronPDF 處理頁首和頁尾

我們可以看到

  • Header Text 列印在頁首的左側。
  • DateTime 列印在頁首的右側。
  • 繪製一條水平線將頁首與頁面內容分隔開來。
  • 頁面 CurrentPage of TotalPages 在頁腳的右側。

    閱讀更多有關使用 IronPDF 設定HTML 到 PDF 屬性的資訊。

    現在讓我們使用 ActivePDF WebGrabber 設置頁眉和頁腳:

8.2. 使用 ActivePDF 的頁首和頁尾

ActivePDF WebGrabber 提供了 HeaderHTMLFooterHTML 屬性來分別設置頁首和頁尾。 原始 HTML 被作為頁面頁首或頁尾傳遞給這些屬性。 與 IronPDF 不同,ActivePDF WebGrabber 並未提供預定義的函數來設置頁眉和頁腳的對齊方式,因此我們必須使用 HTML 和 CSS 屬性來進行設置,如下所示:

using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //Page content source
    string html = @"<h1 style='text-align:center;'>Page Content</h2>";
    //assign above source to WebGrabber
    wg.CreateFromHTMLText = html;
    //specify Footer height
    wg.FooterHeight = 0.5f;
    //Add Footer setting
    wg.FooterHTML = "<div style='text-align: right;'>%cp% of %tp%</div>";
    //create object for datetime
    DateTime now = DateTime.Now;
    //specify header height
    wg.HeaderHeight = 0.5f;
    //Add Header setting
    wg.HeaderHTML = "<div style='float: left;'>Header Text</div>";
    //append Header settings
    wg.HeaderHTML = $"<div style='float: right;'>{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}</div>";
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert above sources to PDF file
    wg.ConvertToPDF();
}
using APWebGrabber;
static void Main(string [] args)
{
    //Instantiate Object
    WebGrabber wg = new WebGrabber();
    //Page content source
    string html = @"<h1 style='text-align:center;'>Page Content</h2>";
    //assign above source to WebGrabber
    wg.CreateFromHTMLText = html;
    //specify Footer height
    wg.FooterHeight = 0.5f;
    //Add Footer setting
    wg.FooterHTML = "<div style='text-align: right;'>%cp% of %tp%</div>";
    //create object for datetime
    DateTime now = DateTime.Now;
    //specify header height
    wg.HeaderHeight = 0.5f;
    //Add Header setting
    wg.HeaderHTML = "<div style='float: left;'>Header Text</div>";
    //append Header settings
    wg.HeaderHTML = $"<div style='float: right;'>{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}</div>";
    //specify directory for newly created file
    wg.OutputDirectory = "E:/";
    //specify file name
    wg.NewDocumentName = "Sample.pdf";
    //convert above sources to PDF file
    wg.ConvertToPDF();
}
Imports APWebGrabber
Shared Sub Main(ByVal args() As String)
	'Instantiate Object
	Dim wg As New WebGrabber()
	'Page content source
	Dim html As String = "<h1 style='text-align:center;'>Page Content</h2>"
	'assign above source to WebGrabber
	wg.CreateFromHTMLText = html
	'specify Footer height
	wg.FooterHeight = 0.5F
	'Add Footer setting
	wg.FooterHTML = "<div style='text-align: right;'>%cp% of %tp%</div>"
	'create object for datetime
	Dim now As DateTime = DateTime.Now
	'specify header height
	wg.HeaderHeight = 0.5F
	'Add Header setting
	wg.HeaderHTML = "<div style='float: left;'>Header Text</div>"
	'append Header settings
	wg.HeaderHTML = $"<div style='float: right;'>{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}</div>"
	'specify directory for newly created file
	wg.OutputDirectory = "E:/"
	'specify file name
	wg.NewDocumentName = "Sample.pdf"
	'convert above sources to PDF file
	wg.ConvertToPDF()
End Sub
$vbLabelText   $csharpLabel

以下截圖是從上述程式碼新生成的Sample.pdf文件:

Active6 related to 8.2. 使用 ActivePDF 的頁首和頁尾

閱讀更多有關如何使用ActivePDF WebGrabber 設定頁首和頁尾的信息。

8.3. IronPDF 與 ActivePDF 的區別

  • ActivePDF WebGrabber 沒有預定義的功能來繪製將頁眉與頁面內容分隔的水平線。
  • ActivePDF 需要使用 .NET 框架的 DateTime 函數
  • IronPDF 提供簡單的頁首和頁尾屬性設定。

9. ActivePDF 組件列表

NameDetail
ActivePDF DocConverterIt is used to convert popular file types to and from PDF format.
ActivePDF WebGrabberIt grabs the HTML from many sources and converts it to PDF files.
ActivePDF DocSpaceIt provides Batch Process Automation, and a user interface for display, generate, converting, manipulating, and interacting with PDF and other file formats.
ActivePDF ToolkitIt is used to create, modify, view, extract, manipulate, and automate the document content to and from PDF files.
ActivePDF PortalIt enables the users to view and modify PDF documents from any source in a standard web browser.
ActivePDF CADConverterIt is used to convert CAD files into PDF.
ActivePDF XtractorIt is used to extract and find the text and images from PDF files.
ActivePDF SpoolerIt allows the developer to print the PDF file page on paper.
ActivePDF RedactorIt is used to hide sensitive information from the viewer.
ActivePDF ServerIt provides the printing solution for different purposes.

10. 授權

ActivePDF 並未在其ActivePDF 網站上提供有關其套件的任何資訊。 要獲取有關授權的信息,您必須聯繫他們的銷售人員。 然而,您必須清楚地知道您正在尋找哪種類型的生產許可證。 他們沒有提供價格清單,年度授權的價格從 $1,180 起,但可能根據使用範圍而更高,必須詳細說明以取得報價。

IronPDF 提供透明的定價,授權從$749起,並具多種自訂選項。 如果您有任何問題,請聯絡團隊。


快速指南

探索 IronPDF API 參考文獻

探索 IronPDF C# 庫的 API 參考,包括 IronPDF 的所有功能、類別、方法字段、命名空間和枚舉的詳細資料。

查看 API 參考資料
Documentation related to 快速指南
Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
Aspose PDF 轉換教學與比較
下一個 >
SpirePDF C# HTML轉PDF教學與庫比較