Aspose Words - Group

Hi

I have a word document with numerous content controls which I am wanting to lock down by selecting them and then on the developer tab I select the group option which stops anyone changing text within the document i.e. they are only able to amend values in the content controls.
Is the group feature available in aspose and if so can you provide me with a code example of its use?

thanks

Hi Jane,


Thanks for your inquiry. We will provide an ability to group or ungroup a selected range of text in SDT in future. I have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-10182. Your request has also been linked to this issue and you will be notified via this thread as soon as it is supported. Sorry for the inconvenience.

Best regards,

Hi Jane,


Thanks for being patient. It is to inform you that Group SDT controls are already supported by Aspose.Words. You just need to place Group controls at the correct place inside the document tee and then all the child controls can be happily put inside to be grouped. Please try executing the following code. I hope, this helps.
Document doc = new Document();

StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
sdt.removeAllChildren();
sdt.isShowingPlaceholderText(false);

Paragraph para = new Paragraph(doc);
Run before = new Run(doc, "This is simple text in SDT. ");
para.appendChild(before);

StructuredDocumentTag groupSdt = new StructuredDocumentTag(doc, SdtType.GROUP, MarkupLevel.INLINE);
groupSdt.removeAllChildren();
Run groupedTextRange = new Run(doc, "This text is grouped and can’t be edited. ");
groupSdt.appendChild(groupedTextRange);
para.appendChild(groupSdt);

Run after = new Run(doc, “This is simple text again.”);
para.appendChild(after);

sdt.appendChild(para);
doc.getFirstSection().getBody().appendChild(sdt);

doc.save(getMyDir() + “out.docx”);

If we can help you with anything else, please feel free to ask.

Best regards,

Thanks for that information, I tried the code out and it was very useful.
What I am trying to do is add protection to a form. Within word I can select all, then choose the group option and
all the document is locked for updates apart from the structured document tags.
I was trying to do this programatically, along the lines of the code below, but the layout doesn’t seem to be
quite correct as I appear to be losing paragraphs.

SectionCollection sections = document.getSections();
Section section = sections.get(0);
Body body = section.getBody();
StructuredDocumentTag groupSdt = new StructuredDocumentTag(document, SdtType.GROUP, MarkupLevel.BLOCK);
groupSdt.removeAllChildren();
NodeCollection nc = body.getChildNodes();
for(Node n:nc)
{
groupSdt.appendChild(n);
}
body.removeAllChildren();
body.appendChild(groupSdt);
document.save(file.getAbsolutePath());

Are you able to provide me with a way of selecting all and grouping?

thanks

Hi Jane,


Thanks for your feedback. Could you please attach your input Word document and expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word. We will provide you code to achieve the same using Aspose.Words.

Best regards,

Thanks for your help. I’ve attached 2 documents the input one and expected one. The expected one was created by taking the input document, selecting all and then grouping it, all within Word.

Hi Jane,


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

Best regards,

Hi Jane,


Thanks for being patient. To achieve this, you can insert a Block level Group SDT in Document Body and then move all other nodes inside this Group SDT. Please see the following code:

Document doc = new Document(getMyDir() + “InputDocument.docx”);

StructuredDocumentTag groupSdt = new StructuredDocumentTag(doc, SdtType.GROUP, MarkupLevel.BLOCK);
groupSdt.removeAllChildren();

CompositeNode node = (CompositeNode)doc.getFirstSection().getBody().getChildNodes().get(0);
node.getParentNode().insertBefore(groupSdt, node);

int count = doc.getFirstSection().getBody().getChildNodes().getCount();
for (int i = 1; i < count; i++)
{
groupSdt.appendChild(doc.getFirstSection().getBody().getChildNodes().get(1));
}

doc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,

I tried your suggestion and it works thanks, but there does seem to be a slight problem with it.
If I group the document using this code, then later, within word, if I select all and choose ungroup the document becomes corrupted.

Hi Jane,


Thanks for your inquiry. I tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10324. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Jane,


Thanks for being patient. After further investigation, this turned out to be not a bug in Aspose.Words. Please try executing the following code:

Document doc = new Document(getMyDir() + “InputDocument.docx”);

StructuredDocumentTag groupSdt = new StructuredDocumentTag(doc, SdtType.GROUP, MarkupLevel.BLOCK);
groupSdt.isShowingPlaceholderText(false);
groupSdt.removeAllChildren();

CompositeNode node = (CompositeNode)doc.getFirstSection().getBody().getChildNodes().get(0);
node.getParentNode().insertBefore(groupSdt, node);

int count = doc.getFirstSection().getBody().getChildNodes().getCount();
for (int i = 1; i < count; i++)
{
groupSdt.appendChild(doc.getFirstSection().getBody().getChildNodes().get(1));
}

doc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,