Wednesday, January 5, 2011

Creating a PDF Document through C# Part 1.

Creating PDF Documents through C# is a great skill to have. It enables us to present data, text, and pictures in a safe, and versatile format.  To begin download the itextsharp.dll file from SourceForge.net.

Add it to the project using the add reference option, by browsing. Now, we are ready to create our first document. I have kept it deliberately simple, so that we can focus on the basics.
The PDFWriter Windows Application.


Form1.cs

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


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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                iTextSharp.text.Document newdoc = new iTextSharp.text.Document();
                System.IO.FileStream fs = new System.IO.FileStream(txtFileName.Text , System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(newdoc, fs);
                newdoc.Open();
              
                newdoc.AddSubject(txtSubject.Text );
                newdoc.AddTitle(txtTitle.Text);
                newdoc.AddAuthor(txtAuthor.Text );
                System.IO.MemoryStream ms=new System.IO.MemoryStream ();
                pic.Image.Save (ms,System .Drawing.Imaging.ImageFormat.Jpeg);
               
                iTextSharp.text.Image img = iTextSharp.text.Jpeg.GetInstance(ms.ToArray());
                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph(txt.Text);
                p.Add(img);
               
               
                newdoc.Add(p);
               
             
              newdoc.Close();
              MessageBox.Show("Document Created");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ofd.ShowDialog(this);
        }

        private void ofd_FileOk(object sender, CancelEventArgs e)
        {
            pic.Image = System.Drawing.Image.FromFile(ofd.FileName);
        }
    }
}
 

Form1.Designer.cs



namespace PDFWriter
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txt = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.txtSubject = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtAuthor = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txtTitle = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.txtFileName = new System.Windows.Forms.TextBox();
            this.pic = new System.Windows.Forms.PictureBox();
            this.button2 = new System.Windows.Forms.Button();
            this.ofd = new System.Windows.Forms.OpenFileDialog();
            ((System.ComponentModel.ISupportInitialize)(this.pic)).BeginInit();
            this.SuspendLayout();
            //
            // txt
            //
            this.txt.Location = new System.Drawing.Point(86, 149);
            this.txt.Multiline = true;
            this.txt.Name = "txt";
            this.txt.Size = new System.Drawing.Size(347, 268);
            this.txt.TabIndex = 0;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(305, 441);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(117, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Save To PDF";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(86, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(43, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Subject";
            //
            // txtSubject
            //
            this.txtSubject.Location = new System.Drawing.Point(162, 13);
            this.txtSubject.Name = "txtSubject";
            this.txtSubject.Size = new System.Drawing.Size(156, 20);
            this.txtSubject.TabIndex = 3;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(86, 48);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(38, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Author";
            //
            // txtAuthor
            //
            this.txtAuthor.Location = new System.Drawing.Point(162, 48);
            this.txtAuthor.Name = "txtAuthor";
            this.txtAuthor.Size = new System.Drawing.Size(156, 20);
            this.txtAuthor.TabIndex = 3;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(86, 123);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(49, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Contents";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(86, 87);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(27, 13);
            this.label4.TabIndex = 2;
            this.label4.Text = "Title";
            //
            // txtTitle
            //
            this.txtTitle.Location = new System.Drawing.Point(162, 87);
            this.txtTitle.Name = "txtTitle";
            this.txtTitle.Size = new System.Drawing.Size(156, 20);
            this.txtTitle.TabIndex = 3;
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(355, 13);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(49, 13);
            this.label5.TabIndex = 2;
            this.label5.Text = "Filename";
            //
            // txtFileName
            //
            this.txtFileName.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
            this.txtFileName.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem;
            this.txtFileName.Location = new System.Drawing.Point(431, 13);
            this.txtFileName.Name = "txtFileName";
            this.txtFileName.Size = new System.Drawing.Size(156, 20);
            this.txtFileName.TabIndex = 3;
            //
            // pic
            //
            this.pic.Location = new System.Drawing.Point(528, 149);
            this.pic.Name = "pic";
            this.pic.Size = new System.Drawing.Size(177, 169);
            this.pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pic.TabIndex = 4;
            this.pic.TabStop = false;
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(590, 334);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "Get Picture";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // ofd
            //
            this.ofd.Filter = "All Pictures|*.jpg;*.jpeg;*.bmp;*.wmf;*.gif;*.png|All Files|*";
            this.ofd.FileOk += new System.ComponentModel.CancelEventHandler(this.ofd_FileOk);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(790, 486);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.pic);
            this.Controls.Add(this.txtTitle);
            this.Controls.Add(this.txtAuthor);
            this.Controls.Add(this.txtFileName);
            this.Controls.Add(this.txtSubject);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.txt);
            this.Name = "Form1";
            this.Text = "PDF Creator";
            ((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txt;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtSubject;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtAuthor;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtTitle;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox txtFileName;
        private System.Windows.Forms.PictureBox pic;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.OpenFileDialog ofd;
    }
}






 

4 comments:

  1. Respected sir
    Thanking you sir,
    for given some help on C#.
    I am happy fr this.
    Please issue some other example on C# for conectivity with SQLServer & using fill in list view through data binding in a form.

    Regards
    Abhishek Pathak

    ReplyDelete
  2. Great post sir...

    I want to create a functionality similar to google suggest in web application.
    Can you please provide any demo for that.

    Looking forward for your prompt response

    ReplyDelete
  3. sir interesting program suggest in c or c++

    ReplyDelete