Spire.PDF For Generating PDF

I will be using Spire.NET to generate various types in PDFs. It is very simple, fast and easy to use.

To use this library, you need not install Acrobat Reader and the fabulous thing is that it can be integrated with .NET, so using the C# or VB.NET language we can create our PDF document easily.

Let me show some examples that will provide you a clear idea of generating a PDF. I will try to show the following combination of PDFs:

  1. With text accepted from a textbox.
  2. With an image.
  3. Display the grid details shown on a screen into a PDF.
  4. Password-protected PDF.

The following is the procedure to generate the PDF document:

Step 1

generate the PDF

UI Code

  1. <asp:TextBox ID="txtFreeText" runat="server" TextMode="MultiLine" Height="146px" Width="465px"></asp:TextBox>  
  2.     <br />  
  3.      <h4>Upload Image</h4>  
  4.     <asp:FileUpload ID="FileUpload1" runat="server" />  
  5.     <br />  
  6.    <h4>Customers</h4>  
  7.     <asp:GridView ID="grdNames" runat="server" AutoGenerateColumns="false" GridLines="Vertical" BorderColor="Blue">  
  8.         <Columns>  
  9.             <asp:BoundField DataField="Id" HeaderText="Id" />  
  10.             <asp:BoundField DataField="FirstName" HeaderText="First Name" />  
  11.             <asp:BoundField DataField="LastName" HeaderText="Last Name" />  
  12.         </Columns>  
  13.     </asp:GridView>  
  14. <br/>  
  15. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load PDF" />  

Step 2

Add the reference of the Spire.PDF DLL to your project. You will also get this from Nugget.

Add the referenc

Step 3

On the click event of the button:

  1. Create an object of PDF document as in the following:
    1. PDFDocument doc = new PDFDocument();  
  2. Add a section for the content as in the following:
    1. PDFSection section = doc.Sections.Add();  
  3. Now you need a content place holder for where to load the actual content, so add a page to the PDF document.
    1. PDFPageBase page = section.Pages.Add();  

Creating PDF document with Text

Using the Canvas.DrawString method, add the content. Here I am using one textbox in my web page and loaded the content from that text box.

Code for adding text object

  1. private void LoadText(PDFSection section)  
  2. {  
  3.    PDFPageBase page = section.Pages.Add();  
  4.    page.Canvas.DrawString(txtFreeText.Text,  
  5.    new PDFFont(PDFFontFamily.Helvetica, 20f),  
  6.    new PDFSolidBrush(Color.Red),  
  7.    10, 10);  
  8. }  
Output

program output

Creating PDF with image

Loading the image and adding it in a PDF document is simple.

Create an object of PDFImage that takes a parameter of a Stream object. Here I have used a FileUpload control in my web page and passed the InputStream as in the following code snippet:
  1. PDFImage image = PDFImage.FromStream(FileUpload1.PostedFile.InputStream);  
  1. private void LoadImage(PdfSection section)  
  2. {  
  3.     if (FileUpload1.HasFile)  
  4.     {  
  5.         PdfPageBase page = section.Pages.Add();  
  6.         PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);  
  7.         float width = image.Width * 0.50f;  
  8.         float height = image.Height * 0.50f;  
  9.         float x = (page.Canvas.ClientSize.Width - width) / 2;  
  10.   
  11.         page.Canvas.DrawImage(image, x, 60, width, height);  
  12.     }  
  13. }  
Output

DrawImage

Creating PDF with a grid

This is the most important type of data representation that we usually require in our application, showing the data in a grid format. This tool provides an easy way to create a grid structure.

Here we create an object of PDFTable and then using a DataSource property provides a collection object. I want to show First Name and Last Name in my grid.
  1. PDFTable table = new PDFTable();  
Code for generating grid
  1.   PdfPageBase page = section.Pages.Add();  
  2.   
  3.      //create table  
  4.      PdfTable table = new PdfTable();  
  5.      table.Style.CellPadding = 2;  
  6.      table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;  
  7.      table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));  
  8.        
  9.      table.Style.AlternateStyle = new PdfCellStyle();  
  10.      table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;  
  11.      table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));  
  12.        
  13.      table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;  
  14.      table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;  
  15.      table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));  
  16.      table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);  
  17.        
  18.      table.Style.ShowHeader = true;  
  19.   
  20.      table.DataSourceType = PdfTableDataSourceType.TableDirect;  
  21.   
  22. //GenerateList return a data table          
  23. table.DataSource = GenerateList();  
  24.        
  25.      //Set the width of column  
  26.      float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1);  
  27.      table.Columns[0].Width = width * 0.24f * width;  
  28.      table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);  
  29.      table.Columns[1].Width = width * 0.21f * width;  
  30.      table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);  
  31.      table.Columns[2].Width = width * 0.24f * width;  
  32.      table.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);  
  33.      table.Draw(page, new PointF(0, 10));  
Output
Output

Creating Password protected PDF

One more feature about this component is to provide security to your PDF file, which  means creating a password-protected PDF. The following is the code snippet to generate the password-protected file.
  1. doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;  
  2.     doc.Security.OwnerPassword = "pradeep";  
  3.     doc.Security.UserPassword = "123";  
  4.     doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;      
Creating Password protected PDF

There are other features of this library that you may need at another time. It can actually not only load text, images or grid, you can show much other data as in the following: 
  • Adding Watermark
  • Drawing objects
  • Adding action to the links within the PDF
  • Adding Paging
  • Generating Barcodes
  • Merging more than one PDF into single PDF
  • Splitting one PDF into multiple

Conclusion

Here I end my article about generating a PDF that I think was easy to use and simple to understand. So try creating it yourself. I hope you liked this article and please do comment even if it is good or bad. Your comment will definitely help me to get better and write better for you.

References:


Similar Articles