For some important documents, we guarantee that the contents of the document will not be leaked, the files must always be encrypted. When viewing a file, you must enter the password correctly before opening the file. In this paper, we will show you how to use Spire.Doc for Java to set password protection for Word documents and remove passwords.
[Example 1] Word password protection setting
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class EncryptWord {
public static void main(String[] args) {
//Read a Word document
Document document = new Document();
document.loadFromFile("sample.docx");
//Protect your documents with a password
document.encrypt("abc-123");
//Save document
document.saveToFile("Encrypt.docx", FileFormat.Docx);
}
}
File encryption result:
[Example 2] Unprotect Word password
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class DecryptWord {
public static void main(String[] args) {
//Read the file with the password and enter the original password to unlock it
Document document = new Document();
document.loadFromFile("Encrypt.docx", FileFormat.Docx, "abc-123");
//Save document
document.saveToFile("Decrypt.docx", FileFormat.Docx);
}
}
When you run the program, the created file will no longer be password protected.
Recommended Posts