Set protraction type as readonly for content control

@tahir.manzoor
In my document each section is content control. And I am using protection type read only (contentDoc.protect(ProtectionType.READ_ONLY)) for current document.

Now I want text of a particular section to be editable.

for your Ref. PFA.
I want highlighted text should be editable.
But in my case whole document is non-editable.

image.png (15.1 KB)

Document contentDoc = new Document(Teste.class.getResourceAsStream(“originaldocx”));
contentDoc.protect(ProtectionType.READ_ONLY);
DocumentBuilder builder = new DocumentBuilder(contentDoc);
for(Section section: contentDoc.getSections()){
section.setProtectedForForms(false);
builder.moveToSection(contentDoc.indexOf(section));
builder.moveTo(section.getBody().getFirstChild());
EditableRangeStart s = builder.startEditableRange();
builder.moveTo(section.getBody().getLastChild());
builder.endEditableRange(s);

@rabinintig

Could you please ZIP and attach your input and expected output documents here for our reference? We will then provide you more information about your query along with code.

@tahir.manzoor
my input file is “input.docx”
and output file id “output.docx”
in my output.docx file i need only highlighted text will be for editable and other will be non-editable.
but above code doing full document non-editable.Input-Output.zip (36.2 KB)

@rabinintig

We have tested the scenario using the latest version of Aspose.Words for Java 19.11 and have not found the shared issue. So, please use Aspose.Words for Java 19.11.

Following code example shows how to protect the whole document except specific section. We have attached the output DOCX with this post for your kind reference. 19.11.zip (16.4 KB)

Document contentDoc = new Document(MyDir + "input.docx");
contentDoc.protect(ProtectionType.READ_ONLY);
DocumentBuilder builder = new DocumentBuilder(contentDoc);
builder.moveToSection(10);
Section section = builder.getCurrentSection();
builder.moveTo(section.getBody().getFirstChild());
EditableRangeStart s = builder.startEditableRange();
builder.moveTo(section.getBody().getLastChild());
builder.endEditableRange(s);
contentDoc.save(MyDir + "19.11.docx");

@tahir.manzoor
Thanks …
Above code is working fine.
But my scenario is little bit different . I am fetching the bookmarks of the document and checking for clauses and section,if found then i am storing name of the bookmarks in a list (bookmark List).
I have a particular bookmark,if it presents in list (bookmark List) then i used to make this particular section editable.

for your Ref. i am sharing my code.

List<String> list = new ArrayList<String>();
for (Bookmark b : document.getRange().getBookmarks())
{
    if (b.getName().startsWith(WordDocConstants.SECTION) || b.getName().startsWith(WordDocConstants.CLAUSE))
    {
        list.add(b.getName());
        /*System.out.println(b.getName());*/
    }
}
document.protect(ProtectionType.READ_ONLY, tmsComapnyId);
//NodeCollection<Section> docSectionsCollection = 
document.getChildNodes(NodeType.SECTION, true);
DocumentBuilder documentBuilder = new DocumentBuilder(document);
for (int i = 0; i < list.size(); i++)
{
    // bookmarkNames may contains multiple bookmark Names for which we have to make editable section
    if (bookmarkNames.contains(list.get(i)))  // ex. bookmarkname may be like "clause_2c9f80f76ef38992016ef38b29bd0008"
    {
        documentBuilder.moveToBookmark(list.get(i));
        Section section = documentBuilder.getCurrentSection();
        int sectionIndex = document.getSections().indexOf(section);
        //builder.moveToSection(10); 
        documentBuilder.moveToSection(sectionIndex);
        documentBuilder.moveTo(section.getBody().getFirstChild());
        EditableRangeStart s = documentBuilder.startEditableRange();
        documentBuilder.moveTo(section.getBody().getLastChild());
        documentBuilder.endEditableRange(s);
    }
}

Input-Output.zip (36.2 KB)

@rabinintig

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor
Now i am attaching working code as well as input and output files for your ref.
My requirement is only highlighted part of my output document should be editable.

Input file is : “Underwriting-Work Order-Latish.docx”
Output file : “Underwriting-Work Order-Latish_output.docx”
Code : "AsposePoc.java"input.zip (46.6 KB)

@rabinintig

In this case, we suggest you please get the start and end nodes of highlighted section of document, move the cursor to these nodes and insert start and end editable ranges. Please check the following code snippet.

Node start = (Node)list.get(0);
Node end = (Node)list.get(list.size() - 1);

document.protect(ProtectionType.READ_ONLY, "c8957185-b1e3-4bd6-8baa-9d33f8ecc295");
DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.moveTo(start);
EditableRangeStart s = documentBuilder.startEditableRange();
documentBuilder.moveTo(end);
documentBuilder.endEditableRange(s);

We suggest you please protect the document just before saving the document. All other document processing tasks should be done before it.

@tahir.manzoor

thanks a lot…
It’s working fine now.:grinning::grinning: