产品比较

ActivePDF DocConverter 教程和对比 IronPDF

Chipego
奇佩戈-卡琳达
2021年九月23日
分享:

ActivePDF Toolkit 是一个用于处理PDF文件的软件组件(包括从不同来源生成PDF文件)并设置其属性(如页眉、页脚、边距或水印)。

IronPDF 是一个C# PDF 库,同时提供这些功能,并且具有竞争力的定价。

在这里,我们将逐步讲解如何在 .NET Visual Studio 项目中使用这两个软件组件的功能、代码示例和操作步骤,这样您可以自行决定哪种方法最适合您的应用程序。


概述

关于 IronPDF C# 库

Iron Software 是一家市场领先的组件提供商,提供用于处理 PDF 文件的 IronPDF。 一种包罗万象的方法,可轻松从不同格式生成 PDF 文件,并以编程方式设置所有属性。 它受到开发人员的青睐,因为只需几行代码就能输出一致、可靠和准确的 PDF 文件。

IronPDF 专为 C#、.NET、VB、ASPX、ASP.NET、MVC 和 .NET Core 而设计。 它支持 Visual Studio、NuGet、Linux、Azure、Docker 等。

关于 ActivePDF 工具包

ActivePDF 是一家软件公司,提供许多组件来处理 PDF 文件。 与单一组件 IronPDF 不同,ActivePDF 为 PDF 文件提供了不同的解决方案。 例如,要减小PDF文件的大小,您可以使用ActivePDF Compressor。 要从HTML源创建PDF,请使用ActivePDF WebGrabber

本文将使用 ActivePDF WebGrabber 与 IronPDF 进行对比,一起来看看吧:

用于创建 PDF 的 ActivePDF WebGrabber

ActivePDF WebGrabber 是 ActivePDF 的一个独立组件,专门用于从 URL、HTML 文件或 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");
}

输出:

上述代码将在本地磁盘E:中创建一个PDF文件sample.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();
}

以下截图是通过此代码新生成的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");
}

以下截图是使用上述代码新生成的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();
}

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

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

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");
}

以下屏幕截图是上述代码新生成的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();
}

以下屏幕截图是上述代码生成的新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 提供了以下添加 WaterMark 的功能:

WatermarkPage(水印HTML字符串, PageIndexToWaterMark, 水印位置, 不透明度, 旋转, 超链接)

我们可以使用WaterMarkLocation在以下位置设置水印:

  • 左上

  • 顶部居中

  • TopRight

  • 左中

  • 中间居中

  • 中右

  • 左下角

  • 底部居中

  • 右下角

    让我们看看如何使用上述函数设置水印:

/**
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");
}

以下屏幕截图是上述代码生成的新Sample.pdf文件:

Iron4 related to 6.1.使用 IronPDF 创建水印

我们可以添加任何类型的 WaterMark,并通过 CSS 设置其属性。 现在,我们将使用 ActivePDF WebGrabber 完成同样的任务。

6.2. 使用 ActivePDF 添加水印

与 IronPDF 不同,ActivePDF WebGrabber 没有为 WaterMark 提供特定功能。 但是我们可以使用AddStampText()函数作为此目的的变通方法:

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

  • 浮点数 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();
}

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

Active4 related to 6.2. 使用 ActivePDF 添加水印

6.3.IronPDF 与 ActivePDF 的区别

  • IronPDF 让添加水印变得非常简单
  • IronPDF 提供设置水印属性的直接功能
  • ActivePDF WebGrabber 的编程结构复杂,有许多行代码需要处理

7.在 PDF 页面上设置页边距

假设我们在本地磁盘E中有一个名为myHtmlFile.html的简单网页,其宽度为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");
}

以下屏幕截图是上述代码生成的新Sample.pdf文件:

Iron5 related to 7.1.IronPDF 的利润率

可以看出,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();
}

以下屏幕截图是上述代码生成的新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");
}

以下屏幕截图是上述代码生成的新Sample.pdf文件:

Iron6 related to 8.1.使用 IronPDF 的页眉和页脚

我们可以看到

  • Header Text打印在页眉的左侧。
  • DateTime打印在页眉的右侧。
  • 画一条水平线,将页眉与页面内容分开。
  • 页 当前页数 总页数 在页脚的右侧。

    了解更多关于使用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();
}

以下截图是从上述代码生成的新 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教程及库对比