Split a cell

In an existing document I move to a bookmark located within a cell on a row (which contains just that one cell):

builder.movetobookmark("myBookmark")

How can I split that row into two cells, move to the new cell on the right, and place a bookmark in it called “myBookmark2”?

I’m having trouble with InsertCell() not generating the new cell in the same row as the existing cell.

Any help would be appreciated, thanks.

Hi Bill,

Thanks for your inquiry. Please attach the following sample resources here for testing:

  • Your input Word document
  • Your expected Word document showing the final result. You can create expected document using Microsoft Word.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you code to achieve the same using Aspose.Words. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

OK, see attached.

- Move to “myBookMark”
- Split the cell in which that bookmark is contained
- Size the new cell to about 1/3 the width of the row
- Move to that new cell
- Add a bookmak in that new cell called “myBookmark2”

Using Word you could simple use Split Cell. ASPOSE does not contain that function so I need to emulate that functionality.

Thanks…

Hi Bill,

Thanks for your inquiry. Please try using the following code:

Document doc = new Document(MyDir + @"ASPOSE_Sample_In.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Cell cell = (Cell)doc.Range.Bookmarks["myBookmark"].BookmarkStart.GetAncestor(NodeType.Cell);
if (cell != null)
{
    double width = cell.CellFormat.PreferredWidth.Value;
    double newCellWidth = width / 3;
    cell.CellFormat.Width = (width - newCellWidth);
    Cell newCell = (Cell)cell.Clone(true);
    cell.ParentRow.InsertAfter(newCell, cell);
    newCell.CellFormat.Width = (newCellWidth);
    newCell.EnsureMinimum();
    builder.MoveTo(newCell.FirstParagraph);
    builder.StartBookmark("myBookmark2");
    builder.EndBookmark("myBookmark2");
}
doc.Save(MyDir + @"16.3.0.doc");

Hope, this helps.

Best regards,

Awais, thanks.

Seems pretty easy and it almost works. But it seems to effect (resizes cells) other rows in the doc as well, not just the intended row.

To better explain the problem see attached. In your code, substitute “myBookmark” for an existing bookmark in this doc called “OtherProfessionalQualifications” and you can see how it oddly effects other cells in other rows.

Hi Bill,

Thanks for your inquiry. I have generated a DOCX file using Aspose.Words 16.3.0 and this code and attached it here for your reference. Please create a comparison screenshot highlighting (encircle) the problematic areas in this Aspose.Words generated DOCX and attach it here for our reference. Also, please attach your expected Word document corresponding to ASPOSE_Sample2.doc which shows the final result. You can create expected document using Microsoft Word. We will investigate the issue further on our end and provide you more information.

Best regards,

I’ve used arrows on the attached screen shot to point to the cells that are changed when I run the code you supplied. Same “in” document that I had uploaded previously.

Only the cell containing the “myBookmark” should be modifed yet the code effects all the other row in the table.

Hi Bill,

Thanks for your inquiry. We are checking this scenario and will get back to you soon.

Best regards,

Did you folks look any further into this? Solution?

Hi Bill,

Thanks for your inquiry. I have used 16.4.0 version of Aspose.Words, produced another doc file and attached it here for your reference. I am unable to observe any issue in MS Word 2016 on my side. Please upgrade to latest version of Aspose.Words. Hope, this helps.
https://releases.aspose.com/words/net/

Best regards,