How To Merge Content of First Pdf to Second PDF using itext sharp and How to add image logo in pdf


using (Stream inputPdfStream = new FileStream(“input.pdf”, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream(“some_image.jpg”, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(“result.pdf”, FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);

Image image = Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
stamper.Close();
}

Advertisements

How to Read CSV Into DataTable Using asp.net c#


public DataTable CsvFileToDatatable(string path, bool IsFirstRowHeader)
{
string header = "No";
string sql = string.Empty;
DataTable dataTable = null;
string pathOnly = string.Empty;
string fileName = string.Empty;
try
{
pathOnly = Path.GetDirectoryName(path);
fileName = Path.GetFileName(path);
sql = @"SELECT * FROM [" + fileName + "]";
if (IsFirstRowHeader)
{
header = "Yes";
}
using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
		 ";Extended Properties=\"Text;HDR=" + header + "\""))
{
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
}
}
}
}
finally
{
}
return dataTable;
}

DropDownList in GridView Async and find Control in Class File in c#.net


In this article, I have described the ways of keeping DropDownList in the GridView and binding the data by preserving the default SelectedValue. Apart from DropDownList, I have also shown how to keep CheckBox, RadioButtonList, TextBox in the GridView and preserving the default data,how to find control in class file ,how to use async Trigger in Update Panel using GridView in C#.net

Placing the DropDownList in the GridView

To place the DropDownList in the GridView, you will have to use asp:TemplateField that under <Columns>, Generally you will need to use the DropDownList in the GridView in the Edit mode (under EditItemTemplate, However this solution will work for you even if you want to place the DropDownList in the ItemTemplate) where you want your user to select the data from the DropDownList and update the record. A typical example is shown in the picture below

Image

The aspx page code of above GridView is below

<%@ Page Title="" Language="C#" MasterPageFile="~/GridMaster.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="Default" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"
    ClientIDMode="Static">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ClientIDMode="Static">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="GridView1" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="lblMessage" runat="server" EnableViewState="true" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ClientIDMode="Static"
                GridLines="None" AutoGenerateEditButton="True" OnRowCancelingEdit="GridViewCancelEdit"
                OnRowEditing="GridViewEdit" OnRowUpdating="GridViewUpdate" 
                DataKeyNames="PersonID" CellPadding="4" ForeColor="#333333">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="PersonID" HeaderText="Person ID" ReadOnly="True" Visible="false" />
                    <asp:TemplateField HeaderText="First Name">
                        <ItemTemplate>
                            <%# Eval("FName") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FName") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Last Name">
                        <ItemTemplate>
                            <%# Eval("LName") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LName") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Department DropDown">
                        <ItemTemplate>
                            <%# Eval("DeptName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="dropDownDept" DataSource="<%# BindDropDownDepartment() %>"
                                DataTextField="DeptName" DataValueField="DeptId" runat="server" SelectedValue='<%# Eval("DeptId").ToString() %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CustomCheckBox Field">
                        <ItemTemplate>
                            <%# Eval("IsActive") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkActive" runat="server" Checked='<%# Eval("IsActive") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="RadioButtonField">
                        <ItemTemplate>
                            <%# Eval("IsActive") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:RadioButtonList ID="radioActive" runat="server" RepeatDirection="Horizontal"
                                RepeatLayout="Flow" SelectedValue='<%# Eval("IsActive") %>'>
                                <asp:ListItem Text="True" Value="True" />
                                <asp:ListItem Text="False" Value="False" />
                            </asp:RadioButtonList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

In your code behind file of Default.aspx.cs code will be such as below given

using System;
using System.Data;
 
public partial class Default : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var obj = new BAL();
            obj.LoadPerson(ref  GridView1);
        }
    }
    
    protected DataTable BindDropDownDepartment()
    {
        var obj = new BAL();
        return obj.LoadAll();
    }
    protected void GridViewCancelEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        var obj = new BAL();
        GridView1.EditIndex =-1;
        obj.LoadPerson(ref  GridView1);
 
    }
    protected void GridViewEdit(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        var obj = new BAL();
        GridView1.EditIndex = e.NewEditIndex;
        obj.LoadPerson(ref  GridView1);
 
    }
    protected void GridViewUpdate(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        
        GridView1.EditIndex = -1;
        var obj = new BAL();
        obj.UpdateGrid(ref GridView1, e.RowIndex);
    }
}

Now Add a class File in Your Application and write down in that below given code.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
 
/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
 
    public BAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void LoadPerson(ref GridView grd)
    {
 
        using (var objCon = new SqlConnection())
        {
            objCon.ConnectionString = ConfigurationManager.ConnectionStrings["cnGrid"].ConnectionString;
            if (objCon.State == ConnectionState.Closed)
                objCon.Open();
            using (var objAdp = new SqlDataAdapter("usp_PersonRecords", objCon))
            {
                objAdp.SelectCommand.CommandType = CommandType.StoredProcedure;
                var ds = new DataSet();
                objAdp.Fill(ds);
                grd.DataSource = ds.Tables[0];
                grd.DataBind();
            }
        }
 
    }
    public DataTable LoadAll()
    {
        dynamic objDt;
        using (var objCon = new SqlConnection())
        {
            objCon.ConnectionString = ConfigurationManager.ConnectionStrings["cnGrid"].ConnectionString;
            if (objCon.State == ConnectionState.Closed)
                objCon.Open();
            using (var objAdp = new SqlDataAdapter("usp_DeptRecords", objCon))
            {
                objAdp.SelectCommand.CommandType = CommandType.StoredProcedure;
                var ds = new DataSet();
                objAdp.Fill(ds);
                objDt = ds.Tables[0];
            }
        }
        return objDt;
    }
    public void UpdateGrid(ref GridView grd, int index)
    {
 
        var row = grd.Rows[index];
        var ddlDept = (DropDownList)row.FindControl("dropDownDept");
        var txtFname = (TextBox)row.FindControl("txtFirstName");
        var txtLname = (TextBox)row.FindControl("txtLastName");
        var isActive = (RadioButtonList)row.FindControl("radioActive");
        var chkAct = (CheckBox)row.FindControl("chkActive");
 
        var id = Convert.ToInt32(grd.DataKeys[row.RowIndex].Value);
        var FNAME = txtFname.Text.ToUpper();
        var lname = txtLname.Text.ToUpper();
        var deptID = Convert.ToInt32(ddlDept.SelectedItem.Value);
        var isactive = isActive.SelectedValue == "True" ? Convert.ToInt32(1) : Convert.ToInt32(0);
        using (var objCon = new SqlConnection())
        {
            objCon.ConnectionString = ConfigurationManager.ConnectionStrings["cnGrid"].ConnectionString;
            if (objCon.State == ConnectionState.Closed)
                objCon.Open();
            using (var cmd = new SqlCommand("usp_PersonUpdate", objCon))
            {
                cmd.CommandType = CommandType.StoredProcedure;
 
                var parm = new[]
                               {
                                   new SqlParameter("@PersonID", SqlDbType.Int, Int32.MaxValue) {Value =id},
                                   new SqlParameter("@FName", SqlDbType.VarChar, 100) {Value =FNAME },
                                   new SqlParameter("@LName", SqlDbType.VarChar, 100) {Value = lname},
                                   new SqlParameter("@DeptID", SqlDbType.Int, Int32.MaxValue) {Value =deptID},
                                   new SqlParameter("@IsActive", SqlDbType.Bit) {  Value = isactive}
                               };
                cmd.Parameters.AddRange(parm);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                LoadPerson(ref grd);
            }
        }
    }
}

Now  Run by pressing F5 remember that before run to this ensure your database and connection string  in web config file and change in that setting of your MSSQLServer

<connectionStrings>
		<add name="cnGrid" connectionString="Data Source=xxxx;Initial Catalog=dbGrid;Persist Security Info=True;User ID=xxx;Password=xxx;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>

Download Url : https://docs.google.com/file/d/0B-sDsBkcsM1YODBWaVZmLTJ6YlE/edit?usp=sharing