在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在 C# 中,处理 PDF 文件对开发人员来说是一项挑战。在为此类文件创建内容时需要考虑很多因素,而将不同格式的内容转换为 PDF 时更是如此。现在,自动化已成为软件开发流程的关键。现在,这些问题都已迎刃而解,因为有了专门用于帮助读取、写入、创建和修改 PDF 文档,以及编辑其属性和将其从其他格式转换为 PDF 格式的库。
在本博文中,我们将对 .NET 和 .NET Core 的两个 PDF 库进行比较。这两个库是
IronPDF 和 Apitron PDF Kit 是为 .NET 和 .NET Core 提供 PDF 处理功能的两个库。它们都可用于文档字段转换和处理任务。它们还都可以创建书签。下一个问题是决定哪个 C# PDF .NET 库最适合我们的 .NET 项目。这篇比较文章将帮助您在这两个 PDF 库之间做出选择。
首先,让我们看看这两个库都有哪些功能,然后再进行比较。
Apitron PDF Kit是一个.NET组件,可让您以任何方式处理或转换PDF文件。您可以添加图像、图纸和文本,签署文件等等。您还可以编辑现有内容。Apitron PDF Kit .NET 组件可用于创建跨多个平台的移动、桌面、Web、Windows 和云应用程序。
使用该 PDF SDK,您将能够
开发人员,尤其是 C# 程序员,一定会对 IronPDF .NET 库爱不释手。有了这个神奇的工具,您就可以轻松构建.NET Core PDF 处理程序。
IronPDF 使用 .NET Chromium 引擎更改 HTML 页面 (代码或 URL 形式) 到 PDF 文件。您完全无需使用复杂的 API 来定位导航对象或从 HTML 设计 PDF。IronPDF 支持标准 Web 文档,包括 HTML、ASPX、JS、CSS 和图片。
可以使用 HTML5、CSS、JS 和图片创建 IronPDF。PDF 可以轻松编辑、加盖公章并添加页眉和页脚。它还能让阅读 PDF 文本和提取图形变得轻而易举。
IronPDF 的突出功能包括
IronPDF 可用于几乎所有与 C# 兼容的操作系统和框架,包括以下系统:
文章其余部分如下:
1.IronPDF 安装
2.Apitron PDF 套件安装
3.创建 PDF 文档
4.在 PDF 中创建页眉和页脚
5.对 PDF 进行数字签名
6.定价和许可
7.结论
下载和安装 IronPDF 库有四种方法。具体如下:
1.使用 NuGet 软件包管理器
2.开发人员命令提示符
3.直接下载 NuGet 软件包
4.下载 IronPDF .DLL 库
要安装 IronPDF,只需右键单击解决方案资源管理器中的项目。这将打开 NuGet 包管理器。
选择后,浏览 IronPDF 软件包并安装,如下图所示。
您还可以通过开发人员命令提示符安装 IronPDF。
Install-Package IronPdf
也可以直接访问 NuGet 网站并下载软件包来安装 IronPDF。具体步骤如下
IronPDF 已下载并准备就绪。不过,在此之前,我们应该先安装 Apitron PDF 库。
对于 .NET Core 应用程序,我们可以使用 NuGet 包管理器安装 Apitron,也可以从以下网址下载并安装 Apitron NuGet 直接访问网站。
您也可以使用 NuGet 包管理器控制台安装 Apitron。按照 IronPDF 中提到的 "使用开发人员命令提示符 "步骤,键入以下命令:
Install-Package Apitron.PDF.Kit
按 ENTER 键。它将下载并安装 Apitron.PDF.Kit。
添加以下命名空间:
using Apitron.PDF.Kit;
using Apitron.PDF.Kit.FixedLayout.Resources;
using Apitron.PDF.Kit.FixedLayout.Resources.Fonts;
using Apitron.PDF.Kit.FlowLayout.Content;
using Apitron.PDF.Kit.Styles;
using Apitron.PDF.Kit.Styles.Appearance;
using Font = Apitron.PDF.Kit.Styles.Text.Font;
using Apitron.PDF.Kit;
using Apitron.PDF.Kit.FixedLayout.Resources;
using Apitron.PDF.Kit.FixedLayout.Resources.Fonts;
using Apitron.PDF.Kit.FlowLayout.Content;
using Apitron.PDF.Kit.Styles;
using Apitron.PDF.Kit.Styles.Appearance;
using Font = Apitron.PDF.Kit.Styles.Text.Font;
Imports Apitron.PDF.Kit
Imports Apitron.PDF.Kit.FixedLayout.Resources
Imports Apitron.PDF.Kit.FixedLayout.Resources.Fonts
Imports Apitron.PDF.Kit.FlowLayout.Content
Imports Apitron.PDF.Kit.Styles
Imports Apitron.PDF.Kit.Styles.Appearance
Imports Font = Apitron.PDF.Kit.Styles.Text.Font
现在,让我们来比较一下 IronPDF 和 Apitron PDF。
IronPDF 有多种创建 PDF 的方法。让我们来看看其中两种重要的方法。
IronPDF 可以轻松地将任何 URL 转换为 PDF。以下代码有助于将 URL 转换为 PDF。
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using var Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Pdf.SaveAs("url.pdf");
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using var Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Pdf.SaveAs("url.pdf");
Dim Renderer As New IronPdf.ChromePdfRenderer()
Dim Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Pdf.SaveAs("url.pdf")
下面的代码解释了如何从 HTML 字符串渲染 PDF 文档。您可以只使用 HTML,也可以将其与 CSS、图片和 JavaScript 结合使用。
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");
PDF.SaveAs("pixel-perfect.pdf");
// Load external HTML assets: images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
using var AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>Sample Text</h1>", @"C:\site\assets\");
AdvancedPDF.SaveAs("html-with-assets.pdf");
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");
PDF.SaveAs("pixel-perfect.pdf");
// Load external HTML assets: images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
using var AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>Sample Text</h1>", @"C:\site\assets\");
AdvancedPDF.SaveAs("html-with-assets.pdf");
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>")
PDF.SaveAs("pixel-perfect.pdf")
' Load external HTML assets: images, CSS and JavaScript.
' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
Dim AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>Sample Text</h1>", "C:\site\assets\")
AdvancedPDF.SaveAs("html-with-assets.pdf")
输出结果如下
Apitron PDF 不提供将 URL 或 HTML 转换为 PDF 的功能。它允许将 PDF 转换为 HTML。不过,它也能将 XML 文件转换为 PDF。使用的 XML 文件是
<?xml version="1.0" encoding="utf-8"?>
<FlowDocument xmlns="Apitron.PDF.Kit.FlowLayout.v1">
<Resources>
<Image resourceId="logo" fileName="../../images/logo.png" />
</Resources>
<Styles>
<Style selector="flowdocument">
<Color value="Black" />
</Style>
<Style selector="grid">
<InnerBorder thickness="1" />
<InnerBorderColor value="Black" />
</Style>
</Styles>
<Elements>
<Image>
<Properties>
<Class value="logo" />
<ResourceId value="logo" />
</Properties>
</Image>
<TextBlock>
<Properties>
<Class value="header" />
<Text value="Sample Interview Questions for Candidates" />
</Properties>
</TextBlock>
<Br />
<TextBlock>
<Properties>
<Class value="headerNote" />
<Text value="To help facilitate the interview process, the Human Resources Department has compiled a list of questions that might be used during the phone and/or on-campus interviews. Some of the questions deal with the same content, but are phrased differently while other questions may not pertain to a specific discipline; however all of the questions are unbiased and appropriate to ask. We hope you'll find this helpful." />
</Properties>
</TextBlock>
</Elements>
<Properties>
<Margin value="30,20,30,20" />
</Properties>
</FlowDocument>
将 XML 模板转换为 PDF 的代码如下:
using (Stream stream = File.OpenRead("C:\\PLACE YOUR EXACT PATH HERE\\template.xml"), outputStream = File.Create("fromTemplate.pdf"))
{
ResourceManager resourceManager = new ResourceManager();
FlowDocument doc = FlowDocument.LoadFromXml(stream, resourceManager);
doc.Write(outputStream, resourceManager);
}
using (Stream stream = File.OpenRead("C:\\PLACE YOUR EXACT PATH HERE\\template.xml"), outputStream = File.Create("fromTemplate.pdf"))
{
ResourceManager resourceManager = new ResourceManager();
FlowDocument doc = FlowDocument.LoadFromXml(stream, resourceManager);
doc.Write(outputStream, resourceManager);
}
Using stream As Stream = File.OpenRead("C:\PLACE YOUR EXACT PATH HERE\template.xml"), outputStream As Stream = File.Create("fromTemplate.pdf")
Dim resourceManager As New ResourceManager()
Dim doc As FlowDocument = FlowDocument.LoadFromXml(stream, resourceManager)
doc.Write(outputStream, resourceManager)
End Using
输出结果如下
如果比较一下 IronPDF 和 Apitron 的输出结果,我们可以清楚地看到,IronPDF 使用 HTML 渲染技术创建了令人惊叹的文档,甚至无需缩放图片大小。另一方面,Apitron 的输出结果与 IronPDF 相似,但前提是 XML 文件已注册样式。
使用 IronPDF 添加页眉和页脚非常简单。IronPDF 允许您添加页码和分页符,附加封面页和页边距等。
添加页眉和页脚的代码如下:
var Renderer = new ChromePdfRenderer();
// Add a header
Renderer.RenderingOptions.FirstPageNumber = 1; // use 2 if a cover page will be appended
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "This is the header text";
Renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica;
Renderer.RenderingOptions.TextHeader.FontSize = 12;
Renderer.RenderingOptions.MarginTop = 25; //create 25mm space for header
// Add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial;
Renderer.RenderingOptions.TextFooter.FontSize = 12;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
Renderer.RenderingOptions.MarginTop = 25; //create 25mm space for footer
using var AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>This is header and footer generated pdf file</h1>", @"C:\site\assets\");
AdvancedPDF.SaveAs("html-with-assets.pdf");
var Renderer = new ChromePdfRenderer();
// Add a header
Renderer.RenderingOptions.FirstPageNumber = 1; // use 2 if a cover page will be appended
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "This is the header text";
Renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica;
Renderer.RenderingOptions.TextHeader.FontSize = 12;
Renderer.RenderingOptions.MarginTop = 25; //create 25mm space for header
// Add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial;
Renderer.RenderingOptions.TextFooter.FontSize = 12;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
Renderer.RenderingOptions.MarginTop = 25; //create 25mm space for footer
using var AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>This is header and footer generated pdf file</h1>", @"C:\site\assets\");
AdvancedPDF.SaveAs("html-with-assets.pdf");
Dim Renderer = New ChromePdfRenderer()
' Add a header
Renderer.RenderingOptions.FirstPageNumber = 1 ' use 2 if a cover page will be appended
Renderer.RenderingOptions.TextHeader.DrawDividerLine = True
Renderer.RenderingOptions.TextHeader.CenterText = "This is the header text"
Renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica
Renderer.RenderingOptions.TextHeader.FontSize = 12
Renderer.RenderingOptions.MarginTop = 25 'create 25mm space for header
' Add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = True
Renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial
Renderer.RenderingOptions.TextFooter.FontSize = 12
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
Renderer.RenderingOptions.MarginTop = 25 'create 25mm space for footer
Dim AdvancedPDF = Renderer.RenderHtmlAsPdf("<img src='logo.png'><h1>This is header and footer generated pdf file</h1>", "C:\site\assets\")
AdvancedPDF.SaveAs("html-with-assets.pdf")
输出结果如下
Apitron PDF Kit 通过将 XML 结构格式的内容转换为 PDF 来创建 PDF。在 Apitron PDF Fit 中,可以使用 PageHeader
和 PageFooter
属性添加页眉和页脚。具体操作代码如下:
// register doc's resources first
ResourceManager resourceManager = new ResourceManager();
resourceManager.RegisterResource(new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("logo","../../data/logo.png"));
// create document
FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};
// register styles
doc.StyleManager.RegisterStyle(".pageHeader",new Style(){Font = new Font(StandardFonts.TimesBold, 20)});
doc.StyleManager.RegisterStyle(".pageFooter",new Style(){Align = Align.Right});
doc.StyleManager.RegisterStyle("hr",new Style(){Height = 2, Margin = new Thickness(0,5,0,5)});
doc.StyleManager.RegisterStyle(".content",new Style(){Align = Align.Left, Display = Display.InlineBlock});
// fill the header section
doc.PageHeader.Class = "pageHeader";
doc.PageHeader.Add(new Image("logo"){Width = 100, Height = 50});
doc.PageHeader.Add(new TextBlock("This document is intended for internal use only"){TextIndent = 20});
doc.PageHeader.Add(new Hr());
// fill the footer section
doc.PageFooter.Class = "pageFooter";
doc.PageFooter.Add(new Hr());
doc.PageFooter.Add(new TextBlock((ctx)=>string.Format("Page {0} from ",ctx.CurrentPage+1)));
doc.PageFooter.Add(new PageCount(3){Display = Display.Inline});
// add pages
for (int i = 0; i < 2; ++i)
{
doc.Add(new TextBlock("This is header and footer generation pdf file.") {Class = "content"});
doc.Add(new PageBreak());
}
// generate PDF
using (Stream stream = File.Create("out.pdf"))
{
doc.Write(stream, resourceManager);
}
Process.Start("out.pdf");
// register doc's resources first
ResourceManager resourceManager = new ResourceManager();
resourceManager.RegisterResource(new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("logo","../../data/logo.png"));
// create document
FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};
// register styles
doc.StyleManager.RegisterStyle(".pageHeader",new Style(){Font = new Font(StandardFonts.TimesBold, 20)});
doc.StyleManager.RegisterStyle(".pageFooter",new Style(){Align = Align.Right});
doc.StyleManager.RegisterStyle("hr",new Style(){Height = 2, Margin = new Thickness(0,5,0,5)});
doc.StyleManager.RegisterStyle(".content",new Style(){Align = Align.Left, Display = Display.InlineBlock});
// fill the header section
doc.PageHeader.Class = "pageHeader";
doc.PageHeader.Add(new Image("logo"){Width = 100, Height = 50});
doc.PageHeader.Add(new TextBlock("This document is intended for internal use only"){TextIndent = 20});
doc.PageHeader.Add(new Hr());
// fill the footer section
doc.PageFooter.Class = "pageFooter";
doc.PageFooter.Add(new Hr());
doc.PageFooter.Add(new TextBlock((ctx)=>string.Format("Page {0} from ",ctx.CurrentPage+1)));
doc.PageFooter.Add(new PageCount(3){Display = Display.Inline});
// add pages
for (int i = 0; i < 2; ++i)
{
doc.Add(new TextBlock("This is header and footer generation pdf file.") {Class = "content"});
doc.Add(new PageBreak());
}
// generate PDF
using (Stream stream = File.Create("out.pdf"))
{
doc.Write(stream, resourceManager);
}
Process.Start("out.pdf");
' register doc's resources first
Dim resourceManager As New ResourceManager()
resourceManager.RegisterResource(New Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("logo","../../data/logo.png"))
' create document
Dim doc As New FlowDocument() With {.Margin = New Thickness(10)}
' register styles
doc.StyleManager.RegisterStyle(".pageHeader",New Style() With {.Font = New Font(StandardFonts.TimesBold, 20)})
doc.StyleManager.RegisterStyle(".pageFooter",New Style() With {.Align = Align.Right})
doc.StyleManager.RegisterStyle("hr",New Style() With {
.Height = 2,
.Margin = New Thickness(0,5,0,5)
})
doc.StyleManager.RegisterStyle(".content",New Style() With {
.Align = Align.Left,
.Display = Display.InlineBlock
})
' fill the header section
doc.PageHeader.Class = "pageHeader"
doc.PageHeader.Add(New Image("logo") With {
.Width = 100,
.Height = 50
})
doc.PageHeader.Add(New TextBlock("This document is intended for internal use only") With {.TextIndent = 20})
doc.PageHeader.Add(New Hr())
' fill the footer section
doc.PageFooter.Class = "pageFooter"
doc.PageFooter.Add(New Hr())
doc.PageFooter.Add(New TextBlock(Function(ctx) String.Format("Page {0} from ",ctx.CurrentPage+1)))
doc.PageFooter.Add(New PageCount(3) With {.Display = Display.Inline})
' add pages
For i As Integer = 0 To 1
doc.Add(New TextBlock("This is header and footer generation pdf file.") With {.Class = "content"})
doc.Add(New PageBreak())
Next i
' generate PDF
Using stream As Stream = File.Create("out.pdf")
doc.Write(stream, resourceManager)
End Using
Process.Start("out.pdf")
输出结果如下
最重要的 PDF 处理功能之一就是对 PDF 文档进行数字签名。IronPDF 提供了实现这一功能所需的所有工具。
using IronPdf;
// Cryptographically sign an existing PDF in 1 line of code!
new IronPdf.Signing.PdfSignature("Iron.p12", "123456").SignPdfFile("any.pdf");
/***** Advanced example for more control *****/
// 1. Create a PDF
var Renderer = new IronPdf.ChromePdfRenderer();
var doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
var signature = new IronPdf.Signing.PdfSignature("Iron.pfx", "123456");
// 3. Handwritten signature graphic
signature.LoadSignatureImageFromFile("handwriting.png");
// 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(signature);
// 5. The PDF is not signed until saved to file, stream or byte array.
doc.SaveAs("signed.pdf");
using IronPdf;
// Cryptographically sign an existing PDF in 1 line of code!
new IronPdf.Signing.PdfSignature("Iron.p12", "123456").SignPdfFile("any.pdf");
/***** Advanced example for more control *****/
// 1. Create a PDF
var Renderer = new IronPdf.ChromePdfRenderer();
var doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
var signature = new IronPdf.Signing.PdfSignature("Iron.pfx", "123456");
// 3. Handwritten signature graphic
signature.LoadSignatureImageFromFile("handwriting.png");
// 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(signature);
// 5. The PDF is not signed until saved to file, stream or byte array.
doc.SaveAs("signed.pdf");
Imports IronPdf
' Cryptographically sign an existing PDF in 1 line of code!
Call (New IronPdf.Signing.PdfSignature("Iron.p12", "123456")).SignPdfFile("any.pdf")
'''*** Advanced example for more control ****
' 1. Create a PDF
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>")
' 2. Create a Signature.
' You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
Dim signature = New IronPdf.Signing.PdfSignature("Iron.pfx", "123456")
' 3. Handwritten signature graphic
signature.LoadSignatureImageFromFile("handwriting.png")
' 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(signature)
' 5. The PDF is not signed until saved to file, stream or byte array.
doc.SaveAs("signed.pdf")
使用 Apitron PDF Kit 在 PDF 上加盖签名是一个漫长的技术过程。
static void Main(string [] args)
{
string fileName = "signedTwice.pdf";
using (Stream stream = File.Create(fileName))
{
FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};
doc.Add(new TextBlock("Signed using Apitron PDF Kit for .NET"));
doc.Write(stream,new ResourceManager());
}
// save
Sign(fileName, "../../data/certs/JohnDoe.pfx", "password", "../../data/images/signatureImage.png", new Boundary(10, 750, 110, 800));
}
// Implementation of Sign Method
private static void Sign(string pathToDocument, string pathToCertificate, string password, string pathToSignatureImage, Boundary signatureViewLocation)
{
// open existing document and sign once
using (Stream inputStream = new FileStream(pathToDocument, FileMode.Open, FileAccess.ReadWrite))
{
using (FixedDocument doc = new FixedDocument(inputStream))
{
string imageResourceId = Guid.NewGuid().ToString("N");
string signatureFieldId = Guid.NewGuid().ToString("N");
// register signature image resource
doc.ResourceManager.RegisterResource(new Image(imageResourceId, pathToSignatureImage));
// create first signature field and initialize it using a stored certificate
SignatureField signatureField = new SignatureField(signatureFieldId);
using (Stream signatureDataStream = File.OpenRead(pathToCertificate))
{
signatureField.Signature = Signature.Create(new Pkcs12Store(signatureDataStream,password));
}
// add signature fields to the document
doc.AcroForm.Fields.Add(signatureField);
// create first signature view using the image resource
SignatureFieldView signatureView = new SignatureFieldView(signatureField, signatureViewLocation);
signatureView.ViewSettings.Graphic = Graphic.Image;
signatureView.ViewSettings.GraphicResourceID = imageResourceId;
signatureView.ViewSettings.Description = Description.None;
// add views to page annotations collection
doc.Pages [0].Annotations.Add(signatureView);
// save as incremental update
doc.Save();
}
}
}
static void Main(string [] args)
{
string fileName = "signedTwice.pdf";
using (Stream stream = File.Create(fileName))
{
FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};
doc.Add(new TextBlock("Signed using Apitron PDF Kit for .NET"));
doc.Write(stream,new ResourceManager());
}
// save
Sign(fileName, "../../data/certs/JohnDoe.pfx", "password", "../../data/images/signatureImage.png", new Boundary(10, 750, 110, 800));
}
// Implementation of Sign Method
private static void Sign(string pathToDocument, string pathToCertificate, string password, string pathToSignatureImage, Boundary signatureViewLocation)
{
// open existing document and sign once
using (Stream inputStream = new FileStream(pathToDocument, FileMode.Open, FileAccess.ReadWrite))
{
using (FixedDocument doc = new FixedDocument(inputStream))
{
string imageResourceId = Guid.NewGuid().ToString("N");
string signatureFieldId = Guid.NewGuid().ToString("N");
// register signature image resource
doc.ResourceManager.RegisterResource(new Image(imageResourceId, pathToSignatureImage));
// create first signature field and initialize it using a stored certificate
SignatureField signatureField = new SignatureField(signatureFieldId);
using (Stream signatureDataStream = File.OpenRead(pathToCertificate))
{
signatureField.Signature = Signature.Create(new Pkcs12Store(signatureDataStream,password));
}
// add signature fields to the document
doc.AcroForm.Fields.Add(signatureField);
// create first signature view using the image resource
SignatureFieldView signatureView = new SignatureFieldView(signatureField, signatureViewLocation);
signatureView.ViewSettings.Graphic = Graphic.Image;
signatureView.ViewSettings.GraphicResourceID = imageResourceId;
signatureView.ViewSettings.Description = Description.None;
// add views to page annotations collection
doc.Pages [0].Annotations.Add(signatureView);
// save as incremental update
doc.Save();
}
}
}
Shared Sub Main(ByVal args() As String)
Dim fileName As String = "signedTwice.pdf"
Using stream As Stream = File.Create(fileName)
Dim doc As New FlowDocument() With {.Margin = New Thickness(10)}
doc.Add(New TextBlock("Signed using Apitron PDF Kit for .NET"))
doc.Write(stream,New ResourceManager())
End Using
' save
Sign(fileName, "../../data/certs/JohnDoe.pfx", "password", "../../data/images/signatureImage.png", New Boundary(10, 750, 110, 800))
End Sub
' Implementation of Sign Method
Private Shared Sub Sign(ByVal pathToDocument As String, ByVal pathToCertificate As String, ByVal password As String, ByVal pathToSignatureImage As String, ByVal signatureViewLocation As Boundary)
' open existing document and sign once
Using inputStream As Stream = New FileStream(pathToDocument, FileMode.Open, FileAccess.ReadWrite)
Using doc As New FixedDocument(inputStream)
Dim imageResourceId As String = Guid.NewGuid().ToString("N")
Dim signatureFieldId As String = Guid.NewGuid().ToString("N")
' register signature image resource
doc.ResourceManager.RegisterResource(New Image(imageResourceId, pathToSignatureImage))
' create first signature field and initialize it using a stored certificate
Dim signatureField As New SignatureField(signatureFieldId)
Using signatureDataStream As Stream = File.OpenRead(pathToCertificate)
signatureField.Signature = Signature.Create(New Pkcs12Store(signatureDataStream,password))
End Using
' add signature fields to the document
doc.AcroForm.Fields.Add(signatureField)
' create first signature view using the image resource
Dim signatureView As New SignatureFieldView(signatureField, signatureViewLocation)
signatureView.ViewSettings.Graphic = Graphic.Image
signatureView.ViewSettings.GraphicResourceID = imageResourceId
signatureView.ViewSettings.Description = Description.None
' add views to page annotations collection
doc.Pages (0).Annotations.Add(signatureView)
' save as incremental update
doc.Save()
End Using
End Using
End Sub
两个库的输出结果基本相同。你可以清楚地看到,IronPDF 在处理 PDF 内容方面更简单、更方便。输出的 PDF 上印有证书和签名。
IronPDF 是一个 C# 库,可免费用于开发,也可随时授权用于商业用途。面向单个开发人员、机构和跨国组织的项目许可,以及 SaaS 和 OEM 再分发许可均可获得。所有许可证均提供 30 天退款保证、一年支持和升级、开发/分期/生产有效性以及永久许可证。 (一次性购买).
精简版软件包的价格为 $749,无经常性费用。有关选择最佳许可证的更多详情和帮助,请访问 产品许可页面.
目前使用的许可方案有两种--现代和传统。如果您已经拥有许可证,那么传统许可证方案将一直有效,直到您的许可证到期。之后,您必须联系客户支持部门进行过渡。现代许可证方案仅适用于新客户。
根据现代计划,许可分为三个等级:
有关购买的详细信息,您可以 联系 Apitron 销售代表.
IronPDF 库可从 HTML、JS、CSS、JPG、PNG、GIF 和 SVG 等文档类型创建像素完美的 PDF。Chromium 引擎可帮助渲染 HTML 文件或 URL,从而输出完美的 PDF。
Apitron 可让您使用固定布局 API 创建 PDF,其实现与 PDF 规范 100% 兼容。作为一种类似于 HTML 和 CSS 的样式驱动内容生成方法,该固定布局 API 可让您在几分钟内创建出令人惊叹的报告、账单、目录等。它还支持创建 XML 模板。
Apitron PDF 的许可价格不在其网站上提供。您必须联系支持人员,根据您的使用情况获得报价。另一方面,IronPDF 在其网站上显示了非常清晰的许可套餐。这使得 IronPDF 从众多竞争对手中脱颖而出,让用户可以根据自己的需求做出明智的选择。
与 Apitron PDF 相比,使用 IronPDF 可以更轻松地处理 PDF 文件。IronPDF 允许用户编写更少行的代码来完成具有挑战性的 PDF 工作。IronPDF 提供多种方法从多种文件类型生成 PDF,而 Apitron 只能将 XML 文档转换为 PDF 文档。与 Apitron 相比,IronPDF 无需添加任何技术选项即可实现完美输出。
所有 Iron Software 客户均可选择以购买两套产品的价格购买公司的全部五套产品。试用 免费试用 30 天 测试其全部功能。购买全套 Iron 软件产品 这里.