I am trying to combine two word documents with Aspose.Words for Java. One of those documents should be protected, so that it isn’t possible for a user to edit it while the other document should still be editable. When I protect one of the documents using firstDoc.protect(ProtectionType.READ_ONLY, “password”); and combine the two via firstDoc.appendDocument(secondDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); however, the resulting document is protected in its entirety. (Andi f I switch the order of the documents, i.e. use the not protected document first, the resulting document isn’t protected at all).
So my question is: Is it possible to combine two documents and only protect one of them in the resulting document?
Best regards
Thanks for your inquiry. Please use the following code example to achieve your requirements. This code example protects the combined document and selectively turn protection off the contents of second document. Hope this helps you. Please let us know if you have any more queries.
Document doc1 = new Document(MyDir + "Doc1.docx");
Document doc2 = new Document(MyDir + "Doc2.docx");
DocumentBuilder builder = new DocumentBuilder(doc2);
builder.startBookmark("SecondDocument");
builder.endBookmark("SecondDocument");
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc1.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS);
DocumentBuilder builderDoc1 = new DocumentBuilder(doc1);
builderDoc1.moveToBookmark("SecondDocument");
Section currentSection = builderDoc1.getCurrentSection();
while (!currentSection.equals(doc1.getLastSection()))
{
System.out.println(currentSection.getText());
currentSection.setProtectedForForms(false);
currentSection = (Section)currentSection.getNextSibling();
}
currentSection.setProtectedForForms(false);
doc1.save(MyDir + "Out.docx");
This solution works perfectly, thank you very much for the quick reply!
I have one more question concerning the protection of documents: When protecting a document with Aspose and setting a password, this password is ignored when opening the document in Microsoft Word for Mac 2011, i.e. I can simply unprotect the document by clicking on Preferences->Security->“Unprotect Document” without beeing asked for the given pasword. When opening the document in Microsoft Word for Windows this problem doesn’t occur, i.e. the password is required to unprotect the file.
When I set the password in Microsoft Word for Mac directly, save the file and reopen it, the password is required to unprotect the document.
I have discovered that this problem only occures when the Documents are created in Mac Word. When creating them in Word for Windows the password works correctly, even if opened in Mac Word.
Is this a problem of Aspose? And if so, is there any way to solve it?
Thanks for your inquiry. Please use the Document.protect Method (ProtectionType, String) to protect the document from changes and optionally sets a protection password.
In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.4.0) from here and let us know how it goes on your side. Hope this helps you.
Document doc1 = new Document(MyDir + "Doc1.docx");
Document doc2 = new Document(MyDir + "Doc2.docx");
DocumentBuilder builder = new DocumentBuilder(doc2);
builder.startBookmark("SecondDocument");
builder.endBookmark("SecondDocument");
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc1.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS, "password");
DocumentBuilder builderDoc1 = new DocumentBuilder(doc1);
builderDoc1.moveToBookmark("SecondDocument");
Section currentSection = builderDoc1.getCurrentSection();
while (!currentSection.equals(doc1.getLastSection()))
{
currentSection.setProtectedForForms(false);
currentSection = (Section)currentSection.getNextSibling();
}
currentSection.setProtectedForForms(false);
doc1.save(MyDir + "Out.docx");
I am sorry if I was too vague about this.
I have created the document in question in Word for Mac, uploaded it in
our Application, used Aspose Words to protect it (and append further documents to it).
I already used the method you suggested to protect the document, i.e. I set the password using
When downloading it and opening it again in Word for Mac, I can simply unprotect the document by clicking on “unprotect document”, without ever needing the password. When I open this document in Word for Windos, everything is alright, I can only unprotect the document using the given password.
When creating the original Document in Word
for Windows, there seems to be no problem concerning the password (i.e.
it is recognized correctly, no matter if it is opened in Word for Mac or
for Windows).
Thanks
for sharing the detail. We will prepare the required platform (MAC operating system and MS Word for mac) to
simulate the environment as of yours. As soon as everything is setup, we
will test the issue at our end and will post the results here for your
kind reference.
Thanks
for your patience. I have tested the scenario and have managed to reproduce the same issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10183. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.
I have one more question concerning the partial protection of combined document. I want one part of the combined document to be protected (which works fine) and the other part to be editable (which usually also works fine). But now I have included a text box in the editable part of the document, but unfortunately the text box is not editable.
My code looks just like you suggested, i.e. I protect the whole combined document using
and than use bookmarks to unprotect the sections I want to edit. But this sections don’t seem to include the text box.
Is there a way to make the text box also editable?
Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.
Please attach your input Word document.
Please
create a standalone/runnable simple application (for example a Console
Application Project) that demonstrates the code (Aspose.Words code) you used to generate
your output document
Please attach the output Word file that shows the undesired behavior.
Please
attach your target Word document showing the desired behavior.
As soon as you get these pieces of information to
us we’ll start our investigation into your issue.
I have tried to reproduce the desired result in Microsoft Word and was unsuccessful. After some research I came to the conclusion that the result I was trying to achieve is not posible in Microsoft Word. It is not possible to have a document that is partially editable and has editable text boxes. It seems that as soon as the document is protected the textboxes are automatically protected too, no matter which section I exclude from the protection.
So it seems to me that this is a problem with Microsoft Word and not Aspose. Sorry for not having tried this before, it didn’t occure to me that this problem could have its roots in Microsoft Word.
Nevertheless I found another possibility to produce the desired result. Instead of using a textbox I used a position frame. This element can also be used to place content at precise positions in the document and it is remains editable when protecting parts of the document.
Thank you for your quick response!
Thanks for your feedback. It is nice to hear from you that you have found the solution of your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.
Thanks for your patience.
Regarding WORDSNET-10183, could you please create the expected output document at Mac environment using MS Word for Mac and protect it and share it here for our reference? We need this document for further investigations. Thanks for your cooperation in advance.
Thanks for your patience. Could you please open the attached Word document “in aw.docx” at your side? If Word for Mac asks password as it expected then please save your document with OoxmlCompliance.Ecma376_2006. Please share your feedback here for our reference. Thanks for your cooperation.