Published September 27, 2021
Aspose PDF Converter Tutorial and Comparison
In this article I will examine how to work with the Aspose PDF converter in .NET by comparing its programming structure, provided functions, licensing and pricing packages. Follow the step-by-step tutorials and use the code examples to see for yourself which library is best for working with PDFs in your C# project.
Convert your webpages into PDF documents quickly and easily using the IronPDF library for C# and Java. IronPDF supports all forms of HTML content and renders them with pixel-perfect precision. No fuss. No stress. This library just simply works.
Read more about IronPDF for C# below; refer to the product webpage for more information about IronPDF for Java.
Compare Aspose PDF Functions
- Compare Pricing & Licensing options
- Convert HTML String to PDF
- Convert HTML File to PDF document
- Add headers, footers, and styling
- Add a watermark and more functions
About Aspose PDF API
Aspose API provider is a file format component provider company. The specialized component Aspose PDF DLL is used to deal with PDF files. It allows developers to create and manipulate PDF files programmatically. You can convert PDF file to different file formats. We can also use it to add Watermarks, Text Stamps, Bookmarks, compress PDF files, digitally sign PDF documents, convert PDF documents, print PDF documents, decrypt PDF documents and manage PDF forms in PDF document without using Adobe Acrobat.
About IronPDF
IronPDF C# Library is provided by Iron Software for generating, editing, and manipulating PDF files in .NET projects. It is known well because it requires only a few lines of code with a simple programming structure to get the required PDF files. By using it, we can easily generate PDF files from many sources (such as HTML String, HTML Files, or URLs), and then set its properties (such as Page Number, Headers, Page Margins or Watermarks and more). PDF processing capabilities of IronPDF are at extreme level. It supports custom font handling. To add PDF processing capabilities in a Java application, IronPDF is the best choice.
IronPDF is also available for Java.
1. Aspose.PDF Licensing and Pricing
When we need to purchase APIs for our project, pricing and licensing packages can play a very important role in the selection.
Here we compare the main 4 perpetual licenses provided by Aspose PDF to their IronPDF counterparts.
License Coverage | IronPDF | Aspose.PDF |
---|---|---|
# of License Options | 3 customizable license options for your needs | 4 licenses available for general needs |
1 developer, 1 project, 1 location | $749 with 1 yr free support | No license |
1 developer, unlimited projects, 1 location | No license | $1199 with standard suport or $1598 with paid support |
1 developer, unlimited projects, unlimited locations | No license | $3,597 with standard support or $5,394 with paid support |
Up to 10 developers, 10 projects, up to 10 locations | $999 with 1 yr free support | $5,995 with standard suport or $9,990 with paid support |
1 SaaS/OEM product, 10 developers, 10 locations | $1,599 with 1 yr free support | No license |
Unlimited SaaS/OEM products, unlimited developers, unlimited locations | $3,999 with 1 yr free support | $13,986 with standard suport or $21,985 with paid support |
Purchase a license | IronPDF Licenses | Aspose PDF Licenses |
While Aspose offers multiple tiers of support pricing, IronPDF offers more affordable licenses, customizable options, and free support for all coverage.
Aspose PDF Supported Platforms
Let's look at the supported framework and platforms of Aspose.PDF.
Aspose PDF Supported Frameworks
- .NET Framework 2.0 or above
- .NET Core 2.0
- .NET Core 2.1
Aspose PDF Supported Platforms
- Windows
- macOS
- Linux
- Azure
Aspose PDF Dependencies
If we want to use the Aspose.PDF component in our project, then we need to make sure that we fulfill the dependencies in our Visual Studio Project:
Aspose PDF for .NET Framework 4.0
- No dependencies.
Aspose PDF For .NET Standard 2.0
Microsoft.Extensions.DependencyModel
(>= 2.0.4)System.Diagnostics.PerformanceCounter
(>= 4.5.0)System.Drawing.Common
(>= 4.7.0)System.Reflection.Emit
(>= 4.7.0)System.Security,Cryptography.Pkcs
(>= 4.7.0)System.Security.Permissions
(>= 4.5.0)System.Text.Encoding.CodePages
(>= 4.5.0)
Aspose PDF For .NET Standard 2.1
Microsoft.Extensions.DependencyModel
(>= 2.0.4)System.Diagnostics.PerformanceCounter
(>= 4.5.0)System.Drawing.Common
(>= 4.7.0)System.Security.Cryptography.Pkcs
(>= 4.7.0)System.Security.Permissions
(>= 4.5.0)System.Text.Encoding.CodePages
(>= 4.5.0)
IronPDF Supported Platforms
.NET framework 4.0 or above for:
- Windows
- Azure
.NET Standard 2.0 & Core 2, 3 & 5:
- Windows
- macOS
- Linux
- Azure
Cloud hosting services included:
- Microsoft Azure
- Docker
- AWS
IronPDF can be used with:
- ASP.NET MVC Application
- ASP.NET Web Forms Application
- Console Application
- Cloud Application
- Service
- Function
- Desktop Application.
IronPDF Dependencies
IronPDF for .NET Framework 4.0
Iron.Assets.Rendering.Windows
(>= 20.0.0)
IronPDF for .NET Standard 2.0 or above
Iron.Assets.Rendering.Universal
(>= 20.0.0)System.Drawing.Common
(>= 4.7.0)
Step 1 Installation
Aspose PDF Download
There are two possible ways that we can use Aspose.PDF components in our project, and there is no difference whichever one you choose.
Aspose.PDF Download via NuGet Package
Open the NuGet Package Manager of your project in Visual Studio and browse Aspose.Pdf
, then install it.
Install-Package Aspose.Pdf
Install with Aspose.PDF.dll
You can also download Aspose.PDF.dll and extract the downloaded file. It will create some folder and files.
- Go to Bin folder
- Go to the required .NET Framework folder, which includes Aspose.PDF.dll.
- Add the reference of this file in your Visual Studio project.
Now, you can use Aspose.PDF functions and classes using Aspose.Pdf
namespace.
Install IronPDF
We can also install IronPDF in our project using two different ways.
Install IronPDF with NuGet
The easiest technique is to add IronPDF in the project.
- Go to NuGet Package Manager in Visual Studio project and browse IronPDF
- Install IronPDF
Install-Package IronPdf
Download IronPDF.dll
Another option is to download IronPDF.dll, then add its reference to the project. After this, we can use IronPDF classes and provided functions from the using IronPdf
namespace.
2. HTML String to PDF
Let's take the first major use case: that we want to create a PDF file by HTML string and save it in the target location. Let's compare the code required and start with IronPDF.
2.1. IronPDF Convert HTML String to PDF
var converter = new IronPdf.ChromePdfRenderer();
string HTML = "<h1>Hello IronPDF!</h1> <h2>Welcome to PDF File</h2> ";
using var PDF = converter.RenderHtmlAsPdf(HTML);
PDF.SaveAs("Sample.pdf");
var converter = new IronPdf.ChromePdfRenderer();
string HTML = "<h1>Hello IronPDF!</h1> <h2>Welcome to PDF File</h2> ";
using var PDF = converter.RenderHtmlAsPdf(HTML);
PDF.SaveAs("Sample.pdf");
Dim converter = New IronPdf.ChromePdfRenderer()
Dim HTML As String = "<h1>Hello IronPDF!</h1> <h2>Welcome to PDF File</h2> "
Dim PDF = converter.RenderHtmlAsPdf(HTML)
PDF.SaveAs("Sample.pdf")
The above code will create a PDF file Sample.pdf and save it in the target location.
Note
- If you do not give the full path and just give the file name as we do in the above example, then the PDF file will be created in the bin\Debug folder of your project.
- In the case that you want to generate the Sample.pdf file in Local Disk E then you can write:
PDF.SaveAs("E:\\Sample.pdf");
Read more about how to create PDF from HTML and working with PDF files using IronPDF.
2.2. Aspose PDF Convert HTML String to PDF
using Aspose.Pdf;
static void Main(string[] args)
{
//Create new document object
Document doc = new Document();
//Add a page in document object
Page page = doc.Pages.Add();
//Generate HTML String
string HtmlStr = "<h1>Hello Aspose.PDF!</h1> <h2>Welcome to PDF File</h2> ";
//Create html fragment by above HTML string
HtmlFragment HtmlFrg= new HtmlFragment(HtmlStr);
//Add html fragment as paragraph in document page
page.Paragraphs.Add(HtmlFrg);
//Save the file on target location
doc.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
using Aspose.Pdf;
static void Main(string[] args)
{
//Create new document object
Document doc = new Document();
//Add a page in document object
Page page = doc.Pages.Add();
//Generate HTML String
string HtmlStr = "<h1>Hello Aspose.PDF!</h1> <h2>Welcome to PDF File</h2> ";
//Create html fragment by above HTML string
HtmlFragment HtmlFrg= new HtmlFragment(HtmlStr);
//Add html fragment as paragraph in document page
page.Paragraphs.Add(HtmlFrg);
//Save the file on target location
doc.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
Imports Aspose.Pdf
Shared Sub Main(ByVal args() As String)
'Create new document object
Dim doc As New Document()
'Add a page in document object
Dim page As Page = doc.Pages.Add()
'Generate HTML String
Dim HtmlStr As String = "<h1>Hello Aspose.PDF!</h1> <h2>Welcome to PDF File</h2> "
'Create html fragment by above HTML string
Dim HtmlFrg As New HtmlFragment(HtmlStr)
'Add html fragment as paragraph in document page
page.Paragraphs.Add(HtmlFrg)
'Save the file on target location
doc.Save("Sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
The above code will create a new PDF file Sample.pdf, just like we created using IronPDF.
2.3. Difference between Aspose and IronPDF
Using Aspose.PDF, we first need to create a new Page, HtmlFragment
, and add it into the page paragraph, and then save it to the target location. IronPDF has no such type of coding structure and provides a way with many fewer lines of code.
Read more about how to Convert PDF with Aspose PDF DLL and working with Aspose.PDF in .NET applications.
3. HTML File to PDF
Now, let's take another use case: create a PDF file from an existing HTML file.
Note: Assume that we have an HTML file MyHtmlFile.html which exists in the bin\Debug folder of the project, and we want to create a PDF file from it.
3.1. IronPDF Convert HTML File to PDF
var converter = new IronPdf.ChromePdfRenderer();
using var PDF = converter.RenderHTMLFileAsPdf("MyHtmlFile.html");
PDF.SaveAs("Sample.pdf");
var converter = new IronPdf.ChromePdfRenderer();
using var PDF = converter.RenderHTMLFileAsPdf("MyHtmlFile.html");
PDF.SaveAs("Sample.pdf");
Dim converter = New IronPdf.ChromePdfRenderer()
Dim PDF = converter.RenderHTMLFileAsPdf("MyHtmlFile.html")
PDF.SaveAs("Sample.pdf")
The above code will create a new PDF file Sample.pdf from the MyHtmlFile.html file.
3.2. Aspose PDF Convert HTML File to PDF
using Aspose.Pdf;
static void Main(string[] args)
{
//create HTML load option object
HtmlLoadOptions options = new HtmlLoadOptions();
//load HTML file in document object
Document pdfDocument = new Document("MyHtmlFile.html", options);
//save to the target location
pdfDocument.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
using Aspose.Pdf;
static void Main(string[] args)
{
//create HTML load option object
HtmlLoadOptions options = new HtmlLoadOptions();
//load HTML file in document object
Document pdfDocument = new Document("MyHtmlFile.html", options);
//save to the target location
pdfDocument.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
Imports Aspose.Pdf
Shared Sub Main(ByVal args() As String)
'create HTML load option object
Dim options As New HtmlLoadOptions()
'load HTML file in document object
Dim pdfDocument As New Document("MyHtmlFile.html", options)
'save to the target location
pdfDocument.Save("Sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
The above code will also create a new PDF file Sample.pdf from MyHtmlFile.html, but here we need to specify HtmlLoadOptions
.
Another Comparison of IronPDF and Aspose.PDF
This is the practical URL to PDF conversion comparison using C# .NET APIs IronPDF and Aspose.PDF.
IronPDF
Aspose.PDF
Comparison
Aspose.PDF for .NET is a library used to manipulate PDFs and manage conversions to, and from, PDF to different formats. Aspose.PDF is a similar product to IronPDF, but it has fewer features. In this article we convert a website named medium.com using both IronPDF and Aspose.PDF. Above are the screenshots of the output of both APIs.
Interface design
IronPDF made a nearly pixel-perfect copy of the webpage and rendered all the sidebars and images with high accuracy. On the other hand Aspose.PDF did not perform well based on the fact it left details and images out of the output file.
CSS and JavaScript
IronPDF utilized and rendered all the CSS and JavaScript of the webpage without error. To compare, we found Aspose.PDF misses practically all the medium.com webpage's CSS and JavaScript files. Aspose.PDF was unable to make icons and images appear accurately.
Navigation and Side Bars
Aspose.PDF does not render navigation bars, or sidebars, as shown in the above images. On the other side IronPDF rendered these clearly and did a great job showcasing its abilities to convert URL to HTML.
Images
In this step we review the accuracy of image placement. As we all know, placing images incorrectly can change the meaning of a webpage; and in some cases make the whole purpose of an exercise like this pointless if the result is not accurate. The verdict: Images are in the wrong places in the output generated by Aspose.PDF. In contrast, IronPDF places images with great accuracy.
Conclusion
Aspose.PDF takes nearly 10 times more time than IronPDF but still creates a very low quality PDF from the URL. The Aspose.PDF rendering engine is too slow and of low quality. IronPDF creates high quality PDF from URL in just a few seconds.
4. Work with Headers and Footers
It is a common requirement that we need to add Headers and Footers on PDF pages for different purposes such as including the document name, page numbers, references, date and time, or other similar functions. Both components provide different types of functions to accomplish these:
4.1. IronPDF Headers and Footers Functions
IronPDF has the following properties by which we can easily set Headers and Footers of newly created PDF file pages.
LeftText
to write text on the left side of the Header or Footer.RightText
to write text on the right side of the Header or Footer.CenterText
to write text in the center of the Header or Footer.FontSize
to specify the font size of the text of the Header of Footer.FontFamily
to specify the font family of the Header or Footer.DrawDividerLine
to draw a line that separates page content and the Header or Footer.- Spacing to specify the space between page content and the Header or Footer.
The following example showcases how to use the above properties.
/**
Header Footer Style Functions
anchor-ironpdf-headers-and-footers-functions
**/
using IronPdf;
static void Main(string[] args)
{
//Create HTML to PDF converter
var converter = new IronPdf.ChromePdfRenderer();
//Add Header
converter.RenderingOptions.TextHeader = new TextHeaderFooter()
{
LeftText = "Header Text",
RightText = "{date} {time}",
};
//Add Footer
converter.RenderingOptions.TextFooter = new TextHeaderFooter()
{
RightText = "Footer Text"
};
//Add Page Content
using var PDF = converter.RenderHtmlAsPdf("<h1>Page Content</h2>");
//save to target location
PDF.SaveAs("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
/**
Header Footer Style Functions
anchor-ironpdf-headers-and-footers-functions
**/
using IronPdf;
static void Main(string[] args)
{
//Create HTML to PDF converter
var converter = new IronPdf.ChromePdfRenderer();
//Add Header
converter.RenderingOptions.TextHeader = new TextHeaderFooter()
{
LeftText = "Header Text",
RightText = "{date} {time}",
};
//Add Footer
converter.RenderingOptions.TextFooter = new TextHeaderFooter()
{
RightText = "Footer Text"
};
//Add Page Content
using var PDF = converter.RenderHtmlAsPdf("<h1>Page Content</h2>");
//save to target location
PDF.SaveAs("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
'''
'''Header Footer Style Functions
'''anchor-ironpdf-headers-and-footers-functions
'''*
Imports IronPdf
Shared Sub Main(ByVal args() As String)
'Create HTML to PDF converter
Dim converter = New IronPdf.ChromePdfRenderer()
'Add Header
converter.RenderingOptions.TextHeader = New TextHeaderFooter() With {
.LeftText = "Header Text",
.RightText = "{date} {time}"
}
'Add Footer
converter.RenderingOptions.TextFooter = New TextHeaderFooter() With {.RightText = "Footer Text"}
'Add Page Content
Dim PDF = converter.RenderHtmlAsPdf("<h1>Page Content</h2>")
'save to target location
PDF.SaveAs("Sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
The above code will create a PDF file, Sample.pdf, and print Header Text on the left side of the Header, Date Time on the right side of the Header, and Footer Text on the right side of the Footer.
Read more about how to Add Header and Footers in an HTML to PDF conversion with full list of functions provided by IronPDF.
4.2. Aspose PDF Headers and Footers Functions
To deal with Headers and Footers, Aspose.PDF introduced the TextStamp
class. By using it, we can easily add Headers and Footers on PDF file pages and set their properties. There are many subclasses of TextStamp
, but here we will discuss the basic three classes and their properties which are commonly used in our daily work.
VerticalAlignment
(used to set the vertical alignment of TextStamp
and has the following properties)
_None_
for no vertical alignment._Top_
sets the vertical alignment to the top._Center_
sets vertical alignment in the center._Bottom_
sets vertical alignment the bottom.HorizontalAlignment
(used to set the horizontal alignment ofTextStamp
and has the following properties)_None_
for no horizontal alignment._Left_
sets horizontal alignment to left._Right_
sets horizontal alignment to right._Center_
sets horizontal alignment in the center._Justify_
Text will be aligned on both left and right margins._FullJustify_
all lines will be left-aligned and right-alignedTextState
(used to set the properties of the text, and has some basic properties)_Font_
sets the font of the text_FontStyles_
sets the font style of the text._FontSize_
sets the font size of the text._Color_
sets the color of the text._Underline_
sets the underline for the text._StrikeOut_
sets the strikeout style for the text.
See the following example for how to use the above properties to set Header and Footer.
using Aspose.Pdf;
static void Main(string[] args)
{
Document doc = new Document();
Page page = doc.Pages.Add();
HtmlFragment html = new HtmlFragment("<h1>Page Content</h2>");
page.Paragraphs.Add(html);
//Add Header text
TextStamp title = new TextStamp("Header Text");
title.VerticalAlignment = VerticalAlignment.Top;
title.HorizontalAlignment = HorizontalAlignment.Left;
title.TextState.FontSize = 20;
doc.Pages[1].AddStamp(title);
//Add Header Datetime
TextStamp datetime = new TextStamp(System.DateTime.Now.ToString());
datetime.VerticalAlignment = VerticalAlignment.Top;
datetime.HorizontalAlignment = HorizontalAlignment.Right;
datetime.TextState.FontSize = 20;
doc.Pages[1].AddStamp(datetime);
//Add Footer text
TextStamp footer = new TextStamp("Footer Text");
footer.VerticalAlignment = VerticalAlignment.Bottom;
footer.HorizontalAlignment = HorizontalAlignment.Right;
footer.TextState.FontSize = 20;
doc.Pages[1].AddStamp(footer);
doc.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
using Aspose.Pdf;
static void Main(string[] args)
{
Document doc = new Document();
Page page = doc.Pages.Add();
HtmlFragment html = new HtmlFragment("<h1>Page Content</h2>");
page.Paragraphs.Add(html);
//Add Header text
TextStamp title = new TextStamp("Header Text");
title.VerticalAlignment = VerticalAlignment.Top;
title.HorizontalAlignment = HorizontalAlignment.Left;
title.TextState.FontSize = 20;
doc.Pages[1].AddStamp(title);
//Add Header Datetime
TextStamp datetime = new TextStamp(System.DateTime.Now.ToString());
datetime.VerticalAlignment = VerticalAlignment.Top;
datetime.HorizontalAlignment = HorizontalAlignment.Right;
datetime.TextState.FontSize = 20;
doc.Pages[1].AddStamp(datetime);
//Add Footer text
TextStamp footer = new TextStamp("Footer Text");
footer.VerticalAlignment = VerticalAlignment.Bottom;
footer.HorizontalAlignment = HorizontalAlignment.Right;
footer.TextState.FontSize = 20;
doc.Pages[1].AddStamp(footer);
doc.Save("Sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
Imports System
Imports Aspose.Pdf
Shared Sub Main(ByVal args() As String)
Dim doc As New Document()
Dim page As Page = doc.Pages.Add()
Dim html As New HtmlFragment("<h1>Page Content</h2>")
page.Paragraphs.Add(html)
'Add Header text
Dim title As New TextStamp("Header Text")
title.VerticalAlignment = VerticalAlignment.Top
title.HorizontalAlignment = HorizontalAlignment.Left
title.TextState.FontSize = 20
doc.Pages(1).AddStamp(title)
'Add Header Datetime
Dim datetime As New TextStamp(DateTime.Now.ToString())
datetime.VerticalAlignment = VerticalAlignment.Top
datetime.HorizontalAlignment = HorizontalAlignment.Right
datetime.TextState.FontSize = 20
doc.Pages(1).AddStamp(datetime)
'Add Footer text
Dim footer As New TextStamp("Footer Text")
footer.VerticalAlignment = VerticalAlignment.Bottom
footer.HorizontalAlignment = HorizontalAlignment.Right
footer.TextState.FontSize = 20
doc.Pages(1).AddStamp(footer)
doc.Save("Sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
This will create the same PDF file Sample.pdf as we created using IronPDF: Header Text on the left side of the Header, Date Time on the right side of the Header and Footer Text on the right side of page bottom.
You can compare the ease of working with IronPDF and their simple functions to the list provided by Aspose.
Read more about using Aspose Manage Header and Footer of PDF File.
5. Add Page Numbers to PDF File
Let's suppose that we want to create a PDF file from an HTML string, and then add a page number to the Header of the page.
5.1. IronPDF Add Page Numbers to PDF File
var converter = new IronPdf.ChromePdfRenderer();
converter.RenderingOptions.TextHeader = new TextHeaderFooter()
{
RightText = "Page {page} of {total-pages}",
};
string html = "<h1 style='text-align:center'>Welcome To IronPdf</h1>";
using var PDF = converter.RenderHtmlAsPdf(html);
PDF.SaveAs("Sample.pdf");
var converter = new IronPdf.ChromePdfRenderer();
converter.RenderingOptions.TextHeader = new TextHeaderFooter()
{
RightText = "Page {page} of {total-pages}",
};
string html = "<h1 style='text-align:center'>Welcome To IronPdf</h1>";
using var PDF = converter.RenderHtmlAsPdf(html);
PDF.SaveAs("Sample.pdf");
Dim converter = New IronPdf.ChromePdfRenderer()
converter.RenderingOptions.TextHeader = New TextHeaderFooter() With {.RightText = "Page {page} of {total-pages}"}
Dim html As String = "<h1 style='text-align:center'>Welcome To IronPdf</h1>"
Dim PDF = converter.RenderHtmlAsPdf(html)
PDF.SaveAs("Sample.pdf")
The above code will create a PDF file Sample.pdf, insert the HTML data, and add the Page Number in the page Header.
Output: Here's a screenshot of our newly created sample.pdf file.
5.2. Aspose PDF Add Page Numbers to PDF File
Aspose.PDF does not provide the predefined function to set Headers and Footers, unlike IronPDF. Using Aspose.PDF, we would need to work with PageNumberStamp
and set its alignment for the Header and Footer on the PDF page. See the example below.
using Aspose.Pdf;
static void Main(string[] args)
{
//Create new document
Document doc = new Document();
//New new page of document
Page page = doc.Pages.Add();
//Source HTML string
string html = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1>";
//Create HTML fragment for source string
HtmlFragment frag = new HtmlFragment(html);
//Add fragment as page paragraph
page.Paragraphs.Add(frag);
//Create PageNumberStamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();
//Setting its txt to print
pageNumberStamp.Format = "Page # of " + doc.Pages.Count;
pageNumberStamp.VerticalAlignment = VerticalAlignment.Top;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right;
// Add PageNumberStamp to page
page.AddStamp(pageNumberStamp);
//Save document to the target location
doc.Save("sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
using Aspose.Pdf;
static void Main(string[] args)
{
//Create new document
Document doc = new Document();
//New new page of document
Page page = doc.Pages.Add();
//Source HTML string
string html = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1>";
//Create HTML fragment for source string
HtmlFragment frag = new HtmlFragment(html);
//Add fragment as page paragraph
page.Paragraphs.Add(frag);
//Create PageNumberStamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();
//Setting its txt to print
pageNumberStamp.Format = "Page # of " + doc.Pages.Count;
pageNumberStamp.VerticalAlignment = VerticalAlignment.Top;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right;
// Add PageNumberStamp to page
page.AddStamp(pageNumberStamp);
//Save document to the target location
doc.Save("sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
Imports Aspose.Pdf
Shared Sub Main(ByVal args() As String)
'Create new document
Dim doc As New Document()
'New new page of document
Dim page As Page = doc.Pages.Add()
'Source HTML string
Dim html As String = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1>"
'Create HTML fragment for source string
Dim frag As New HtmlFragment(html)
'Add fragment as page paragraph
page.Paragraphs.Add(frag)
'Create PageNumberStamp
Dim pageNumberStamp As New PageNumberStamp()
'Setting its txt to print
pageNumberStamp.Format = "Page # of " & doc.Pages.Count
pageNumberStamp.VerticalAlignment = VerticalAlignment.Top
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right
' Add PageNumberStamp to page
page.AddStamp(pageNumberStamp)
'Save document to the target location
doc.Save("sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
Output: Here is the screenshot of newly created sample.pdf file.
6. Add Watermarks
We often need to add Watermarks on our PDF file pages for advertisement, security, or copyright issues. Let's create a PDF file from an HTML string, and then add Watermark using both components.
6.1. IronPDF Add Watermark
To add a watermark on the PDF page, IronPDF provides the WatermarkPage
class.
WatermarkPage
Definition: WatermarkPage (WatermarkText, PageIndex, WaterMarkLocation, Opacity, Rotation, Hyperlink)
;
WatermarkText
= the actual text written in HTML which displays as Watermark text.PageIndex
= write the page number as the Watermark.Opacity
= set the opacity of the Watermark.Rotation
= set the rotation of the Watermark content clockwise.Hyperlink
= hyperlink that the Watermark will link to.WaterMarkLocation
= set the position of the Watermark, which we can set using the following positions:TopLeft
TopCenter
TopRight
MiddleLeft
MiddleCenter
MiddleRight
BottomLeft
BottomCenter
BottomRight
Let's see the following example, where we will understand how to use the above functions in our project.
using IronPdf;
var converter = new IronPdf.ChromePdfRenderer();
string html = "<h1 style='text-align:center'>Welcome To IronPdf</h1><h2 style='text-align:center'>Page Content</h2>";
using var PDF = converter.RenderHtmlAsPdf(html);
//Create string for Watermark
string WMStr = "<h1 style='color:red'>Sample WaterMark</h1>";
//Add Watermark
PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter,100,-45, "");
PDF.SaveAs("sample.pdf");
using IronPdf;
var converter = new IronPdf.ChromePdfRenderer();
string html = "<h1 style='text-align:center'>Welcome To IronPdf</h1><h2 style='text-align:center'>Page Content</h2>";
using var PDF = converter.RenderHtmlAsPdf(html);
//Create string for Watermark
string WMStr = "<h1 style='color:red'>Sample WaterMark</h1>";
//Add Watermark
PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter,100,-45, "");
PDF.SaveAs("sample.pdf");
Imports IronPdf
Private converter = New IronPdf.ChromePdfRenderer()
Private html As String = "<h1 style='text-align:center'>Welcome To IronPdf</h1><h2 style='text-align:center'>Page Content</h2>"
Private PDF = converter.RenderHtmlAsPdf(html)
'Create string for Watermark
Private WMStr As String = "<h1 style='color:red'>Sample WaterMark</h1>"
'Add Watermark
PDF.WatermarkPage(WMStr, 0, PdfDocument.WaterMarkLocation.MiddleCenter,100,-45, "")
PDF.SaveAs("sample.pdf")
Output: Here is the screenshot of the newly created sample.pdf file with watermark.
If we want to add a Watermark on multiple pages, but not all, then we can use the WatermarkPages
function as follows:
WatermarkPages(WaterMark Text, new[] { PageIndex 1, PageIndex 2,...., PageIndex N }, WaterMarkLocation, Opacity, rotation)
WatermarkPages(WaterMark Text, new[] { PageIndex 1, PageIndex 2,...., PageIndex N }, WaterMarkLocation, Opacity, rotation)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WatermarkPages(WaterMark Text, New[] { PageIndex 1, PageIndex 2,...., PageIndex N }, WaterMarkLocation, Opacity, rotation)
If we do want to add a Watermark on all pages, then we can use the WatermarkAllPages
() function like this: WatermarkAllPages(WaterMark Text,WaterMarkLocation, opacity, rotation);
Read more about how to add a watermark to PDFs.
6.2. Aspose PDF Add Watermark
Aspose.PDF does not provide a special function to add text as a Watermark, unlike IronPDF. Instead, we need to use the TextStamp
class. Let's see how that works.
using Aspose.Pdf;
using Aspose.Pdf.Text;
static void Main(string[] args)
{
//create new document
Document pdfDocument = new Document();
//add new page to document
Page page = pdfDocument.Pages.Add();
//source HTML string
string HtmlStr = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1><h2 style='text-align:center'>Page Content</h2>";
//create HTML fragment by source HTML string
HtmlFragment HtmlFrg = new HtmlFragment(HtmlStr);
//add HTML fragment as page paragraph
page.Paragraphs.Add(HtmlFrg);
//Watermark string
string WMStr = "Sample WaterMark";
//create TextStamp of above string
TextStamp textStamp = new TextStamp(WMStr);
//setting TextStamp properties
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Center;
textStamp.RotateAngle = 45;
textStamp.TextState.Font = FontRepository.FindFont("Times New Roman");
textStamp.TextState.FontSize = 15F;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
//add to the specific page
pdfDocument.Pages[1].AddStamp(textStamp);
//save to the target location
pdfDocument.Save("sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
using Aspose.Pdf;
using Aspose.Pdf.Text;
static void Main(string[] args)
{
//create new document
Document pdfDocument = new Document();
//add new page to document
Page page = pdfDocument.Pages.Add();
//source HTML string
string HtmlStr = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1><h2 style='text-align:center'>Page Content</h2>";
//create HTML fragment by source HTML string
HtmlFragment HtmlFrg = new HtmlFragment(HtmlStr);
//add HTML fragment as page paragraph
page.Paragraphs.Add(HtmlFrg);
//Watermark string
string WMStr = "Sample WaterMark";
//create TextStamp of above string
TextStamp textStamp = new TextStamp(WMStr);
//setting TextStamp properties
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Center;
textStamp.RotateAngle = 45;
textStamp.TextState.Font = FontRepository.FindFont("Times New Roman");
textStamp.TextState.FontSize = 15F;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
//add to the specific page
pdfDocument.Pages[1].AddStamp(textStamp);
//save to the target location
pdfDocument.Save("sample.pdf");
Console.WriteLine("PDF File Successfully Created....");
Console.ReadKey();
}
Imports Aspose.Pdf
Imports Aspose.Pdf.Text
Shared Sub Main(ByVal args() As String)
'create new document
Dim pdfDocument As New Document()
'add new page to document
Dim page As Page = pdfDocument.Pages.Add()
'source HTML string
Dim HtmlStr As String = "<h1 style='text-align:center'>Welcome To Aspose.PDF</h1><h2 style='text-align:center'>Page Content</h2>"
'create HTML fragment by source HTML string
Dim HtmlFrg As New HtmlFragment(HtmlStr)
'add HTML fragment as page paragraph
page.Paragraphs.Add(HtmlFrg)
'Watermark string
Dim WMStr As String = "Sample WaterMark"
'create TextStamp of above string
Dim textStamp As New TextStamp(WMStr)
'setting TextStamp properties
textStamp.HorizontalAlignment = HorizontalAlignment.Center
textStamp.VerticalAlignment = VerticalAlignment.Center
textStamp.RotateAngle = 45
textStamp.TextState.Font = FontRepository.FindFont("Times New Roman")
textStamp.TextState.FontSize = 15F
textStamp.TextState.FontStyle = FontStyles.Bold
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)
'add to the specific page
pdfDocument.Pages(1).AddStamp(textStamp)
'save to the target location
pdfDocument.Save("sample.pdf")
Console.WriteLine("PDF File Successfully Created....")
Console.ReadKey()
End Sub
Output: And here is the screenshot of the stamped sample.pdf file.
Read more about using Aspose PDF add stamp and watermark in your files and projects.
6.3. Difference between Aspose and IronPDF Watermarking
When talking about Watermarks on selected pages or multiple pages, there are no specified functions when using Aspose.PDF. But we can do it using page iteration of a PDF file with For Loop, While Loop, or ForEach
Loop etc. This does create bulky code and requires us to add different conditions if we want to add a Watermark only on select pages.
7. Compare License Options
As developers, we all want to complete our projects with minimal cost and resources, and budget can be especially important. Review the chart to compare what license is right for your needs and budget.
IronPDF offers licenses with a customizable # of developers, projects, and locations, so you can meet the needs of your project and pay only for the coverage you need. Check out the HTML to PDF licenses or contact the support team to get a custom quote and free 30 day deployment trial.
8. Aspose PDF and IronPDF Takeaways
IronPDF | Aspose.PDF |
---|---|
Works with PDF files programmatically. | Works with PDF files programmatically. |
Developed in .NET framework using C#. | Developed in .NET framework using C#. |
Supports Windows, Linux, and macOS. | Supports Windows, Linux, and macOS. |
Provides a generalized way to convert .aspx page to PDF file. |
No generalized way to convert .aspx pages to PDF file. |
Provides predefined functions for Header and Footer. | No predefined functions for Header and Footer, need to use TextStamp . |
Can set Watermark styling properties Using HTML and CSS. | No support for HTML and CSS for TextStamp styling properties. |
Easily attach a Cover Page to a PDF with just a few lines of code. | More difficult to add a cover page to a PDF file. |
Licenses start at $749. | Licenses start at $999. |
9. Aspose Components Overview
Aspose provides a range of components to suit your project needs. See the full list below and compare to other IronPDF products for competitive pricing and clear & condensed code functions.
Component | Supports | Usage | IronSoftware Comparison |
---|---|---|---|
Aspose.Words | .NET, Java, C++, Android via Java | Create, edit, convert & print Word files. | |
Aspose.PDF | .NET, Java, C++, Android via Java | Work with PDF files to generate, edit and manipulate the PDFs programmatically. | IronPDF |
Aspose.Cells | .NET, Java, C++, Android via Java, NodeJS via Java, PHP via Java, Python via Java | Create, edit, import, export and convert Excel files into many formats. | IronXL |
Aspose.Email | .NET, Java, C++, Android via Java, Python via .NET | Create, manipulate, convert and send Outlook Emails, and implement.NETwork Protocols such as SMTP, POP3 & IMAP. | |
Aspose.Slides | .NET, .NET Core, Java, C++, Android via Java, Xamarin | Create, manage and convert PowerPoint Presentation and Slides. | |
Aspose.Imaging | .NET, Java | Draw, manipulate, convert, and transform images. | |
Aspose.BarCode | .NET, Java, C++, Android via Java, NodeJS via Java, PHP via Java | Create and scan any type of Barcodes e,g, 1D, 2D, and Postal. | IronBarCode |
Aspose.Diagram | .NET, Java, Node.JS via Java | Work with with Microsoft Visio files to create, edit and convert them on any platform. | |
Aspose.CAD | .NET, Java | Convert AutoCAD, DWG & DXF drawings to PDF & raster images. | |
Aspose.PSD | .NET, Java | Create, edit, convert Adobe Photoshop and Adobe Illustrator files programmatically. |
Tutorial Quick Access
Explore the IronPDF API Reference
Explore the API Reference for IronPDF C# Library, including details of all of IronPDF�s features, classes, method fields, namespaces, and enums.
View the API Reference