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();
        page.Canvas.DrawString(txtFreeText.Text,
                               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.
Conclusion
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:
http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html#.U8pJYuOSzSl
Hope you like this article. Please share your comments whether it’s good or bad.


Tuesday, 5 August 2014

Easy way for taking data in pdf file from Visual Stdio


                                                                                                                                           Next

Work with Spire.PDF for creating and writing PDF files on any .NET applications
Introduction
Spire.PDF  is a professional PDF .NET library which is  designed for .NET developers in order to create, read, write, convert and print PDF Files from any .NET Platform fast and with a high quality performance in this way of user for taking data in PDF file. And in present lot of Big organization  using this product.
This software support  C#, VB.NET,ASP.NET, AND ASP.NET MVC
Platform: Microsoft visual studio 2010 Express.
Why we use Spire.PDF

Spire.PDF is very useful software for reading, writing and editing a dot net file without using any other software. Sometimes we have huge data in our project which we would like to save in pdf format and for this we have to use some external software. Spire.PDF solves such problems. It helps to convert the dot net file to PDF file on one click.

How to Install this Spire.PDF

Step(1): First download this software from this link.
Step(2): After download double click on software After that this window will open.
Here click on next



Step(3): In next step accept the terms and condition and click on next after that a window will open where you select a feature.
If you are using a free version then this window will  open



Otherwise this window will open


Step(4): After click on next it will ask for install and this window will open

Step(5): After that install this software and use this software.

How to use this product with code.
For using this feature we have to use libraries as shown below.
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

These namespace are used for graphics.
Convert Text file to PDF format in C# .net
Converting a text file to PDF is very difficult in dot net. In organization they convert this text file to PDF format by using other external software.
But this is very difficult for users. For removing this problem, Spire.Pdf  make it very simple. Only we have to use some code for this.
Go to the below link and download the software.
Step(1):- First of all open your visual studio and select a console application.


Step(2):- Add library

This are some libraries which we have to use for generate PDF files
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

Step(3):- Code

using System.Drawing;
using System.Drawing.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;
using System.IO;


namespace ConvertTextFile_to_pdfFile
{
    class TextFile
    {
        static void Main(string[] args)
        {
            PdfDocument PDF = new PdfDocument();
            DrawText(PDF.Sections.Add());
            PDF.SaveToFile("pdfFile.pdf");
            PDF.Close();
            System.Diagnostics.Process.Start("pdfFile.pdf");
        }

        private static void WriteTextFile(PdfSection section)
        {
            string text = File.ReadAllText(@"..\..\test.txt");
            PdfPageBase page = section.Pages.Add();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
            PdfStringFormat format = new PdfStringFormat();
            format.LineSpacing = 20f;
            float pageWidth = page.Canvas.ClientSize.Width;
            PdfBrush brush = PdfBrushes.LightSeaGreen;
            float y = 30;
            PdfStringLayouter textLayouter = new PdfStringLayouter();
            PdfStringLayoutResult result
                = textLayouter.Layout(text, font, format, new                         SizeF(pageWidth, 0));

            foreach (LineInfo line in result.Lines)
            {
                page.Canvas.DrawString(line.Text, font, brush, 0, y, format);
                y = y + result.LineHeight;
            }
        }
    }
}


Step(4):-  Image of PDF

Monday, 30 June 2014

Retrieving Image from database in window application


                                                                                                                                               Previous..
                                                                                                                                                    Next..
Here i will explain How to Retrieving Image from database in window application.

Step(1): Form 
drag and down  a combo box when i select a name from combo box then all fields will show with his image.
Step(2): Code
Click on combo box a write this code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

String gender;                                                              //we have to define this
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
              SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
        string query = "select * from DataBaseTable Where name = '" + comboBox1.text+"';

sqlCommand cmd = new sqlCommand ( query,con);
sqlDatareader dbr = new sqlDatareader;

try
{
con.open();
dbr.cmd.ExecuteReder();while(dbr.read())
{string sEid = dbr.GetInt32("Eid").toString();
string sName = dbr.GetInt32("name");
string sSurname = dbr.GetInt32("Sname");
string sAge = dbr.GetInt32("Age").toString();Eid.text =    sEid ;
Firstname.text=     sName; 
Surname.text =     sSurname; 
Age.text=              sAge ;

byte[]  imageBt = (byte[])(dbr[ " image"]);
if( imageBt == null ) 
{
pictureBox.Image = null;
}

else
{
memoryStream  mstream =  new memoryStream(   imageBt );

pictureBox.Image = system.Drawing.Image.FromStream( mstream );
}
}
}

Catch( Exception ex)
{

messageBox.show(ex.message);


}


Step(3): Output

Run your application and retrieve the data. 
                                                                                                                                               Previous..
                                                                                                                                                    Next..


Wednesday, 21 May 2014

How to save image in database in window application


                                                                                                                                                Previous..
                                                                                                                                                    Next...
Here i will explain How to save image in database in window application.

Step(1): Form
In Tutorial 23 i shown you how to insert data into database so this is also in same way we will insert employee detail with his picture. Drag and down a textbox for image path. and this textbox name as "textBox_image_path"


Step(2): Code
Coding at saving button.Make a column a in database as "image"
we will use a namespace as "Using system . IO;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

String gender;                                                              //we have to define this
        private void button1_Click(object sender, EventArgs e)
        {

         byte[]  imageBt = null;
   FileStream fstream = new FileStream ( this.textBox_image_path.Text , FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader (fstream  );
imageBt = br.ReadBytes((int)fstream.Length);

           SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "insert into data (E.id,name,surname,age,gender,DOB,image) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"' , '"+this.gender+"' , '"+this.DateTimePicker1.Text+"' ,@IMG)";
     SqlCommand cmd = new sqComamnd(query,con);         
  SqlDataReader dbr;
                       
               try
              {
               con.open();

cmd.Parameters.Add( new MySqlParameters( "@IMG" , imageBt));
               dbr = cmd.ExecuteReader();
                MessageBox.Show("saved");
              while(dbr.read())
             {
                 
              }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "male";
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "female";
}
    }
} 

private void LoadImage_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();

openfile .filter = "JPG Files(* . jpg) | *.jpg | PNG Files(* . png) | * . png | All Files( * . *) | * . * " ;

if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{

string picpath openfile .FileName.ToString();
textBox_image_path.Text = picpath ;
pictureBox1.ImageLocation = picpath;


}
}
}
}


Step(3): Output

Now run your application then when you fill the data and you will see in database there image will be store. you will see database as "BLOB" which store your image.
                                                                                                                                                Previous..
                                                                                                                                                    Next...

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