在C#中增加PDF中两列的宽度 [英] Increase the width of two columns in PDF in C#

查看:88
本文介绍了在C#中增加PDF中两列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用Itextsharp下载PDF。我需要增加pdf中两列的宽度。我尝试过使用celcols.width,但它无法正常工作。现在所有的列有相同的宽度。任何帮助将非常感谢。谢谢你提前。







这里我需要增加第2和第4列的宽度,即名称和用户。任何帮助将非常感谢。谢谢。



我尝试过什么:



Hi all,
I am downloading a PDF using Itextsharp .I need to increase the width of two columns in pdf.I have tried using celcols.width ,however its not working.Now all the columns have same width.Any help will be really appreciated.Thanks in Advance`.



Here I need to increase the width of 2nd and 4rd column ie Name and User .Any help will be really appreciated .Thanks in advance .

What I have tried:

<pre><pre>protected void Button6_Click(object sender, EventArgs e)
    {
        try
        {

            string fromdate = "", todate = "";
            string compgrp = "All";


            var pdfDoc = new Document(PageSize.A3, 10f, 10f, 10f, 0f);
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
            PdfPageEventHelper pageEventHelper = new PdfPageEventHelper();
            writer.PageEvent = pageEventHelper;
            HeaderFooter header = new HeaderFooter(new Phrase("Schedule report" + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + ""), false);
            header.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#62e456"));
            // Remove the border that is set by default  
            header.Border = iTextSharp.text.Rectangle.TITLE;
            // Align the text: 0 is left, 1 center and 2 right.  
            header.Alignment = Element.ALIGN_CENTER;
            pdfDoc.Header = header;
            // Header.  
            pdfDoc.Open();
            string Connectionstring = ConfigurationManager.ConnectionStrings["SPCFConnectionString"].ConnectionString;
            SqlConnection cn = new SqlConnection(Connectionstring);
            cn.Open();
            try
            {
                if (ddl_clntsearch.SelectedValue.ToString() == "")
                {
                    compgrp = "All";
                }
                else
                {
                    compgrp = ddl_clntsearch.SelectedValue.ToString();
                }
                string[] querylist = new string[]
                     {
                                 "exec [SP0480_05] '" + compgrp +"','" + txt_mobnum.Text +"','"+txt_searchdate.Text+"','" + ddl_status.SelectedValue.ToString() +"'",

                     };

                string[] Headers = new string[]
                     {
                         "Date"," Name","Shift time","User","Remarks","Amount","Signature"
                     };

                foreach (string query in querylist)
                {
                    iTextSharp.text.Table pdfTableheader = new iTextSharp.text.Table(7, 1);
                    pdfTableheader.BorderWidth = 1; pdfTableheader.Width = 100; pdfTableheader.Spacing = 1;
                    //pdfTableheader.Padding = 1;
                    foreach (string items in Headers)
                    {
                        Cell cellCols = new Cell();
                        Chunk chunkCols = new Chunk();
                        cellCols.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#62e05e"));
                        iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK);
                        //if (items == "Name")
                        //{
                        //    cellCols.Width = 300f;
                        //}
                        //else
                        //{
                        //    cellCols.Width = 100f;
                        //}
                        chunkCols = new Chunk(items, ColFont);

                        cellCols.Add(chunkCols);
                        pdfTableheader.AddCell(cellCols);
                    }
                    pdfDoc.Add(pdfTableheader);
                    SqlCommand cmd = new SqlCommand(query, cn);
                    cmd.CommandTimeout = 30;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    DataTable dataTable = new DataTable();
                    dataTable.Load(dr);
                    object[] array = new object[dataTable.Columns.Count];

                    //dataTable.Rows.Add(array);
                    int cols = dataTable.Columns.Count;
                    int rows = dataTable.Rows.Count;
                    iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
                    pdfTable.BorderWidth = 1; pdfTable.Width = 100; pdfTable.Spacing = 2;
                    //pdfTable.Padding = 1; 

                    //creating table data (actual result)   

                    for (int k = 0; k <= rows - 1; k++)
                    {
                        for (int j = 0; j < cols; j++)
                        {


                            Cell cellRows = new Cell();
                            //if (k % 2 == 0)
                            cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#ffffff"));

                            iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA,12);
                            Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
                            cellRows.Add(chunkRows);

                            pdfTable.AddCell(cellRows);
                        }
                    }

                    pdfDoc.Add(pdfTable);
                }
            }
            catch (Exception exc)
            {

            }

            //creating table headers  
            cn.Close();
            pdfDoc.Close();

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename=REF-{0}.pdf", "Report"));
            Response.BinaryWrite(mStream.ToArray());


        }
        catch (Exception ex)
        {

        }
    }

推荐答案

在标题上设置宽度,但不在数据单元格上设置。
You set the "width" on the "header", but not on the "data cells".


这篇关于在C#中增加PDF中两列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆