-4

how can we unprotect the word document using java apache poi? I have protected the document as read-only using password pro-grammatically.Now I want to unprotect it. How can we do ? Is there any method to unprotect the document. I have used removePasswordProtection() but that document is not editable even after using that method.

The sample code that I have used for protection is

XWPFDocument document=new XWPFDocument();
 document.enforceReadonlyProtection(strPassword,HashAlgorithm.sha1);

The document is getting protected successfully.

But when I am unprotecting document using the below code snippet it is not working.

 if(document.isEnforcedReadonlyProtection())
     {
     if(document.validateProtectionPassword(strPassword))
     {
        document.removeProtectionEnforcement();
     }
     }

Can anyone help me what method that I can use to unprotect the document?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Harshini
  • 11
  • 1

2 Answers2

1

Cannot reproducing.

Following code produces two Word documents. One, WordProtected.docx, which is protected and one, WordUnprotected.docx in which protection is removed.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.poifs.crypt.HashAlgorithm;

class XWPFReadOnlyProtection {

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

  XWPFDocument document = new XWPFDocument();

  String strPassword = "password";

  document.enforceReadonlyProtection(strPassword, HashAlgorithm.sha1);

  FileOutputStream fileout = new FileOutputStream("WordProtected.docx");
  document.write(fileout);
  fileout.close();
  document.close();

  document = new XWPFDocument(new FileInputStream("WordProtected.docx"));

  document.removeProtectionEnforcement();

  fileout = new FileOutputStream("WordUnprotected.docx");
  document.write(fileout);
  fileout.close();
  document.close();

 }
}
Axel Richter
  • 56,077
  • 6
  • 60
  • 87
0

use this code for Word to Protect

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class WordTest {

       public static void main(String[] args) throws IOException {
       FileInputStream in = new FileInputStream("D:\\govind.doc");    

       BufferedInputStream bin = new BufferedInputStream(in);            
       POIFSFileSystem poiFileSystem = new POIFSFileSystem(bin);

       Biff8EncryptionKey.setCurrentUserPassword("P@ssw0rd");
       HWPFDocument doc = new HWPFDocument(poiFileSystem);            
       Range range = doc.getRange();

       FileOutputStream out = new FileOutputStream("D:\\govind.doc");
       doc.write(out);
       out.close();
    }
}

this is use for protected word File unportected

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class wordFileTest {

    public static void main(String[] args) throws IOException {
       geenrateUnprotectedFile("D:\\","govind","1234");
    }


     public static void geenrateUnprotectedFile(String filePath,String fileName,String pwdtxt) {
    try {
     FileInputStream in = new FileInputStream(filePath+fileName+".doc");

        BufferedInputStream bin = new BufferedInputStream(in);
        POIFSFileSystem poiFileSystem = new POIFSFileSystem(bin);

        Biff8EncryptionKey.setCurrentUserPassword(pwdtxt);
        HWPFDocument doc = new HWPFDocument(poiFileSystem);

        String docType=doc.getDocumentText();

         FileOutputStream out = new FileOutputStream(filePath+fileName+"12.doc");
        out.write(docType.getBytes());
        System.out.println("don");
    }catch (Exception e) {
      e.printStackTrace();
    } 
     }

}
Govind Sharma
  • 127
  • 1
  • 4
  • Can u please tell me how to un-protect the document? In the code snippet that you mentioned there is only code to protect the document. Please tell me the method that can to be used to unprotect the document. – Harshini Jun 12 '19 at 10:23