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

Add docx comment support #1006

Merged
merged 5 commits into from
Feb 2, 2023
Merged

Add docx comment support #1006

merged 5 commits into from
Feb 2, 2023

Conversation

9rnsr
Copy link
Contributor

@9rnsr 9rnsr commented Jan 30, 2023

This is my first contribution to NPOI. I'm not sure about any contributing process that I should follow, please give me advice.

When I've tried to modify comments in docx file, the feature was not yet implemented. Porting code from POI was not so difficult.

@tonyqus tonyqus added this to the NPOI 2.6.1 milestone Jan 30, 2023
@tonyqus
Copy link
Member

tonyqus commented Jan 30, 2023

Thank you for your contribution.

I notice that there is no real code change but formatting changes and remove namespaces in the following files
XWPFDocument.cs
XWPFRelation.cs

Can you revert any changes about formatting so that it's easy for me to focus on reviewing the real code changes.

Btw, which version of POI do you reference?

@9rnsr
Copy link
Contributor Author

9rnsr commented Jan 31, 2023

Thank you for your quick reply!

I ported real code changes from latest version POI 5.2.3.

Already the style changes has been collected into first one commit so you can see remain commits to review real changes.

(To be honest, those changes has been done by Visual Studio auto-formatting on save, so keeping styles unchanging is hard to do in my development environment. If it's really required in this project, I'll edit commits.)

@9rnsr
Copy link
Contributor Author

9rnsr commented Feb 2, 2023

@tonyqus OK, I completely removed style changes in each commits.

Now this PR contains real code changes only. Good?

@tonyqus
Copy link
Member

tonyqus commented Feb 2, 2023

LGTM

@tonyqus tonyqus merged commit 9a7edd7 into nissl-lab:master Feb 2, 2023
@9rnsr
Copy link
Contributor Author

9rnsr commented Feb 3, 2023

Thanks!

@9rnsr 9rnsr deleted the XWPFComment branch February 25, 2023 01:45
@tonyqus
Copy link
Member

tonyqus commented Jul 26, 2023

I tried to create a comment sample. But I cannot make it work.

XWPFParagraph paragraph = doc.CreateParagraph();
XWPFRun run = paragraph.CreateRun();
run.SetText("This is text");

var comments=doc.CreateComments();
var comment1=comments.CreateComment("0");
comment1.Author = "Tony";
comment1.Initials = "S";
comment1.Date = DateTime.Now.ToShortDateString();

var comment2=comments.CreateComment("1");
comment1.Author = "May";
var para=comment2.CreateParagraph();
var run2 = para.CreateRun();
run2.SetText("Hello World");


using (FileStream sw = File.Create("comments.docx"))
{
    doc.Write(sw);
}

I cannot see the comments in Word even if I turn "Show comments" on

image

Did I miss something?

@9rnsr
Copy link
Contributor Author

9rnsr commented Jul 26, 2023

It's necessary to handle underlying CT_x objects to emit comments to docx.

Reference: https://stackoverflow.com/questions/63935140/adding-comment-to-a-specific-word-or-run-in-docx-document-using-apache-poi

using (XWPFDocument doc = new XWPFDocument())
{
    var comments = doc.CreateComments();

    var comment1 = comments.CreateComment("0");
    comment1.Author = "Tony";
    comment1.Initials = "S";
    comment1.Date = DateTime.Now.ToShortDateString();

    var comment2 = comments.CreateComment("1");
    comment2.Author = "May";
    var para = comment2.CreateParagraph();
    para.CreateRun().SetText("Hello World");

    var paragraph1 = doc.CreateParagraph();
    paragraph1.CreateRun().SetText("This is ");
    paragraph1.GetCTP().AddNewCommentRangeStart().id = comment1.Id;
    paragraph1.CreateRun().SetText("1st comment");
    paragraph1.GetCTP().AddNewCommentRangeEnd().id = comment1.Id;
    paragraph1.CreateRun().SetText(" text.");
    paragraph1.GetCTP().AddNewR().AddNewCommentReference().id = comment1.Id;

    var paragraph2 = doc.CreateParagraph();
    paragraph2.CreateRun().SetText("This is ");
    paragraph2.GetCTP().AddNewCommentRangeStart().id = comment2.Id;
    paragraph2.CreateRun().SetText("2nd comment");
    paragraph2.GetCTP().AddNewCommentRangeEnd().id = comment2.Id;
    paragraph2.CreateRun().SetText(" text.");
    paragraph2.GetCTP().AddNewR().AddNewCommentReference().id = comment2.Id;

    using (FileStream sw = File.Create("comments.docx"))
    {
        doc.Write(sw);
    }
}

image

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

Successfully merging this pull request may close these issues.

2 participants