Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert table at the specified location in word document #606

Closed
zhangymsir opened this issue Aug 3, 2021 · 7 comments
Closed

Insert table at the specified location in word document #606

zhangymsir opened this issue Aug 3, 2021 · 7 comments

Comments

@zhangymsir
Copy link

Npoi version: 2.5.4
Description: For example, insert a table between two more paragraphs or directly replace self defined text placeholder with tables,If there is a solution,please give advice or comments, many thanks.

@tonyqus tonyqus added this to the NPOI 2.5.6 milestone Oct 8, 2021
@tonyqus
Copy link
Member

tonyqus commented Oct 8, 2021

However, XmlCursor in NPOI is not correctly implemented.

XWPFTable InsertNewTbl(/*XmlCursor*/XmlDocument cursor);

InsertNewTbl should have a XmlCursor argument instead

@tonyqus tonyqus modified the milestones: NPOI 2.5.6, NPOI 2.5.7 Mar 1, 2022
@tonyqus tonyqus modified the milestones: NPOI 2.6.1, NPOI 2.6.2 Jun 8, 2023
@constantinje
Copy link

I need this too.
Do you have a due date for this enhancement ?
I hope no having the need to use another library

@tonyqus
Copy link
Member

tonyqus commented Jun 10, 2023

There is no same thing like XmlCursor in .NET. It's the weakness of .NET xml library. I'm afraid this feature will NOT be available until 2024.

@cbs-cbt
Copy link

cbs-cbt commented Dec 27, 2023

Hello,
I also have this problem.

This question has been asked several times:

Here's how I solved it by modifying the NPOI library.

I just added an optional argument pos which is the position of the table in the document. For example, if you want to insert it before a paragraph, you can use XWPFDocument.GetPosOfParagraph to get the position of the paragraph and then use that value in the pos argument of CreateTable

Two functions need to be modified.

NPOI.XWPF.UserModel.XWPFDocument.CreateTable:

public XWPFTable CreateTable(int rows, int cols, int? pos = null)
{
    XWPFTable table = new XWPFTable(ctDocument.body.AddNewTbl(pos), this, rows, cols);
    
    if(pos.HasValue)
    {
        bodyElements.Insert(pos.Value, table);
    }
    else
    {
        bodyElements.Add(table);
    }
    
    tables.Add(table);
    
    return table;
}

NPOI.OpenXmlFormats.Wordprocessing.CBT_Body.AddNewTbl:

public CT_Tbl AddNewTbl(int? pos = null)
{
  CT_Tbl tbl = new CT_Tbl();

  lock (this)
  {
    if(pos.HasValue)
    {
      this.itemsField.Insert(pos.Value, tbl);
      this.itemsElementNameField.Insert(pos.Value, DocumentBodyItemChoiceType.tbl);
    }
    else
    {
      this.itemsField.Add(tbl);
      this.itemsElementNameField.Add(DocumentBodyItemChoiceType.tbl);
    }
  }

  return tbl;
}

I tested this code briefly with a basic document that my program modifies by inserting a table in place of a paragraph and it works, but that's the only test I've done.
Do you think that these modifications are sufficient to properly define the position of a table in the document?

@tonyqus tonyqus closed this as completed in 5428cfd Mar 9, 2024
@tonyqus
Copy link
Member

tonyqus commented Mar 9, 2024

@cbs-cbt Thank you for your code contribution. I think it is workable although it's not as powerful as XmlCursor.

While using XmlCursor, it can follow the position of the specific paragraph

cursor = p.getCTP().newCursor();
XWPFTable t2 = document.insertNewTbl(cursor);

@tonyqus tonyqus changed the title How to insert table at the specified location in word document Insert table at the specified location in word document Mar 12, 2024
@tonyqus
Copy link
Member

tonyqus commented Mar 12, 2024

@cbs-cbt I put your name in the contributor list in release notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants