c# - Word OpenXML remove padding inside table -


i using word open xml.

rowcopy.descendants<tablecell>().elementat(0).append(new paragraph(new run(new text(dr["name"].tostring())))); 

the code writing name cell in table. creating top , bottom padding inside cell. how can remove it. because of new paragraph? new word open xml.

enter image description here

[![enter image description here][2]][2]

if have existing docx file empty table in might find there empty paragraph in each cell. using append adding new paragraph after empty 1 causes space @ top of cell looks padding.

given require new text in cell can remove existing paragraph elements before add new paragraph calling removeallchildren on cell (or table if you're confident don't need on table):

tablecell cell = body.descendants<tablecell>().elementat(0); cell.removeallchildren<paragraph>(); cell.append(new paragraph(new run(new text(dr["name"].tostring())))); 

if isn't problem can control padding editing tablecellmargin. following should work:

if (cell.tablecellproperties != null && cell.tablecellproperties.tablecellmargin != null) {     cell.tablecellproperties.tablecellmargin.bottommargin = new bottommargin() { width = "0" };     cell.tablecellproperties.tablecellmargin.topmargin = new topmargin() { width = "0" }; } 

edit

the full code listing this:

static void adddatatotable(string filename) {     using (wordprocessingdocument worddoc =             wordprocessingdocument.open(filename, true))     {         var body = worddoc.maindocumentpart.document.body;         var paras = body.elements<tablecell>();          tablecell cell = body.descendants<tablecell>().elementat(0);         cell.removeallchildren<paragraph>();         cell.append(new paragraph(new run(new text(dr["name"].tostring()))));          if (cell.tablecellproperties != null && cell.tablecellproperties.tablecellmargin != null)         {             cell.tablecellproperties.tablecellmargin.bottommargin = new bottommargin() { width = "0" };             cell.tablecellproperties.tablecellmargin.topmargin = new topmargin() { width = "0" };         }          worddoc.close(); // close template file     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -