Skip to content

Downloading Problem in PDF File ARABIC FONT

onais ahmer

Asked Dec 30, 2018, 7:53 AM EST1 answer
Share
LinkedInX
 
Java
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Sample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = @"Data Source= .; Initial Catalog=Temp_DB; User ID=sa; Password=mbl@1234";
                //System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }

        public void PDF()
        {
            string selectedValue = string.Empty;

            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {

                if (CheckBoxList1.Items[i].Selected)
                {
                    selectedValue += CheckBoxList1.Items[i].Value + ",";
                }
            }
            string a = selectedValue;
            string Something = selectedValue.TrimEnd(',');
            string strQuery = "select " + Something + " from USER_LOG_TBL";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            GridView GridView11 = new GridView();
            GridView11.AllowPaging = false;
            GridView11.AutoGenerateColumns = false;
            BoundField boundfield = new BoundField();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                boundfield.DataField = dt.Columns[i].ColumnName.ToString();
                boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
                GridView11.Columns.Add(boundfield);
            }
            GridView11.DataSource = dt;
            GridView11.DataBind();
            using (MemoryStream ms = new MemoryStream())
            {
                Document document = new Document(iTextSharp.text.PageSize.A4, 10, 22, 34, 34);
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                document.Open();
                MemoryStream stream = new MemoryStream();
                document.Add(createFirstTable(dt));
                document.Close();
                writer.Close();
                Response.ContentType = "pdf/application";
                Response.AddHeader("content-disposition", "attachment;filename=First PDF document.pdf");
                Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            }
        }

        public static PdfPTable createFirstTable(DataTable dt)
        {
            PdfPTable table = new PdfPTable(dt.Columns.Count);
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                table.AddCell(dt.Columns[j].ToString());
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    table.AddCell(dt.Rows[i][k].ToString());
                }
            }
            return table;
        }

        protected void ExportToPDf(object sender, EventArgs e)
        {
            PDF();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    }
}
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.