Anyway to block users from removing section breaks or adding section breaks in a document

Hi,


We have a requirement where in some parts of the document should be editable and some parts should be non-editable. Also, data in these parts will flow from a different systems. In this process we ended up creating section breaks to differentiate between an Editable Section and a Non-Editable section. And we are grouping all the elements within these sections using the following code snippet :

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

Now the real problem is that theoretically document for the user is locked but the document is actually open, user can still add a section break in the editable region or remove the section breaks from any part in the document.

Could you please let us know if there is a generic way to block users from adding or removing a section break from the document.

Thank you so much for the help.

Hi Rajesh,


Thanks for your inquiry. Are you able to achieve this using MS Word application? If yes, then please elaborate the steps you used to meet this requirement using MS Word. We will then provide you code to achieve the same using Aspose.Words. Thanks for your cooperation.

Best regards,

Hi Awais,


Please find attached a sample document.

All I did was grouped the elements together and clicked on properties, checked “Content Control Cannot be deleted”.

Regards,
Rajesh.

Hi Rajesh,


Thanks for your inquiry. Group SDT controls are already supported by Aspose.Words. You just need to place Group controls at the correct place inside the document tree and then all the child controls/content can be happily put inside to be grouped. Please try executing the following code and see attached document. 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() + “awjava-16.3.0.doc”);

Best regards,

Hi,


Grouping Section Breaks did not work as the user was still able to add section breaks to the document and it breaks our entire logic.

Is there any better way to handle our requirement , let me repost the requirement:

Our requirement is that inside a document, we should have:
1. Completely Editable Regions, where user can enter anything.
2. Non Editable Regions - Where data flows from a database and user cannot edit it.
3. Non Editable Regions with Rich Text editors where user enters data only in the Rich Text Editor.

To achieve this we have used section breaks to identify different sections. I was trying to understand if there is a better way to handle this instead of using section breaks. As, when we open up a particular section or a rich text editor then the user can enter section breaks in there which is breaking the entire logic and any changes in the database will not be reflected in the document.

Could you please let us know the best way to handle such scenarios…

Thank you again.

Regards,
Rajesh.
Hi Rajesh,

Thanks for your inquiry. I think, you may be able to achieve this by protecting whole document and then allow some areas in document to be editable. Please see following example:
Document doc = new Document();
DocumentBuilder builder =
new DocumentBuilder(doc);

builder.writeln(
"Some text is added.");
builder.writeln(
"Some text is added.");
builder.writeln(
"Some text is added.");

builder.startEditableRange();
builder.write(
"Some text is added.");

Comment comment =
new Comment(doc, "Awais Hafeez", "AH", new Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(
new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(
new Run(doc, "Comment text."));
builder.endEditableRange();

doc.protect(ProtectionType.
READ_ONLY);

doc.save(
getMyDir() + "awjava-16.3.0.docx");
Hope, this helps.

Best regards,

Hi,


We tried the editable regions, but the document shows a highlighted text region surrounded by two square brackets. Is there a way to avoid highlighting or can we style the highlighted text region with a different style instead of a yellow highlight ??.. I know it is a word feature, but just checking to see if there is a way to control the highlighting using Aspose ??

Regards,
Rajesh.

Hi Rajesh,


Thanks for your inquiry. This behavior seems specific to MS Word application. Can you meet this requirement using MS Word 2016? If yes, please list steps. We may be able to achieve this using Aspose.Words.

Best regards,

Hi Awais,


Thanks for your response, I wasn’t able to achieve the requirement in word.

I have another question, is it possible to protect the entire document and allow the user to comment only in certain sections, instead of ALLOW_ONLY_COMMENTS which allows the user to allow comments everywhere in the document, instead I want the user to be able to enter comments only in certain sections(Have provided an option “Enter Optional Comments” in the attached documents).

We tried the READ_ONLY + EDITABLE_REGION option but by doing this, user was able to get hold of the comments region which eventually was opened up for editing and discoverable by user when he clicks on “Find Next Region I can edit”. Attached is a sample document.

We also tried the ALLOW_ONLY_COMMENTS options where the user was able to enter comments every where else in the document. We want the user to enter comments only at the places where we provide a placeholder.

Your help is much appreciated…

Thank you.

Regards,
Rajesh.




Hi Rajesh,


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

Best regards,

Hi Awais,


Thank you for looking into this issue, I was checking to see if you were able to find out a solution ?

Regards,
Saketh Mudiganti.

Hi Saketh,


Thanks for being patient. Please try using the following code:

Document doc = new Document();
doc.removeAllChildren();

Section section =
new Section(doc);
doc.appendChild(section);

Body body =
new Body(doc);
section.appendChild(body);

Paragraph para =
new Paragraph(doc);
body.appendChild(para);

Run run =
new Run(doc);
run.setText(
"Hello ");
run.getFont().setColor(Color.
RED);
para.appendChild(run);

Run run2 =
new Run(doc);
run2.setText(
“World!”);
para.appendChild(run2);

doc.protect(ProtectionType.
READ_ONLY);

DocumentBuilder builder =
new DocumentBuilder(doc);

EditableRangeStart start = builder.startEditableRange();
EditableRangeEnd end = builder.endEditableRange();

run2.getParentNode().insertBefore(start, run2);
run2.getParentNode().insertAfter(end, run2);

doc.save(getMyDir() + “awjava-16.4.0.docx”);

In this case, you’ll be able to add comment to the word “World!”. Hope, this helps.

Best regards,

Hi Awais,


Thank you. I was able to achieve the requirement using the following lines of code:

for (int sectionCnt=0;sectionCnt<doc.getSections().getCount();sectionCnt++){
Section section = doc.getSections().get(sectionCnt);
if(section is an editable section){
builder.moveToSection(sectionCnt);
builder.startEditableRange();
builder.moveTo(section.getBody().getLastParagraph());
builder.endEditableRange();
continue; // Not grouping the elements in a section if section is editable
}
if(section is a comments section){
createBlankComment(section);
continue;
}
//If the section is not a comments section or editable section then group and block the elements of the section.
StructuredDocumentTag groupSdt = new StructuredDocumentTag(doc, SdtType.GROUP, MarkupLevel.BLOCK);
groupSdt.isShowingPlaceholderText(false);
groupSdt.removeAllChildren();
groupSdt.setLockContentControl(true);
CompositeNode node =(CompositeNode)doc.getSections().get(sectionCnt).getBody().getChildNodes().get(0);
node.getParentNode().insertBefore(groupSdt, node);
for (int k = 1; k < doc.getSections().get(sectionCnt).getBody().getChildNodes().getCount(); k++)
{
groupSdt.appendChild(doc.getSections().get(sectionCnt).getBody().getChildNodes().get(pointer));
}
}
doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS);

Regards,
Rajesh.



I have another issue with this approach, when we set the protection type of a document to "ALLOW_ONLY_COMMENTS" or "READ_ONLY" the user is not allowed to Toggle Track Changes. Through java code we can toggle the track changes but one of the requirement is to allow the user to Turn on or turn off track changes whenever needed in the document.

Could you please help me find a solution to this problem. Is it possible to set the document to "READ_ONLY" and allow the users to toggle track changes on and off ??

Attached is a screenshot that explains what I am talking about.

Hi Rajesh,


Thanks for your inquiry. This behavior is specific to MS Word application and I am afraid, there is no way in Aspose.Words. to enable/disable “Track Changes” button in “Review” tab of MS Word while the document is protected.

Best regards,