Using Spire.PDF In ASP.NET

In this article we are going to see a new product Spire.PDF that helps us to create, manipulate PDF and perform many other operations. I hope you have read my articles under these categories. If you have not read them, I suggest you to read here.

Please read this article in my blog. 

Download source code.

Background

As you all know a PDF is the 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 develop anything that is needed. But have you ever tried creating and managing PDF file? That’s a quite difficult task, right? Still it is possible. “Everything is possible, the word impossible itself says I am possible”. Now its time to introduce you to a product which lets you do these tasks in an easy manner. That means, you are able to do these tasks with a little effort.

Create a new Project

Now we will create a new Project in Visual Studio.

Using_Spire_PDF_Create_New_Project

Using_Spire_PDF_Create_New_Project

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 limitations. You can try that.

Using_Spire_PDF_Add_License_To_Project

Using_Spire_PDF_Add_License_To_Project

Download the files

You can always download 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 start now.

Using_Spire_PDF_Installing

Using_Spire_PDF_Installing

Using_Spire_PDF_Installing_Complete

Using_Spire_PDF_Installing_Complete

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 look like the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UsingSpirePDF.Default" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div></div>  
  11.         </form>  
  12.     </body>  
  13. </html>  
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 at the following location: C:\Program Files\e-iceblue\Spire.pdf. Now just find your framework version from BIN folder and add Spire.pdf.dll.
 
Using_Spire_PDF_Adding_Reference

Using_Spire_PDF_Adding_Reference

Now we have added reference too, so shall we start coding?

Using the code

There are so many features available, we will look into the most useful feature which you might found useful.

Convert PDF Page to Image

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

We will create a button as in the following code snippet:

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

Next we need to add the needed references.

  1. using Spire.Pdf;  
  2. using Spire.Pdf.Graphics;  
  3. using System;  
  4. using System.Drawing;  

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

  1. protected void btnConvertToImage_Click(object sender, EventArgs e)  
  2. {  
  3.    try  
  4.    {  
  5.       PdfDocument documemt = new PdfDocument();  
  6.       documemt.LoadFromFile(@"D:\\SibeeshPassion.pdf");  
  7.       Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);  
  8.       image.Save(@"D:\\result.jpg");  
  9.       documemt.Close();  
  10.    }  
  11.    catch (Exception)  
  12.    {  
  13.       throw;  
  14.    }  
  15. }  

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

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

And this function has overloaded as in the following image:

Using_Spire_PDF_Covert_PDF_To_Image

Using_Spire_PDF_Covert_PDF_To_Image

Here we are taking the pdf file sibeeshpassion.pdf and getting an image as in the following output:

Using_Spire_PDF_Covert_PDF_To_Image_Output

Using_Spire_PDF_Covert_PDF_To_Image_Output

Now we will see another implementation.

Security

There are many security options available with this package such as 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 in the following:

  1. using Spire.Pdf.Security;  

Now add a new button as in the following code snippet:

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

Now it is time for click event.

  1. protected void btnEncryptPDF_Click(object sender, EventArgs e)  
  2. {  
  3.    try  
  4.    {  
  5.       PdfDocument doc = new PdfDocument(@"D:\\SibeeshPassion.pdf""SibeeshPassionEncrypted");  
  6.       doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;  
  7.       doc.Security.OwnerPassword = "sibeeshpassion";  
  8.       doc.Security.UserPassword = "sibeesh";  
  9.       doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;  
  10.    }  
  11.    catch (Exception)  
  12.    {  
  13.       throw;  
  14.    }  
  15. }  

Three kinds of pdf key sizes are available as in the following image.

Using_Spire_PDF_Possible_Key

Using_Spire_PDF_Possible_Key

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

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

There are some other permission options also available.

Using_Spire_PDF_Possible_File_Permissions

Using_Spire_PDF_Possible_File_Permissions

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 another button.

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

Add the following code block in the button click event.

  1. try  
  2. {  
  3.    PdfDocument doc = new PdfDocument(@"D:\\SibeeshPassion.pdf""SibeeshPassionEncrypted");  
  4.    //extract image  
  5.    Image image = doc.Pages[0].ImagesInfo[0].Image;  
  6.    doc.Close();  
  7.    //Save image file.  
  8.    image.Save("SibeeshPassionDecrypted.png", System.Drawing.Imaging.ImageFormat.Png);  
  9.    //Launching the image file.  
  10.    System.Diagnostics.Process.Start("SibeeshPassionDecrypted.png");  
  11. }  
  12. catch (Exception ex)  
  13. {  
  14.    throw;  
  15. }  

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

Watermark

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.

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

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

  1. protected void btnAddImageWatermark_Click(object sender, EventArgs e) {  
  2.     try {  
  3.         PdfDocument doc = new PdfDocument("D:\\Sibi.pdf""Sibi");  
  4.         Image img = Image.FromFile("D:\\sibi.jpg");  
  5.         doc.Pages[0].BackgroundImage = img;  
  6.         doc.SaveToFile("D:\\Sibeesh.pdf");  
  7.         doc.Close();  
  8.     }   
  9.     catch (Exception) {  
  10.         throw;  
  11.     }  
  12. }  
As you can see we are taking a pdf file and adding a watermark image to the file and at last saving it to another file. In this case from sibi.pdf to sibeesh.pdf.

I am taking sibi.jpg as the watermark image, so you can see the pdf file with watermarked image as in the following screenshot:
 
Using_Spire_PDF_Adding_Watermark

Using_Spire_PDF_Adding_Watermark

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.

Conclusion

These are the features I loved using Spire.PDF. It is much effective and simple. What do you think about this? Are you using Spire.PDF? Do you plan to try this? Did I miss anything that you may think which is needed?. I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on Stack Overflow instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I am able to.

Up Next
    Ebook Download
    View all
    Learn
    View all