如何在PDF中合并具有相同值的行的前3列 [英] How to merge first 3 columns for rows with same value in PDF

查看:187
本文介绍了如何在PDF中合并具有相同值的行的前3列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using itextSharp to export a DataTable to a pdf table. I can export the data to a pdf table using the sample code i have posted below. The DataTable contains close to 21 columns.

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

The first column in the pdf (DataTable) might contain similar values for any number of rows. If the data values in first column for a group of rows is similar, i want to merge the first 3 columns of those rows as one cell.

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

I'm having trouble modifying the code below to achieve this.

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;
    }


推荐答案

我只是编辑您的代码,未检查

try this. i just edited your code, not checked

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

here i have edited 1 line of your code and added a new line (total 2 lines of change only).

不要忘记组合头像行,如果你需要

dont forget to combine headding row like this, if you need

/* 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);
            }
        }

这篇关于如何在PDF中合并具有相同值的行的前3列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆