How do I open a password-protected Word document?

4

I can usually open a .docx file in Word using desktop.open fault of java, but there is a case where I need to open the document that is password protected and Word opens asking for the same. Is there any way I can enter the password still in the program and when opening the document it will open without asking?

Part of the code I'm using:

public void actionPerformed(ActionEvent e) {
    try {
         Desktop desktop = Desktop.getDesktop();
         File file = new File("" + caminhofonte1global.getSelectedFile());  
         desktop.open(file);                    
    }
    catch (IOException ioe) {
         ioe.printStackTrace();
    }           
}
    
asked by anonymous 06.01.2016 / 22:38

1 answer

3

I believe using only < strong> open is not possible because you need to check the password and the method only launches the associated application to open the file type. However there is POI which is an Apache API for manipulating Microsoft documents.

public static void main(String...args) throws
        FileNotFoundException, IOException, GeneralSecurityException {

    final Path ARQUIVO = Paths.get("C:\word.docx");
    final String SENHA = "foo";

    FileInputStream fis = new FileInputStream(ARQUIVO.toFile());
    POIFSFileSystem pfs = new POIFSFileSystem(fis);

    Decryptor decryptor = Decryptor.getInstance(new EncryptionInfo(pfs));

    if (decryptor.verifyPassword(SENHA)){
        // Abre o arquivo se a senha estiver OK.
    }
}
    
08.01.2016 / 16:57