Using Spire.PDF In ASP.NET

In this article we are going to see a new product Spire.PDF which helps us to create, manipulate PDF and many more. This product has been introduced by the company E-Iceblue. The company has introduced so many products like this. For example Spire.Doc, Spire.XLS. I hope you have read my articles under these categories. If you have not read them, I suggest you to read here.

Download source code

Using Spire PDF


As you all know a PDF is a most accurate and effective format of a document. A document is very important in our life, so we create PDF files. Now coming to the matter, As we all are developers, we develops anything that is needed. But have you ever tried creating and managing PDF file? Awww!. That’s a quite difficult task right? Still it is possible. “Everything is possible, the word impossible itself says I am possible”. Now It is time to introduce you a product which let you do these task in an easy manner. So that means, you are able to do these task with a little effort.

Create a new Project

Now we will create a new Project in our Visual studio.


Add License Information To Project

I am using evaluation version with one month temporary license. There are free versions also available for spire.pdf with some limitation. You can try that.


Download the files

You can always the needed files from here: Download Spire.PDF

Install Spire.PDF

Now click on the exe file after you extract the downloaded file. The installation will get started then.



So Shall we start?

Once you Installed, you are ready to go. We will start with a “Simple Web Application” . I hope you have a created a new web application and added your license as suggested before. Now create a page.

So your page will be looking as follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UsingSpirePDF.Default" %>


    <html xmlns="">
        <head runat="server">
                     <form id="form1" runat="server">

Now right click on your project and click add reference, in the browse tab find out the folder in which you have installed spire.pdf. Usually it will be in the C:\Program Files\e-iceblue\Spire.pdf. Now just find your framework version from BIN folder and add Spire.pdf.dll


Now we have added reference too. So shall we start coding ?

Using the code

There are so many features available, we will look in to most useful features which you might found useful.

Convert PDF Page to Image

In this part, we will see how can we convert a PDF file to image with a specific resolution. It is so simple and powerful.

We will create a button as follows.

<asp:Button ID="btnConvertToImage" runat="server" Text="Convert To Image" />

Next we need to add the needed references.

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;

And in the button click we can write the preceding codes.

protected void btnConvertToImage_Click(object sender, EventArgs e) {
    try {
        PdfDocument documemt = new PdfDocument();
        documemt.LoadFromFile(@ "D:\\sibeeshpassion.pdf");
        Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);
        image.Save(@ "D:\\result.jpg");
    } catch (Exception) {

As you can see we are calling the below function to generate the image.

documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);

And this function has overloaded as in the below image.


Here we are taking the pdf file sibeeshpassion.pdf and we are getting a image as follows as output.


Cool Right?

Now we will see another implementation.


There are few many security options are also available with this package like Encryption and Decryption of the pdf file and creating a digital signature and many more.

Encrypting PDF File

In this step, we are going to set a password for our pdf document.

To work with security, you need to add a new reference as follows.

using Spire.Pdf.Security;

Now add a new button as follows.

<asp:Button ID="btnEncryptPDF" runat="server" Text="Encrypt PDF" OnClick="btnEncryptPDF_Click"/>

Now it is time for click event.

protected void btnEncryptPDF_Click(object sender, EventArgs e) {
    try {
        PdfDocument doc = new PdfDocument(@ "D:\\sibeeshpassion.pdf", "sibeeshpassionEncrypted");
        doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
        doc.Security.OwnerPassword = "sibeeshpassion";
        doc.Security.UserPassword = "sibeesh";
        doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;
    } catch (Exception) {

Three kind of pdf key size are available as shown in the below image.


And as you can see, we are setting some permission by using the preceding code.

doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;

There are some other permission options also available.


Now if you try to open our pdf again, it will ask you for a password which we have set in the above codes. Cool.

Decryption of PDF File

AS we encrypt our pdf file, we need to do decryption also right?

Add an another button.

<asp:Button ID="btnDecrypt" runat="server" Text="Decrypt PDF" OnClick="btnDecrypt_Click" />

Add the following code block in the button click event.

try {
    PdfDocument doc = new PdfDocument(@ "D:\\sibeeshpassion.pdf", "sibeeshpassionEncrypted");
    //extract image
    Image image = doc.Pages[0].ImagesInfo[0].Image;
    //Save image file.
    image.Save("sibeeshpassionDecrypted.png", System.Drawing.Imaging.ImageFormat.Png);
    //Launching the image file.
} catch (Exception ex) {

Once you run the above codes, your pdf file will be decrypted.


You can add watermark to your pdf file easily with this package.

Adding a watermark

There are two kind of watermarks. One is text watermark and other is image watermark.

Now we will add another button.

<asp:Button ID="btnAddImageWatermark" runat="server" Text="Add Image Watermark" OnClick="btnAddImageWatermark_Click" />

And in the button click we can write the preceding codes.

protected void btnAddImageWatermark_Click(object sender, EventArgs e) {
    try {
        PdfDocument doc = new PdfDocument("D:\\Sibi.pdf", "Sibi");
        Image img = Image.FromFile("D:\\sibi.jpg");
        doc.Pages[0].BackgroundImage = img;
    } catch (Exception) {

As you can see we are taking a pdf file and add a watermark image to the file and at last we save it to an another file. In this case from sibi.pdf to sibeesh.pdf.

I am taking sibi.jpg as the water mark image, so you can see the pdf file with watermarked image as follows.


Please be noted that, you can convert your PDF file to any other file format, there are plenty of options available. Please try that too. I have given only few options which I use always.

Great, now that we learned about Spire PDF, I would like to introduce another similar product, that is Iron Pdf.

Install with Nuget Package Manager

Open your Visual Studio, and go to the package manager console (Tools -> NuGet Package Manager ->Package Manager Console) and then run the preceding command.

PM > Install-Package IronPdf

You can get more information on this from the ironPdf Nuget gallery here: The package has cross platform support with .NET 6 and .NET 5, .NET Core, Standard, and Framework. 

Let's take a deep dive and understand how Iron PDF can help us to convert an html file to pdf with a few lines of codes. To make things simple, I am going to create a Console Application in .Net6 using Visual Studio 2022, and paste the below code to the Program.cs file.

using IronPdf.Security;
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "Default.log"; //May be set to a directory name or full file
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
var Renderer = new IronPdf.ChromePdfRenderer();
var content = "<h1>Sample PDF Generated</h1><p>This is a sample PDF generated by the Iron PDF from C# and it is password protected!</p>";
var pdfFile = Renderer.RenderHtmlAsPdf(content);
//Adding Watermark
// Watermarks all pages with red "Sample Watermark" text at a custom location.
// Also adding a link to the watermark on-click
pdfFile.WatermarkAllPages("<h4 style='color:red'>Sample Watermark</h4>", IronPdf.Editing.WaterMarkLocation.TopCenter, 50, -15, "your website link here");
// Adding Security Features
pdfFile.SecuritySettings.AllowUserCopyPasteContent = false;
pdfFile.SecuritySettings.AllowUserEdits = PdfEditSecurity.NoEdit;
pdfFile.SecuritySettings.UserPassword = "g6hd7£$_<>$&Q";
//Save the Pdf

The IronPdf library has a lot of security features that can easily be added to your pdf file. Some of them are listed in the screenshot below.

In the code above, we have generated a PDF by providing the content of the PDF, and added some extra security to the PDF such as added password, disabled the user edit option, disabled the content copy option etc. We also added a watermark to our Pdf. Please be noted that the content we provide here is an HTML so you can just add as many customization to your HTML as you wish. Let's run the code and see how our PDF looks like.

As a developer, it is important to get the logs to debug any issues. This tool provides an option for that, you just have to add the code below.

IronPdf.Logging.Logger.EnableDebugging = true;

IronPdf.Logging.Logger.LogFilePath = "Default.log"; //May be set to a directory name or full file

IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

It will generate the logs to your console and also to a file with the name you specify.

When you run, a new PDF file will be generated in your Debug folder, in my case it was in PdfExample\bin\Debug\net6.0. Just open the PDF, you will be asked to enter the password you have defined, as in the previous image.

Just press Enter after you have provided the password, you should be able to see your PDF content as in the image below.

Both Iron PDF and Spire PDF are great and do the job they are intended to. Two things I like about the IronPDF is that it has a lot of customization and has great logging options.

Similar Articles