How to open a password protected Word document?

Asked

Viewed 537 times

5

I can normally open a file .docx in Word using the desktop.open java, but there is a case where I need to open the document that is password protected and Word opens asking for it. Is there any way I can still enter the password in the program and when opening the document it already opens 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();
    }           
}

1 answer

4


I believe using only the open is not possible because you need to check the password and the method just launches the associated application to open the file type. However there is the 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.
    }
}
  • I was already looking for and reading about this Apache but had not understood how the opening with password worked. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.