Tuesday, 12 August 2014

More details about generating PDF using Spire.PDF

The intention behind writing this article is to introduce a new product for generating PDF which I felt is very simple and easy to use. Really generating a PDF makes it very simple using this component.
For this, you need not required to install Acrobat Reader and good thing is it can be integrated with .Net. So using C# or VB.NET we can create our PDF document.
Let me show some couple of example which will give you clear idea of how simple it is to use this component.
Steps to generate the simple PDF document –

1  1)      Add the reference of the Spire.Pdf dll in your project.

2)  Create an object of PDF document
PdfDocument doc = new PdfDocument();
3) Now you need a content place holder where actual content need to be loaded. So add a page in the PDF document.
PdfPageBase page = section.Pages.Add();
Creating PDF document with Text
Using Canvas.DrawString method, add the content. Here I have taken one textbox in my web page and loaded the content from that text box.
Code for add text object
private void LoadText(PdfSection section)
        PdfPageBase page = section.Pages.Add();
                               new PdfFont(PdfFontFamily.Helvetica, 20f),
                               new PdfSolidBrush(Color.Red),
                               10, 10);

Adding image in a PDF
For loading the image and adding it in a PDF document is very simple.
Create an object of PdfImage which takes parameter of Stream object. Here I have used FileUpload control in my web page and passed the InputStream as shown below:
PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);

Code for adding Image
private void LoadImage(PdfSection section)
        if (FileUpload1.HasFile)
            PdfPageBase page = section.Pages.Add();
            PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);
            float width = image.Width * 0.50f;
            float height = image.Height * 0.50f;
            float x = (page.Canvas.ClientSize.Width - width) / 2;

            page.Canvas.DrawImage(image, x, 60, width, height);

Adding grid in a PDF
This is the most important type of data representation which we usually require in our application.
i.e. - Show data in a grid format. This tool provides us easy way of creating 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.
PdfTable table = new PdfTable();

Code for generating grid

  PdfPageBase page = section.Pages.Add();

        //create table
        PdfTable table = new PdfTable();
        table.Style.CellPadding = 2;
        table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
        table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
        table.Style.AlternateStyle = new PdfCellStyle();
        table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
        table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
        table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
        table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
        table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
        table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
        table.Style.ShowHeader = true;

        table.DataSourceType = PdfTableDataSourceType.TableDirect;

//GenerateList return a data table       
table.DataSource = GenerateList();
        //Set the width of column
        float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1);
        table.Columns[0].Width = width * 0.24f * width;
        table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[1].Width = width * 0.21f * width;
        table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[2].Width = width * 0.24f * width;
        table.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Draw(page, new PointF(0, 10));

This is just a simple sample to show you how powerful is the tool. Not only loading text, images or grid, you can show lots of other data like
·         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
And many more…
One more feature about this component is providing Security to your PDF file means creating password protected PDF. Below is the code snippet to generate the password protected file.
doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
        doc.Security.OwnerPassword = "pradeep";
        doc.Security.UserPassword = "123";
        doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;   

Note: One another important thing I would like to share with you is - This tool comes with the proper documentation along with sample demos which you can find in their website. It is a paid tool but the worth tool.
Using Spire.PDF, create you PDF files as per your requirement easily and efficiently. I shared some of its simple features, but there are lots more. So visit the website given below:
Hope you like this article. Please share your comments whether it’s good or bad.

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...