1

I have Document file with header and footer part. In the footer portion I have one table.

So now I am trying insert text into Table cell. But whenever I try to do by this code it will be append paragraph type and change the height and weight of the table .

  using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ORiFilepath, true))
        {
         var docPart = wordDoc.MainDocumentPart;
            MainDocumentPart mainPart = wordDoc.MainDocumentPart;
            if (docPart.FooterParts.Count() > 0)
            {
                //List<Table> table = docPart.FooterParts..Elements<Table>().ToList();
                foreach (FooterPart footer in docPart.FooterParts)
                {
                    List<Table> table = footer.Footer.Elements<Table>().ToList();
                    if (table.Count > 0)
                    {
                        var footertable = table[0];
                        TableRow row1 = footertable.Elements<TableRow>().ElementAt(1);
                        string text1 = cell1.InnerText;
                        if (text1 == "")
                        {
                            cell1.Append(new Paragraph(new Run(new Text("TEXT"))));
                           
                        }
                    }
                }
            }
        }

can I insert text into the cell Using

cell1.Append(new Paragraph(new Run(new Text("TEXT"))));,

or What should be the method ?

macropod
  • 12,757
  • 2
  • 9
  • 21

1 Answers1

0

Here's what I use to add text to a cell. Hopefully it's useful.

The CreateCellProperties and CreateParagraph methods just encapsulate the corresponding objects in OpenXML SDK (TableCellProperties and ParagraphProperties) which you don't really need to write text.

Let me know if you still have problems:

         public TableCell CreateCell(Shading shading, int width, int gridSpan, 
            TableCellBorders borders, TableVerticalAlignmentValues valign,
            JustificationValues justification, Color color, string text, 
            VericalMergeOptions mergeOptions)

        {
            // Set up Table Cell and format it.
            TableCell cell = new TableCell();
            

            TableCellProperties cellProperties = CreateCellProperties(shading, width, gridSpan, borders, valign, mergeOptions);
            ParagraphProperties props = CreateParagraphProperties(justification, color);
            
            // Set cell and paragraph
            Run run = CreateRun(color, text);
            Paragraph paragraph = new Paragraph(new List<OpenXmlElement>
            {
                props,
                run
            });

            // Append the run and the properties
            cell.Append(new List<OpenXmlElement>
            {
                cellProperties,
                paragraph
            });
            return cell;
        }

        protected Run CreateRun(Color color, string text = "")
        {

            Run run = new Run(new List<OpenXmlElement>{
                new RunProperties(color.CloneNode(true)),
            });

            if (text != "") run.Append(new Text(text));

            return run;
        }
Rendition
  • 494
  • 2
  • 12