Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In C#, working with PDF files can be a challenge for developers. There are many factors to consider while creating content for such files, and even more so when converting content from different formats to PDF. Automation is now the key to software development processes. These issues have now been resolved with the construction of libraries designed to help read, write, create, and modify PDF documents, as well as to edit their attributes and to convert them from other formats.
Two PDF libraries will be compared for .NET and .NET Core in this blog post. These two libraries are:
IronPDF and Apitron PDF Kit are two libraries that provide PDF-manipulation functions for .NET and .NET Core. They can both be used for document fields conversion and manipulation tasks. They both can also create bookmarks. The next question is deciding which C# PDF .NET library is best for our .NET projects. This comparison article will help you decide between these two PDF libraries.
First, let's take a look at what the two libraries have to offer, and then move on to the comparison itself.
Apitron PDF Kit is a .NET component that allows you to manipulate or convert PDF files in whatever way you want. You can add images, drawings and texts, sign documents, and much more. You can also edit existing content. The Apitron PDF Kit .NET component can be used to create mobile, desktop, web, Windows, and Cloud applications across multiple platforms.
Using this PDF SDK, you'll be able to:
Developers, especially C# programmers, will adore the IronPDF .NET library. With this amazing tool, you can construct a .NET Core PDF processing application easily.
IronPDF uses the .NET Chromium engine to change HTML pages (in code or in URL form) to PDF files. There is absolutely no need to use complex APIs to position navigation objects or to design PDFs from HTML. Standard web documents are supported by IronPDF, including HTML, ASPX, JS, CSS, and pictures.
IronPDF can be created using HTML5, CSS, JS, and images. A PDF can be easily edited, stamped, and added with headers and footers. It also makes reading PDF text and extracting graphics a breeze.
Standout features of IronPDF include:
IronPDF is available in almost all operating systems and frameworks compatible with C#, including the following:
The rest of the article goes as follows:
There are four methods to download and install the IronPDF library. These are as follows:
To install IronPDF, simply right-click on your project in Solution Explorer. This will open NuGet Package Manager.
Once selected, browse for the IronPDF package and install it, as shown in the screenshot below.
You can also install IronPDF through the Developer Command Prompt.
Install-Package IronPdf
IronPDF can also be installed by visiting the IronPDF NuGet Package site directly and downloading the package. The steps are:
IronPDF is downloaded and is ready to use. However, before that, we should install the Apitron PDF library.
For .NET Core applications, we can install Apitron using NuGet Package Manager, or we can download and install Apitron from the Apitron PDF Kit NuGet website directly.
You can also use the NuGet Package Manager Console to install Apitron. Follow the "Use Developer Command Prompt" step as mentioned in IronPDF and type the following command:
Install-Package Apitron.PDF.Kit
Press ENTER. It will download and install Apitron.PDF.Kit.
Add the following namespaces:
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
Now, let's move into the comparison between IronPDF and Apitron PDF.
IronPDF has multiple methods for creating PDFs. Let's have a look at two important ones.
IronPDF makes it easy to convert any URL to PDF. The following code helps convert URL to 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")
The below code explains how to render a PDF document from an HTML string. You can use just HTML, or combine it with CSS, pictures, and 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")
The output is as follows:
Apitron PDF does not provide facility to convert a URL or HTML to PDF. It allows converting PDF to HTML. However, it also converts XML file to PDF. The XML file used is:
<?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>
The code for converting this XML template to PDF is as follows:
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
The output is as follows:
If we compare the output of IronPDF and Apitron, we can clearly see that IronPDF creates stunning documents using HTML rendering and without even scaling the image size. On the other hand, Apitron gives a similar output to IronPDF but only if the XML file has registered styles.
Adding header and footers is fairly easy with IronPDF. IronPDF allows you to add page numbers and page breaks, attach a cover page, margin etc.
The code for adding headers and footers is below:
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")
The output is as follows:
Apitron PDF Kit creates PDFs by converting content in an XML structure format into a PDF. In Apitron PDF Fit, headers and Footers can be added using the PageHeader
and PageFooter
properties. The code for doing this is as follows:
// 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")
The output is as follows:
One of the most important PDF processing features is the ability to sign a PDF document digitally. IronPDF provides all the tools necessary to do this.
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")
Stamping a signature to a PDF using Apitron PDF Kit is both a lengthy and technical process.
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
The output is pretty much the same for the libraries. You can clearly see that IronPDF is easier and more convenient for manipulating PDF content. The output PDF is signed with the certificate and signature printed on it.
IronPDF is a C# library that is free to use for development and can be licensed for commercial use any time. Project licenses for single developers, agencies, and multinational organizations, as well for as SaaS and OEM redistribution, are all accessible. All licenses provide a 30 day money back guarantee, one year support and upgrades, development/staging/production validity, and a perpetual license (one-time purchase).
The Lite package is available for $749 with no recurring expenses. More details and assistance for choosing the best license are available on the IronPDF product licensing page.
There are two licensing schemes currently in use - Modern and Legacy. If you already have a license, then the legacy license scheme remains active for you until your license expires. Afterward, you have to contact customer support for the transition. The modern licensing scheme applies to you only if you are a new customer.
Under the modern scheme, there are three licensing tiers:
For further details regarding the purchase, you can contact the Apitron Sales Representative.
The IronPDF library creates pixel-perfect PDFs from document types like HTML, JS, CSS, JPG, PNG, GIF, and SVG. The Chromium engine helps to render the HTML files or URLs to give perfect PDFs as output.
Apitron lets you create PDF using a Fixed layout API, implemented to be 100% PDF specification compatible. As a style-driven content generation method that is similar to HTML and CSS, this fixed-layout API provides you with the ability to create stunning reports, bills, catalogues and more in minutes. It also supports the creation of XML templates.
Licensing prices for Apitron PDF are not available on their website. You have to contact support and get a quote based on your usage. On the other hand, IronPDF has a very clear licensing package displayed on their website. This makes IronPDF stand out from its competitors and allows users to choose wisely according to their needs.
With IronPDF, you can work with PDF files more easily than with Apitron PDF. IronPDF allows its users to write fewer lines of code for challenging PDF jobs. IronPDF provides multiple methods to produce PDFs from multiple file types, while Apitron can only convert XML documents to PDF documents. A perfect output is achieved without adding any technical options in IronPDF as compared to Apitron.
All Iron Software customers have the option of purchasing all five of the company's products for the price of two of them. Try the free, 30-day trial of IronPDF to test its full functionality. Purchase the full suite of Iron Software products at IronSoftware.com.
9 .NET API products for your office documents