Protect Forms in DOCX Word Document using Java | Define Editable Ranges or Regions in Word Document

I am trying to figure out how to create a protected rich form in a DOCX file from Aspose Words 20.5 for java.

Right now my inherited code base creates a document with read only sections and normal sections for the form and the fields respectively. The problem we have ran into is our users are still able to add content at the end of the document.

The solution I was trying is to use the protection code that aspose provides to set the protections on the document. (This may not be the best solution to my problem above so I am open to other solutions).

I was able to set doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS) on my document and I was also able to call section.setProtectedForForms(false) on each of the rich text sections on my form that the user should be able to edit.

Problem I am running into is the generated documents are showing up as read only in word. I am unable to edit the contents of the unprotected sections at all.

I suspect I might have messed something up on my end. If there are any guides that i missed for working with Aspose Words I would appreciate them.

Thank you,
~Ryan

@RyanWilliamsUSC,

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

  • Your simplified input Word document
  • Aspose.Words for Java 20.5 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words for Java. Thanks for your cooperation.

@awais.hafeez

While putting together the word document you requested I discovered that the issue I was experiencing was in word and not aspose.

Our version of Microsoft Word opens documents (made in MS Word AND Aspose Words) that have been set to allow only form fields as read only for some reason.

I am looking at this on my end but still appreciate any input.

As far as sending you files goes I am unable to share artifacts (DOCX, PDFSs, and Source) from our system with outside parties for security reasons. I am able to request exceptions for specific files but that can take a while to get approved. I do apologize for this restriction.

I am pretty sure this is not a bug on your end since I still experience the exact same issue when I make the file entirely in MS Word.

Thanks,
~Ryan

@awais.hafeez

I figured out how to set up the file as I want it in word.

This is the procedure I used (See the “Newer Versions” section): Allow changes to parts of a protected document - Microsoft Support

I just need to know how to repeat this procedure in aspose words

Thanks,
~Ryan

@RyanWilliamsUSC,

I think, you can define different Editable Ranges (by using Aspose.Words) in your document to meet this requirement. The following sample Java code demonstrates how you can protect the whole document and then define a few Editable Range scope(s). You can incorporate the same functionality in your project. Hope, this helps.

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("E:\\temp\\awjava-20.5.docx");

@awais.hafeez

Thank you. I am going to try this out in our code base. I will keep you updated on this.

Thanks,
~Ryan

@awais.hafeez

So this code looks to work outside of my code base in demo/toy code.

The code base I inherited does not use Document Builder. Our code directly works with the nodes using Aspose to copy DOCX template files and make changes. We then merge the templates together to create a report. This is a bodge on our end.

We are “using this” because we used doc4j before we switched to Aspose and remapped our internal library code from using doc4j to using Aspose.

I did not see a way to directly add editable ranges to a document without using the document builder in aspose. Is there a way to do this that you know about using Aspose or do I have to migrate our code to use the document builder.

A better question to ask might be is it possible to add editable ranges in specific parts of a existing document with Aspose?

Thanks and my apologies.

@RyanWilliamsUSC,

Please see these sample input and output Word documents and try running the following code:

Java Code:

Document doc = new Document("E:\\Temp\\input.docx");
doc.protect(ProtectionType.READ_ONLY);

Paragraph firstPara = doc.getFirstSection().getBody().getFirstParagraph();
Paragraph lastPara = doc.getLastSection().getBody().getLastParagraph();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveTo(firstPara);
EditableRangeStart start1 = builder.startEditableRange();
builder.endEditableRange();
firstPara.insertBefore(start1, firstPara.getFirstChild());

builder.moveTo(lastPara);
EditableRangeStart start2 = builder.startEditableRange();
builder.endEditableRange();
lastPara.insertBefore(start2, lastPara.getFirstChild());

doc.save("E:\\Temp\\awjava-20.5.docx");

In the output document, you will notice only the first and the last paragraphs are editable. Hope, this helps in achieving what you are looking for.

@awais.hafeez

Thank you. I am going to try this out.