-1

I need to pass the value of a new cell. this is because I need to make the same logic as in ItextSharp, that logic being that I need to be able to create a table only by adding cells.

Instead of adding rows and then adding the cells to the rows.

var cellValue = new Cell();
((Row)Rows.LastObject).Cells[i] = cellValue;

public void Add(PdfPCell cellValue)
{
    MigraRow row = null;
    if (currentRowCellIndex == Columns.Count || Rows.Count == 0)
    {
        row = this.AddRow();
        currentRowCellIndex = 0;
    }

    currentRowCellIndex += cellValue.Colspan;
    int cellCount = ((MigraRow)Rows.LastObject).Cells.Count;
    int celIndex = cellCount == 0 ? 0 : cellCount - 1;

    var cell = ((MigraRow)Rows.LastObject).Cells[celIndex];
    cell = cellValue.Clone();
    cell.Elements = cellValue.Elements;

    //((MigraRow)Rows.LastObject).Cells[celIndex]. = cellValue.Borders.Clone();
    //((MigraRow)Rows.LastObject).Cells[celIndex].Borders = cellValue.Borders.Clone();
    //((MigraRow)Rows.LastObject).Cells[celIndex].Elements = cellValue.Elements.Clone();
    //((MigraRow)Rows.LastObject).Cells[celIndex].Shading.Color = cellValue.BackgroundColor;
    //((MigraRow)Rows.LastObject).Cells[celIndex].MergeRight = cellValue.Colspan;
    //para = ((Paragraph)cell.Elements.LastObject).Clone();
}

this solution doesnt work because ...Cells[i]... is read only. but I need to do something akin to this, is it possible?

As you can see I know that I can pass the values in each property to the Cells[i], but I don't like that solution.

Ps. in the presented code the PdfPCell is just a MigraDoc cell with some added functions by me, it can be used as a regular MigraDoc cell.

Erick Santander
  • 311
  • 3
  • 17
  • if the `Cells[i]` is read-only then you can't set a value to it. It must be set somewhere else. Can you include some more code? This isn't terribly descriptive. –  Sep 10 '19 at 18:18
  • I know that the Cells[i] is read only, I'm asking if there is some other way to do this – Erick Santander Sep 11 '19 at 08:37
  • MigraDoc is not iTextSharp. You can either use MigraDoc as intended - or add a setter to overcome the "read only" limitation since MigraDoc is open source. Instead of creating a PdfPCell and changing that you could pass a reference to the cell created by MigraDoc to your code to change the contents. This should be a simple change. – I liked the old Stack Overflow Sep 11 '19 at 12:03
  • it would be a simple change, if it wasn't about 40k lines of code to change, of a pdf writing class . I also said that to my bosses but it isn't viable since they don't want to change a lot of the code (thats the why the cell is called PdfPCell, its basically a migradoc with another name). I just wanted to know if there was any way to do what I want. – Erick Santander Sep 11 '19 at 13:18

1 Answers1

0

Answer to the current question:
MigraDoc is not iTextSharp. You can either use MigraDoc as intended - or add a setter to overcome the "read only" limitation since MigraDoc is open source. Instead of creating a PdfPCell and changing that you could pass a reference to the cell created by MigraDoc to your code to change the contents. This should be a simple change.

Answer to the original question:
You don't have to call new Cell() - the cell will be created automatically when you call AddRow() for the table.

The code looks much nicer then without casts and such:

Row row = table.AddRow();
Cell cell = row.Cells[0];

Sample code:
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

  • yes, I know that, but I need to make the value of row.Cells[0] = the value of cell, I'll update the question to make it clearer. – Erick Santander Sep 11 '19 at 08:38