在C#中增加PDF中两列的宽度

时间:2018-08-08 17:28:08

标签: c# asp.net itext

我正在使用Itextsharp下载PDF。我需要增加pdf中两列的宽度。我尝试使用celcols.width,但是它不起作用。现在所有列都具有相同的宽度。任何帮助将不胜感激。谢谢您。

 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)
        {

        }
    }

在这里,我需要增加第二列和第三列的宽度,即名称和用户

1 个答案:

答案 0 :(得分:1)

您应该设置列的宽度。您可以通过将它们应用于表对象来实现。例如:

float[] widths = new float[] { 1f, 2f }; //relative col widths in proportions - 1/3 and 2/3
table.SetWidths(widths);

您可以访问此链接以引用更多信息:https://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables 希望对我有帮助!