Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

.NETASP.NETC#ProductsSpire.PDF
Home›.NET›Using Spire.PDF In Asp.Net

Using Spire.PDF In Asp.Net

By SibeeshVenu
August 24, 2015
2458
0
Share:
Using_Spire_PDF_Create_New_Project

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 rad here.

  • Using Spire.Doc
  • Using Spire.Xls
  • Working With Charts Using Spire.Xls
  • Download source code

    Using Spire PDF

    Background

    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.

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

    Using_Spire_PDF_Add_License_To_Project

    Using_Spire_PDF_Add_License_To_Project

    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.

    Using_Spire_PDF_Installing

    Using_Spire_PDF_Installing

    Using_Spire_PDF_Installing_Complete

    Using_Spire_PDF_Installing_Complete

    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.

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

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>
    [/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 in the 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 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.

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

    Next we need to add the needed references.

    [csharp]
    using Spire.Pdf;
    using Spire.Pdf.Graphics;
    using System;
    using System.Drawing;
    [/csharp]

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

    [csharp]
    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");
    documemt.Close();
    }
    catch (Exception)
    {

    throw;
    }
    }
    [/csharp]

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

    [csharp]
    documemt.SaveAsImage(0, PdfImageType.Bitmap, 400, 400);
    [/csharp]

    And this function has overloaded as in the below 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 we are getting a image as follows as output.

    Using_Spire_PDF_Covert_PDF_To_Image_Output

    Using_Spire_PDF_Covert_PDF_To_Image_Output

    Cool Right?

    Now we will see another implementation.

    Security

    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.

    [csharp]
    using Spire.Pdf.Security;
    [/csharp]

    Now add a new button as follows.

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

    Now it is time for click event.

    [csharp]
    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)
    {

    throw;
    }
    }
    [/csharp]

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

    Using_Spire_PDF_Possible_Key

    Using_Spire_PDF_Possible_Key

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

    [csharp]
    doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;
    [/csharp]

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

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

    Add the following code block in the button click event.

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

    }
    catch (Exception ex)
    {

    throw;
    }
    [/csharp]

    Once you run the above codes, 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.

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

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

    [csharp]
    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;
    doc.SaveToFile("D:\\Sibeesh.pdf");
    doc.Close();

    }
    catch (Exception)
    {

    throw;
    }
    }
    [/csharp]

    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.

    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 yet? 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.

    Kindest Regards
    Sibeesh Venu

    TagsAdding a watermark To PDFAsp.NetC#Convert PDF Page to ImageDecryption of PDF FileEncrypting PDF FileProductsSpire.PDFUsing Spire
    Previous Article

    A Drag And Drop Game

    Next Article

    Make Last Option As Selected In Select,Drop ...

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • Add_References
      .NETASP.NETWeb API

      Caching In Web API

      March 24, 2016
      By SibeeshVenu
    • Compression_References
      .NETASP.NET

      Programmatically Extract or Unzip Zip,Rar Files And Check

      February 25, 2016
      By SibeeshVenu
    • .NETASP.NETC#Code Snippets

      Split method with a string value in c#

      May 31, 2015
      By SibeeshVenu
    • .NETCodeProjectMVC

      Uploading and Downloading in MVC Step-by-Step

      June 14, 2015
      By SibeeshVenu
    • Advanced JQX Grid With All Functionality
      JQueryJQWidgetsJQX GridProducts

      Advanced JQX Grid With All Functionality

      October 29, 2014
      By SibeeshVenu
    • Q&A

      CellSetGrid Implementation in VS 2012 or later

      July 14, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version