【问题标题】:How to merge first 3 columns for rows with same value in PDF如何合并PDF中具有相同值的行的前3列
【发布时间】:2016-04-12 09:01:03
【问题描述】:

我正在使用 itextSharp 将 DataTable 导出到 pdf 表。我可以使用我在下面发布的示例代码将数据导出到 pdf 表。 DataTable 包含近 21 列。

pdf (DataTable) 中的第一列可能包含任意行数的相似值。如果一组行的第一列中的数据值相似,我想将这些行的前 3 列合并为一个单元格。

我无法修改下面的代码来实现这一点。

public iTextSharp.text.Table GetItextTable(DataTable dtChartData, string reportType)
    {
        int intCols = dtChartData.Columns.Count; //Total number of columns 
        int intRows = dtChartData.Rows.Count; //Total number of rows 
        iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(intCols, intRows);
        try
        {
            pdfTable.BorderWidth = 1;
            pdfTable.Width = 100;
            pdfTable.Padding = 1;
            pdfTable.Spacing = 1;
            /*creating table headers */
            for (int i = 0; i < intCols; i++)
            {

                iTextSharp.text.Cell cellCols = new iTextSharp.text.Cell();
                iTextSharp.text.Font ColFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07,
                    iTextSharp.text.Font.BOLD);
                for (int l = 0; l < dtChartData.Columns.Count; l++)
                {
                    if (dtChartData.Columns[l].ColumnName.Contains("_"))
                    {
                        dtChartData.Columns[l].ColumnName = dtChartData.Columns[l].ColumnName.Replace("_", " ");
                    }
                }
                iTextSharp.text.Chunk chunkCols = new iTextSharp.text.Chunk(dtChartData.Columns[i].ColumnName,ColFont);
                cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                if ((chunkCols.ToString().ToLower() == "ReportDetails"))
                {
                    cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                }
            }
            /* loop that take values from every row in datatable and insert in itextsharp table */
            for (int k = 0; k < intRows; k++)
            {
                for (int j = 0; j < intCols; j++)
                {
                    iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
                    iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
                    iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);

                    cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                    cellRows.Add(chunkRows);
                    pdfTable.AddCell(cellRows);
                }
            }
        }
        catch (Exception ex)
        {
            //error handling code here removed
        }
        return pdfTable;
    }

【问题讨论】:

    标签: c# c#-4.0 itextsharp


    【解决方案1】:

    您是否尝试过更改Colspan

    iTextSharp.text.Cell cell = new iTextSharp.text.Cell();
    cell.AddElement(new Paragraph("colspan 3"));
    cell.Colspan = 3;
    table.AddCell(cell);
    

    在这种情况下,cell 将跨越三列。

    【讨论】:

    • 我遇到的麻烦是检查行第一列是否具有相同值的逻辑。
    • 这不是 iTextSharp 的问题。 iTextSharp 不会为您进行检查。检查字符串a 是否与字符串b 相同,以及该字符串b 是否与字符串c 相同,这应该不是问题。你试过什么?为什么它不起作用?
    • 我同意布鲁诺的观点。我还要补充一点,在这个阶段,我建议您暂时停止使用 iTextSharp。相反,只关注DataTable。如果它来自 SQL 系统,您可能可以向它抛出一些 SQL 逻辑来获得您想要的结果。如果您只使用 C#,则在您的 DataTable 上创建一个快速的 WinForm 或 ASP.Net WebForm 视图并应用您需要的任何逻辑。一旦你有了它,你就可以轻松地将它捕捉到你发布的代码中以使用 iTextSharp。
    【解决方案2】:

    试试这个。我只是编辑了你的代码,没有检查

    这里我编辑了您的代码的 1 行并添加了一个新行(总共 2 行更改)。

    如果需要,不要忘记像这样组合标题行

    /* loop that take values from every row in datatable and insert in itextsharp table */
            for (int k = 0; k < intRows; k++)
            {
                for (int j = 2; j < intCols; j++)// EDIT: if all first 3 cols are same, then starts with 2
                { 
                    iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
                    if(j == 2) cellRows.Colspan = 3;// ADD: it'll gives a 3 times long cell
                    iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
                    iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);
    
                    cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                    cellRows.Add(chunkRows);
                    pdfTable.AddCell(cellRows);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2020-05-13
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多