Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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_Itext - Fatal编程技术网

C# 多行的常量列

C# 多行的常量列,c#,pdf,itext,C#,Pdf,Itext,我正在向pdf添加一个表。我有3行3列。我希望第一列作为所有行的单个单元格只显示一次。我该怎么做?我的代码如下。如图所示,我的输出应该与公司栏中的德勤一样: PdfPTable table = new PdfPTable(dt.Columns.Count); PdfPRow row = null; float[] widths = new float[] { 4f, 4f, 4f }; table.SetWidths(widths)

我正在向pdf添加一个表。我有3行3列。我希望第一列作为所有行的单个单元格只显示一次。我该怎么做?我的代码如下。如图所示,我的输出应该与公司栏中的德勤一样:

        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);

        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);
我书中的例子正是你所需要的。移植到C#,它看起来像这样:

        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
您可以查看生成的PDF。在你的情况下,你需要

        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);
cell.Rowspan = 6;

对于具有德勤价值的单元格。

您使用的是什么库?将其添加为标记,并将PDF添加到您的问题中,因为它现在听起来很像DB。您是否尝试了
PdfPCell
上的
RowSpan
        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);