产品比较

ActivePDF DocConverter 教程和对比 IronPDF

发布 2021年九月23日
分享:

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

IronPDF` 是一个 C# PDF库 该公司还提供这些功能,而且价格具有竞争力。

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


概述

关于 IronPDF C# 库

铁软件 是市场领先的元件供应商,提供 IronPDF 用于处理 PDF 文件。 这是一种包罗万象的方法,可轻松从不同格式生成 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"。 至 创建 PDF 使用 ActivePDF WebGrabber 从 HTML 源获取文件。

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

用于创建 PDF 的 ActivePDF WebGrabber

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 来设置 WaterMark 属性。ActivePDF 不支持通过 CSS 设置 WaterMark 属性。
IronPDF 可以设置 PDF 文件的纸张方向。ActivePDF 可以设置 PDF 文件的纸张方向。
IronPDF 提供 渲染延迟 函数来延迟 PDF 转换。ActivePDF 提供 超时时间 函数来延迟 PDF 转换。
IronPDF 提供设置页眉或页脚的预定义函数。ActivePDF 需要通过原始 HTML 和 CSS 设置页眉和页脚。
IronPDF 提供了一个预定义函数,用于绘制水平线来分隔内容。ActivePDF 不提供分隔页眉和页脚的行。
要保存 PDF 文件,我们可以在一行中设置目录和文件名。我们必须分别设置文件目录和文件名。
需要以简单的编程结构编写较少行的代码。需要编写多行代码。
许可证从 $749.许可证从 $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#

输出:

上述代码将在 "本地磁盘 E: "中创建一个 PDF 文件 sample.pdf ,其截图如下:

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 提供了以下添加 WaterMark 的功能:

水印页面(水印 HTML 字符串、PageIndexToWaterMark、水印位置、不透明度、旋转、超链接)`

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

  • 左上角
  • 顶级中心
  • 右上角
  • 中左
  • 中部中心
  • 右中部
  • 左下角
  • 底部中心
  • 右下角

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

/**
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 文件:

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

6.2.使用 IronPDF ActivePDF 创建水印

与 IronPDF 不同,ActivePDF WebGrabber 没有为 WaterMark 提供特定功能。 但我们可以使用 `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 页面上设置页边距

假设我们在 "本地磁盘 E "中有一个名为 "myHtmlFile.html "的简单网页,其宽度为 "100%","边框 "为 "黑色"。 我们将从中生成 PDF 文件并设置页边距。 让我们从 IronPDF 开始。

7.1.IronPDF 的利润率

要设置页边距,IronPDF 提供了 "ChromePdfRenderOptions "类,该类具有以下属性:

  • MarginLeft 用于设置页面左侧的页边距。
  • MarginRight 用于设置页面右侧的页边距。
  • MarginTop 从页面顶部开始设置页边距。
  • MarginBottom 从页面底部开始设置页边距。

    注: 默认情况下,IronPDF 会从左、上、右、下各设置一个 20mm mMargin,以使页面更具可读性。 如果不需要,我们可以将其设置为 "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,顶部边距为 40mm,左侧边距为 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
VB   C#

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

我们可以看到,PDF 页面左侧的页边距为 1.5f,顶部的页边距为 1f。使用这两个组件,我们可以轻松地根据要求设置页面边距。

了解更多如何 使用 ActivePDF 设置页边距.


8.为 PDF 文件设置页眉和页脚

在本比较中,我们将了解如何设置 PDF 文件的页眉和页脚。我们将使用这两个组件提供的函数和技术,通过这些函数和技术,我们可以在 PDF 页面上以编程方式打印自定义页眉和页脚。

8.1.使用 IronPDF 的页眉和页脚

IronPDF 提供以下属性,可用于设置页眉和页脚:

  • LeftText : 在左侧设置页眉或页脚文本。
  • CenterText : 在中间打印页眉或页脚文本。
  • RightText : 设置左侧的页眉或页脚文本。
  • FontFamily : 设置页眉或页脚文本的字体系列。
  • FontSize : 设置页眉或页脚文本的字体大小。
  • 间距: 设置页面内容与页眉或页脚之间的间距。
  • DrawDividerLine: 它绘制一条水平线,将页面内容与页眉或页脚分开。

    我们可以使用 IronPDF 的以下预定义函数(用 "大括号 "括起来{}用于页眉或页脚:

  • {页码} 打印当前页码。
  • {总页数} 它用于打印 PDF 文件的总页数。
  • {网址} 它用于打印已渲染 PDF 的 URL。
  • {日期} 它用来打印今天的日期。
  • {时间} 它会打印当前时间。
  • {html-title} 用于打印已渲染 HTML 文件的标题。
  • {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
**/
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 文件:

我们可以看到

  • 页眉文本 "打印在页眉的左侧。
  • 日期时间 "打印在页眉右侧。
  • 画一条水平线,将页眉与页面内容分开。
  • 页脚右侧的 "TotalPages 的当前页"。

    了解更多有关设置的信息 HTML 转换为 PDF 的属性 使用 IronPDF。

    现在让我们使用 ActivePDF WebGrabber 设置页眉和页脚:

8.2.使用 ActivePDF 的页眉和页脚

ActivePDF WebGrabber 提供了 "HeaderHTML "和 "FooterHTML "属性,分别用于设置页眉和页脚。 原始 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 Framework 的 DateTime 函数
  • IronPDF 提供简单的页眉和页脚属性设置

9.ActivePDF 组件列表

名字详细信息
ActivePDF DocConverter它用于将流行的文件类型转换为 PDF 格式或从 PDF 格式转换为 PDF 格式。
ActivePDF WebGrabber它可从多种来源抓取 HTML 并将其转换为 PDF 文件。
ActivePDF 文档空间它提供批处理自动化功能,以及用于显示、生成、转换、操作 PDF 和其他文件格式并与之交互的用户界面。
ActivePDF 工具包它用于创建、修改、查看、提取、操作 PDF 文件,并自动将文件内容导入或导出 PDF 文件。
ActivePDF 门户网站它能让用户在标准网络浏览器中查看和修改来自任何来源的 PDF 文档。
ActivePDF CADConverter它用于将 CAD 文件转换为 PDF。
ActivePDF Xtractor它用于从 PDF 文件中提取和查找文本和图像。
ActivePDF Spooler它允许开发人员在纸张上打印 PDF 文件页面。
ActivePDF Redactor它用于隐藏敏感信息,不让浏览者看到。
ActivePDF 服务器它为不同用途提供打印解决方案。

10.许可

ActivePDF 没有在其网站上提供关于其软件包的任何信息。 ActivePDF 网站. 要获取有关许可的信息,您必须 联系 他们的销售人员。 但是,您必须清楚地知道您正在寻找哪种类型的生产许可证。 他们没有提供价格清单,虽然年度许可证的起价为 1 180 美元,但根据使用范围,价格可能会更高,必须详细说明才能获得报价。

IronPDF 提供透明的定价,包括 来自 $749 的许可证还提供许多可定制的选项。 如有任何疑问,请联系我们的团队。


教程快速访问

探索 IronPDF API 参考

探索 IronPDF C# 库的 API 参考,包括 IronPDF 的所有功能、类、方法字段、命名空间和枚举的详细信息。

查看应用程序接口参考
< 前一页
Aspose PDF转换器教程与对比
下一步 >
SpirePDF C# HTML转PDF教程及库对比

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >