Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中增加PDF中两列的宽度#_C#_Asp.net_Itext - Fatal编程技术网

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

C# 在c中增加PDF中两列的宽度#,c#,asp.net,itext,C#,Asp.net,Itext,我正在使用Itextsharp下载一个PDF。我需要增加PDF中两列的宽度。我曾尝试使用celcols.width,但它不起作用。现在所有列的宽度都相同。如果您有任何帮助,我们将不胜感激。提前感谢 protected void Button6_Click(object sender, EventArgs e) { try { string fromdate = "", todate = ""; strin

我正在使用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)
        {

        }
    }
protectedvoid按钮6\u单击(对象发送者,事件参数e)
{
尝试
{
字符串fromdate=“”,todate=“”;
字符串compgrp=“全部”;
var pdfDoc=新文档(PageSize.A3、10f、10f、10f、0f);
System.IO.MemoryStream mStream=新的System.IO.MemoryStream();
PdfWriter writer=PdfWriter.GetInstance(pdfDoc,mStream);
PdfPageEventHelper pageEventHelper=新的PdfPageEventHelper();
writer.PageEvent=pageEventHelper;
HeaderFooter header=newheaderfooter(新短语(“日程报告”+DateTime.Now.ToString(“dd/MMM/yyyy hh:mm tt”)+”),false);
header.BackgroundColor=new iTextSharp.text.Color(System.Drawing.colorplator.FromHtml(“#62e456”);
//删除默认设置的边框
header.Border=iTextSharp.text.Rectangle.TITLE;
//对齐文本:0向左,1居中,2向右。
header.Alignment=Element.ALIGN\u CENTER;
pdfDoc.Header=头;
//标题。
pdfDoc.Open();
string Connectionstring=ConfigurationManager.Connectionstring[“SPCFConnectionString”]。Connectionstring;
SqlConnection cn=新的SqlConnection(Connectionstring);
cn.Open();
尝试
{
if(ddl\u clntsearch.SelectedValue.ToString()=“”)
{
compgrp=“全部”;
}
其他的
{
compgrp=ddl_clntsearch.SelectedValue.ToString();
}
字符串[]querylist=新字符串[]
{
“exec[SP0480_05]”、“+compgrp+”、“+txt_mobnum.Text+”、“+txt_searchdate.Text+”、“+ddl_status.SelectedValue.ToString()+”,
};
字符串[]头=新字符串[]
{
“日期”、“姓名”、“轮班时间”、“用户”、“备注”、“金额”、“签名”
};
foreach(querylist中的字符串查询)
{
iTextSharp.text.Table pdfTableheader=新的iTextSharp.text.Table(7,1);
pdfTableheader.BorderWidth=1;pdfTableheader.Width=100;pdfTableheader.Spacing=1;
//pdfTableheader.Padding=1;
foreach(标题中的字符串项)
{
Cell cellCols=新单元格();
Chunk chunkCols=新Chunk();
cellCols.BackgroundColor=new iTextSharp.text.Color(System.Drawing.colorplator.FromHtml(“#62e05e”);
iTextSharp.text.Font ColFont=FontFactory.GetFont(FontFactory.TIMES_-ROMAN,12,iTextSharp.text.Font.BOLD,iTextSharp.text.Color.BLACK);
//如果(项目==“名称”)
//{
//单元宽度=300f;
//}
//否则
//{
//单元宽度=100f;
//}
chunkCols=新块(项,ColFont);
cellCols.Add(chunkCols);
pdfTableheader.AddCell(cellCols);
}
pdfDoc.Add(pdfTableheader);
SqlCommand cmd=新的SqlCommand(查询,cn);
cmd.CommandTimeout=30;
cmd.CommandType=CommandType.Text;
SqlDataReader dr=cmd.ExecuteReader();
DataTable=新的DataTable();
数据表负载(dr);
object[]数组=新对象[dataTable.Columns.Count];
//dataTable.Rows.Add(数组);
int cols=dataTable.Columns.Count;
int rows=dataTable.rows.Count;
iTextSharp.text.Table pdfTable=新的iTextSharp.text.Table(列、行);
pdfTable.BorderWidth=1;pdfTable.Width=100;pdfTable.Spacing=2;
//pdfTable.Padding=1;
//创建表数据(实际结果)

对于(int k=0;k您应该设置列的宽度。您可以通过将它们应用于表对象来实现这一点。例如:

float[] widths = new float[] { 1f, 2f }; //relative col widths in proportions - 1/3 and 2/3
table.SetWidths(widths);
您可以访问此链接以了解更多信息:
希望能有所帮助,我的朋友!

这是正确的,因此投票结果是向上的。不幸的是,Mike Dotnetting已经过时了,因为它仍然在谈论iText 5。从iText 7开始,您不必担心设置列的宽度。在iText 7中,列根据其内容调整其宽度。@BrunoLowagie:谢谢您的建议,我的朋友朋友:))