Page 1 of 1

Adding comments

PostPosted: Thu Feb 17, 2011 8:49 am
by fracai
I'm having a bit of trouble figuring out the requirements for adding comments and references to those in the document body.

I've looked through a previous thread and the example for adding Hyperlinks and have made some progress in adding the comments, but I'm failing at connecting them to the text within the document.

Is there a sample, similar to HyperlinkTest.java or CreateWordprocessingMLDocument.java, that covers Comments?

Also, from looking at the Hyperlink example, which I assume will be quite similar to Comments, it seems like the only way to add a link is by using XmlUtils.unmarshalString. Is that correct? Is it the same for Comments? Or is there an alternate way to add the links and comments using a syntax similar to "addParagraphOfText", or the more verbose example in CreateWordprocessingMLDocument.java of adding bold text?

Is it possible, for example, to create a link or comment in the middle of a paragraph by combining multiple paragraphs? Something along the lines of:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
org.docx4j.wml.P p1 = generateParagraph("This is a ");
org.docx4j.wml.P.Hyperlink link = generateLink("hyperlink", "http://example.com");
org.docx4j.wml.P p2 = generateParagraph(" within a paragraph.");

org.docx4j.wml.P paragraph = p1.append(link).append(p2);

org.docx4j.wml.Body documentBody = factory.createBody();

documentBody.getEGBlockLevelElts().add(paragraph);
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: Adding comments

PostPosted: Fri Feb 18, 2011 12:32 am
by jason
I've created a simple CommentsSample to help you get started, at http://dev.plutext.org/trac/docx4j/brow ... ample.java

As you'll see from the sample, you can do everything with the object factory (as opposed to unmashallString).

As only w:commentReference is required; the sample doesn't currently add w:commentRangeStart or w:commentRangeEnd, eg:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:p>
            <w:commentRangeStart w:id="0"/>
            <w:r>
              <w:t>hello</w:t>
            </w:r>
            <w:commentRangeEnd w:id="0"/>
            <w:r>
              <w:rPr>
                <w:rStyle w:val="CommentReference"/>
              </w:rPr>
              <w:commentReference w:id="0"/>
            </w:r>
          </w:p>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


A suitable API might have a method to which you pass 2 paragraphs; the w:commentRangeStart is added at the start of the first one; and w:commentRangeEnd at the end of the second one.

The syntax you suggest reminds me of the fluent API for JAXB, but we don't use that.

Re: Adding comments

PostPosted: Fri Feb 18, 2011 7:47 am
by fracai
Thanks very much.

The sample, along with some trial and error has gotten me exactly what I needed.

I also figured out the syntax for creating a paragraph from multiple text runs, and for adding commentRangeStart and commentRangeEnd. It's actually somewhat intuitive once I got going.

Thanks again for your help, once I get this cleaned up a bit more I'll post my solution.

Re: Adding comments

PostPosted: Fri Feb 18, 2011 7:47 am
by fracai
Thanks very much.

The sample, along with some trial and error has gotten me exactly what I needed.

I also figured out the syntax for creating a paragraph from multiple text runs, and for adding commentRangeStart and commentRangeEnd. It's actually somewhat intuitive once I got going.

Thanks again for your help, once I get this cleaned up a bit more I'll post my solution.