本文介绍了如何使用字符串生成器附加设置所有(五列)数据表列的不同宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am Generating PDF using ItextSmart on WEB APPLICATION,
Pdf is Generated from Database,

I want to Set Width of Columns Name & Value Like this,
Example to Generate PDF,

Sr No(Verry Small)| Description(Big) | Unit Rate(Medium) | Qty(Verry Small)| Price(Med)





我的尝试:





What I have tried:

sb.Append("<tr>");
                for (int i = 0; i < dt2.Columns.Count; i++)
                {                    
                    string width = i == 1 ? "5" : "1"; // first column will be 50% and others 20%
                    sb.Append("<td colspan=" + width + " >");                   
                    sb.Append(dt2.Columns[i].ColumnName);
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
                foreach (DataRow row in dt2.Rows)
                {
                    sb.Append("<tr>");                   
                    for (int i = 0,j = 0; i < dt2.Columns.Count; i++,j++)
                    {
                        string width = i == 1 ? "5" : "1";                       
                        sb.Append("<td  colspan=" + width + ">");                        
                        sb.Append(row[dt2.Columns[i].ColumnName]);
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
                }

推荐答案


using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.IO;

namespace B13
{ 
    class Program
    { 
        static void Main(string[] args)
        { 
            Document document = new Document(PageSize.A4, 10, 10, 10, 10);
            var pdfPath = @"D:\Projects\CP\CP\bin\Debug\aa.pdf";
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.CreateNew));
            document.Open();

            DataTable dt = new DataTable();
            //Sr No(Verry Small)| Description(Big) | Unit Rate(Medium) | Qty(Verry Small)| Price(Med)
            dt.Columns.Add("Sr No");
            dt.Columns.Add("Description");
            dt.Columns.Add("Unit Rate");
            dt.Columns.Add("Qty");
            dt.Columns.Add("Price");
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            PdfPTable table = new PdfPTable(dt.Columns.Count);
            float[] widths = new float[] { 20f, 60f, 20f, 30f, 30f };
            table.SetWidths(widths);
            foreach (DataColumn col in dt.Columns)
            { 
                table.AddCell(col.ColumnName);
            }        
                

            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {
                    var value = row[col];
                    table.AddCell(value.ToString());
                }
            } 

            document.Add(table);
            document.Close();


        }
    }
}





参考 []


这篇关于如何使用字符串生成器附加设置所有(五列)数据表列的不同宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:15