0
votes

I want to open a password protected docx file using Apache POI. Can anyone help me with the complete code please? Am not getting solution with this code

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:126) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:113) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.(NPOIFSFileSystem.java:301) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:413) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:394)

  POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("D:/abc.docx"));
    EncryptionInfo info=new EncryptionInfo(fs);
    Decryptor decryptor=Decryptor.getInstance(info);
    if(!decryptor.verifyPassword("user"))
    {
        throw new RuntimeException("document is encrypted");
    }
    InputStream in=decryptor.getDataStream(fs);
    HSSFWorkbook wb=new HSSFWorkbook(in);
    File f=new File("D:/abc5.docx");
    wb.write(f);
2
please tell what you got and what not. by code level i should workAshok Kumar N
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:Amar Malik
Which line is giving you the error?Gagravarr
HSSFWorkbook wb=new HSSFWorkbook(in);Amar Malik
instead of this POIFSFileSystem fs; fs = new POIFSFileSystem(new FileInputStream(getFilePath())); use this new OPCPackage.open(new FileInputStream(getFilePath()))Ashok Kumar N

2 Answers

2
votes

The basic code for decryption the XML-based formats of Microsoft Office is shown in XML-based formats - Decryption.

But of course one must know that *.docx, which is a Word file in Office Open XML format, cannot be a HSSFWorkbook, which would be a Excel workbook in binary BIFF file format, but instead must be a XWPFDocument.

So:

import java.io.InputStream;
import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.Decryptor;

import java.security.GeneralSecurityException;

public class ReadEncryptedXWPF {

 static XWPFDocument decryptdocx(POIFSFileSystem filesystem, String password) throws Exception {

  EncryptionInfo info = new EncryptionInfo(filesystem);
  Decryptor d = Decryptor.getInstance(info);

  try {
   if (!d.verifyPassword(password)) {
        throw new RuntimeException("Unable to process: document is encrypted");
   }

   InputStream dataStream = d.getDataStream(filesystem);

   return new XWPFDocument(dataStream);

  } catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
  }
 }

 public static void main(String[] args) throws Exception {

  POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream("abc.docx"));
  XWPFDocument document = decryptdocx(filesystem, "user");

  XWPFWordExtractor extractor = new XWPFWordExtractor(document);
  System.out.println(extractor.getText());
  extractor.close();

 }
}
0
votes

I have solved this. The code is below

    POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("D:/abc.docx"));
    EncryptionInfo info=new EncryptionInfo(fs);
    Decryptor decryptor=Decryptor.getInstance(info);
    XWPFDocument document=null;
    if(decryptor.verifyPassword("password"))
    {
          InputStream dataStream = decryptor.getDataStream(fs); 
          document = new XWPFDocument(dataStream); 
    }else{
        throw new Exception("file is protected with password...please open with right password");
    }
    File f=new File("D:/abc.docx");
    FileOutputStream fos = new FileOutputStream(f);
    document.write(fos);
    document.close();