產品比較

ActivePDF DocConverter 教程及與 IronPDF 的比較

發佈 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創建 PDF 從 HTML 來源的檔案,使用 ActivePDF WebGrabber

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

ActivePDF WebGrabber 用於創建PDF

ActivePDF WebGrabber 是ActivePDF的單獨組件,專門用於從HTML來源(如URL、HTML文件或HTML字符串)生成PDF文件。它還提供設置頁面屬性(如頁眉、頁腳、邊距、水印或書籤)的功能,以根據我們的要求創建PDF文件。


比較

1. ActivePDF 與 IronPDF 比較表

讓我們來看看這兩個組件的並排比較。

IronPDFActivePDF
IronPDF 將 HTML 源轉換為 PDF 文件。ActivePDF 將 HTML 源轉換為 PDF 文件。
IronPDF 支援 .NET Core。ActivePDF 不支援 .NET Core。
IronPDF 支援 .NET 4.0 或更高版本。ActivePDF 支援 .NET 4.6.2 或更高版本。
IronPDF 支援 macOS。ActivePDF 不支援 macOS。
IronPDF 可以應用 CSS 設定水印屬性。ActivePDF 不支持使用 CSS 設定水印屬性。
IronPDF 可以設定 PDF 檔案的紙張方向。ActivePDF 可以設定 PDF 文件的紙張方向。
IronPDF 提供 渲染延遲 延遲PDF轉換的函式。ActivePDF 提供的 超時時間 延遲PDF轉換的函式。
IronPDF 提供預設功能來設置頁眉或頁腳。ActivePDF 需要通過原始 HTML 和 CSS 設定頁眉和頁腳。
IronPDF 提供預定義函數來繪製水平線以分隔內容。ActivePDF 不提供分隔頁首和頁尾的線條。
要保存 PDF 檔案,我們可以在一行中設定目錄和檔案名稱。我們必須分別設置文件目錄和文件名稱。
需要用簡單的程式結構寫更少的程式碼。需要寫很多行程式碼。
授權開始於 `$Lite License`.授權開始於 $1180.

步驟 1:安裝

2. 如何安裝 IronPDF

您可以使用兩種不同的方法將 IronPDF 函式庫添加到您的專案中,採用哪種方式都沒有區別。

NuGet 套件管理器

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

或者:

  • 轉到 工具
  • 選擇 套件管理器控制台
  • 執行以下命令:
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
VB   C#

輸出:

上述程式碼會產生一個 PDF 檔案 sample.pdf本地磁盤 E:,其螢幕截圖如下:

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
VB   C#

以下截圖是從此程式碼生成的 sample.pdf 文件:

3.3. IronPDF 與 ActivePDF 的區別

  • 使用 IronPDF 的程式碼行數更少
  • 由 IronPDF 生成的文件更具可讀性,因為預設的邊距

4. 將 HTML 檔案轉換為 PDF 檔案

在此比較中,我們需要將位於 E:/ 目錄下名為 myHtmlFile.html 的 HTML 檔案生成 PDF 檔案,該檔案具有以下 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
VB   C#

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

我們可以看到 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
VB   C#

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

4.3. IronPDF 與 ActivePDF 的區別

  • 使用 IronPDF 只需 3 行程式碼
  • IronPDF 檔案稍微更乾淨/更吸引人

5. 將 URL 轉換為 PDF 文件

假設我們有一個 URL 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
VB   C#

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

您可以訪問該網頁 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
VB   C#

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

5.3. IronPDF 和 ActivePDF 的區別

  • IronPDF 具有更簡單的生成 PDF 文件的結構
  • 只需要三行代碼
  • IronPDF 更接近網站的匹配

6. 在 PDF 上建立浮水印

在這個範例中,我們將使用 HTML 字串來建立一個 PDF 文件,然後在頁面的中央新增一個浮水印。我們先從 IronPDF 開始。

6.1. 使用 IronPDF 添加浮水印

IronPDF 提供以下函數以添加浮水印:

WatermarkPage(水印HTML字符串, 頁面索引水印, 水印位置, 不透明度, 旋轉, 超連結)我們可以使用 WaterMarkLocation 來設置浮水印在以下位置:

  • TopLeft
  • TopCenter
  • TopRight
  • MiddleLeft
  • MiddleCenter
  • MiddleRight
  • BottomLeft
  • BottomCenter
  • BottomRight

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

/**
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
VB   C#

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

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

6.2. 使用 IronPDF 的 Watermark 和 ActivePDF

ActivePDF WebGrabber 不像 IronPDF 提供特定的 WaterMark 功能。但我們可以使用 AddStampText() function 作為此目的的變通方法:

`AddStampText(浮點數 x, 浮點數 y, 字串 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
VB   C#

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

6.3. IronPDF 與 ActivePDF 的區別

  • IronPDF 使添加水印變得非常簡單
  • IronPDF 提供直接設置水印屬性的功能
  • ActivePDF WebGrabber 擁有複雜的編程結構,需要許多行程式碼來繞過

7. 設定 PDF 頁面邊距

假設我們有一個簡單的網頁,名為 myHtmlFile.html,位於我們的 E 磁碟,寬度為 100%,且有一個 黑色邊框。我們將從中生成一個 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
VB   C#

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

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

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

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

7.2. 使用 ActivePDF 設置邊距

要設置頁面邊距,ActivePDF WebGrabber 提供了 SetMargins()函數,我們可以如下使用:

`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
VB   C#

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

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

閱讀更多有關如何 用 ActivePDF 設定邊距.


8. 設定 PDF 的頁首和頁尾

在這個對比中,我們將看到如何設定 PDF 檔案的頁首和頁尾。我們將使用兩個元件提供的功能和技術,通過這些功能和技術,我們可以以程式方式在 PDF 頁面上列印自訂的頁首和頁尾。

8.1. 使用 IronPDF 設定頁首和頁尾

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

  • LeftText: 設置左側的頁首或頁尾文字。
  • CenterText: 在中央列印頁首或頁尾文字。
  • RightText: 設置右側的頁首或頁尾文字。
  • FontFamily: 設置頁首或頁尾文字的字體。
  • FontSize: 設置頁首或頁尾文字的字號。
  • Spacing: 設置頁面內容與頁首或頁尾之間的間距。
  • DrawDividerLine: 繪製一條水平線,用以分隔頁面內容與頁首或頁尾。

我們可以在大括號中使用以下 IronPDF 的預定義函數 {} 用於頁眉或頁腳:

  • {頁面}它會印出當前頁碼。
  • {總頁數} 它用於列印PDF的總頁數。
  • {網址} 它被用來打印渲染後的PDF的URL。
  • {日期} 它用來列印今天的日期。{時間}** 它會打印當前時間。
  • {html-標題}它用於打印渲染的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
VB   C#

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

我們可以看到

  • Header Text 出現在頁眉的左側。
  • DateTime 出現在頁眉的右側。
  • 畫了一條水平線將頁眉與頁面內容分隔開來。
  • Page CurrentPage of TotalPages 出現在頁腳的右側。

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

現在讓我們使用 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
VB   C#

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

請閱讀更多有關如何設置 頁首和頁尾與 ActivePDF WebGrabber.

8.3. IronPDF 與 ActivePDF 的區別

  • ActivePDF WebGrabber 沒有預定函數來繪製區分頁眉與頁面內容的水平線
  • ActivePDF 需要使用 .NET 框架的 DateTime 函數
  • IronPDF 提供簡單設置頁眉和頁腳屬性

9. ActivePDF 組件列表

名稱詳細信息
ActivePDF 文件轉換器它用於將流行的文件類型轉換至 PDF 格式及從 PDF 格式轉換。
ActivePDF WebGrabber它從多個來源獲取HTML並將其轉換為PDF文件。
ActivePDF DocSpace它提供了批次處理自動化和用於顯示、生成、轉換、操作和與PDF及其他文件格式交互的用戶介面。
ActivePDF 工具包用於創建、修改、查看、提取、操作和自動化 PDF 文件的文檔內容。
ActivePDF 入口網站它讓用戶能夠在標準的網頁瀏覽器中檢視和修改來自任何來源的PDF文件。
ActivePDF CADConverter用於將 CAD 文件轉換為 PDF。
ActivePDF Xtractor它用於從 PDF 文件中提取和查找文字和圖像。
ActivePDF Spooler它允許開發者將 PDF 文件頁打印在紙上。
ActivePDF Redactor它用來隱藏觀眾的敏感資訊。
ActivePDF 服务器它提供了適用於不同用途的打印解決方案。

10. 授權

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

IronPDF提供透明的定價 licenses from $749,提供許多可自定義的選項。如果有任何問題,請聯繫團隊。


快速指南

探索 IronPDF API 參考文獻

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

查看 API 參考文件
< 上一頁
Aspose PDF 轉換教學與比較
下一個 >
SpirePDF C# HTML轉PDF教學與庫比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >