【问题标题】:Set cell alignment and (alternate) background row color with iTextSharp使用 iTextSharp 设置单元格对齐和(备用)背景行颜色
【发布时间】:2017-08-04 04:40:41
【问题描述】:

我正在从数据库中读取数据,并使用 iTextSharp 填充表。这是我读取数据的方式:

var db = Database.Open("NewDatabase");
var sql = 
    @"SELECT    FullName,
                [2017-01-02], [2017-01-03], [2017-01-04],
                ([2017-01-02] + [2017-01-03] + [2017-01-04]) as 'DatesSum',
                [Week1]
    FROM        [NewTable]";

var data = db.Query(sql);
var columns = data.First().Columns;                                                                          
var doc = new Document();                                                                                   
PdfWriter.GetInstance(doc, Response.OutputStream);                                                          
doc.Open();
PdfPTable table = new PdfPTable(new float[] {30f, 10f, 10f, 10f, 10f, 10f});

foreach(var row in data)                                                        
{
    foreach(var column in columns)
    {
        table.AddCell(new Phrase(row[column] != null ? row[column].ToString() : string.Empty));
    }
}

doc.Add(table);
doc.Close();

如您所见,我使用“foreach”循环数据并填充表格。这是我知道的填充表格的唯一方法(我对 iTextSharp 还很陌生)。

我一直在尝试将数据对齐到单元格的中间。另外,我想交替行的颜色(每隔一行)。我一直在循环内尝试这样的事情,但没有结果:

row[column].HorizontalAlignment = 1;

参考:https://www.mikesdotnetting.com/article/205/exporting-the-razor-webgrid-to-pdf-using-itextsharp

【问题讨论】:

    标签: c# .net itext


    【解决方案1】:

    设置PdfPCell HorizontalAlignmentBackgroundColor 属性。

    样本数据:

    static readonly int[,] data = new int[,] 
    { 
        { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }
    };
    static readonly int rows = data.GetLength(0);
    static readonly int columns = data.GetLength(1);
    

    创建 PDF:

    var table = new PdfPTable(2) 
    { 
        HorizontalAlignment = Element.ALIGN_LEFT,
        WidthPercentage = 50 
    };
    var cell = new PdfPCell() {HorizontalAlignment = Element.ALIGN_CENTER};
    using (var stream = new MemoryStream())
    {
        using (var document = new Document())
        {
            PdfWriter.GetInstance(document, stream);
            document.Open();
    
            for (int i = 0; i < rows; ++i)
            {
                cell.BackgroundColor = i % 2 == 0
                    ? BaseColor.LIGHT_GRAY : BaseColor.WHITE;
                for (int j = 0; j < columns; ++j)
                {
                    cell.Phrase = new Phrase(data[i, j].ToString());
                    table.AddCell(cell);
                }
            }
            document.Add(table);
        }
        File.WriteAllBytes(OUTPUT, stream.ToArray());
    }
    

    输出:

    【讨论】:

    • 谢谢 kuujinbo。我将如何将您的答案应用于数据库查询,例如我自己的案例?我看到您这样做了static readonly int rows = data.GetLength(0);,但我不确定如何将其转换为我的案例(您创建了自己的表,而我从数据库中获取了它)。或者您可能知道在我的foreach 循环中执行此操作的方法?
    猜你喜欢
    • 2015-06-06
    • 2010-11-21
    • 2017-03-26
    • 2012-03-23
    • 1970-01-01
    • 2012-06-06
    • 2019-10-21
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多