Page 1 of 1

Opening a doc file with a Password

PostPosted: Tue Aug 06, 2013 7:57 am
by Will Nye
I have been looking through the examples on github and can't find one that uses a password protected document (opening from not saving as). Did I miss it? If not could any examples would be great! Thanks :mrgreen:

Re: Opening a doc file with a Password

PostPosted: Tue Aug 06, 2013 9:35 am
by jason
Hi, If you mean a password protected docx (as opposed to legacy binary doc), then OpcPackage contains a method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
load(final java.io.File docxFile, String password)
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


WordprocessingMLPackage extends OpcPackage

Re: Opening a doc file with a Password

PostPosted: Tue Aug 06, 2013 2:17 pm
by Will Nye
Thanks for your help Jason, I tried as you had mentioned but for some reason I am getting an error

Here is a class that I wrote and for some reason it is having troubles with setting wordMLPackage = (WordprocessingMLPackage)OpcPackage.load(new java.io.File(inputfilepath), "password");

Code: Select all
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import java.io.File;


public class Testclass {

public void readFromdoc(){
//   JAXBContext context = org.docx4j.jaxb.Context.jc;
   
   String inputfilepath = System.getProperty("user.dir") + "\\" + "mytest.docx";
   System.out.println(inputfilepath);          


   // Load the docx
   WordprocessingMLPackage wordMLPackage=null;
   try {
      wordMLPackage = (WordprocessingMLPackage)OpcPackage.load(new java.io.File(inputfilepath), "password");
   } catch (Docx4JException e1) {
      System.out.println("Unable to call load method from OpcPackage");
      e1.printStackTrace();
      System.exit(0);
   }
   // Save it
   String outputfilepath = System.getProperty("user.dir") + "/OUT_OpenAndSaveRoundTripTest.docx";
   try {
      wordMLPackage.save(new File(outputfilepath));
   } catch (Docx4JException e) {
      System.out.println("Unable to output file");
      e.printStackTrace();
      System.exit(0);
   }
}
   
}


Here is what my console output looks like

my user directory\mytest.docx
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
at Testclass.readFromdoc(Testclass.java:20)
at Kapowmainn.main(Kapowmainn.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

I downloaded the log4j.jar file and imported it just to see if that would make a difference but it still isn't compiling. Any advice would be great, thanks again.

Re: Opening a doc file with a Password

PostPosted: Tue Aug 06, 2013 4:35 pm
by jason
So what happens when you add log4j to your classpath?

Re: Opening a doc file with a Password

PostPosted: Thu Aug 08, 2013 6:03 am
by Will Nye
Sorry for the delay in response. After importing apache.* I still am getting the following issue.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
at Testclass.readFromdoc(Testclass.java:19)
at Kapowmainn.main(Kapowmainn.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
at Testclass.readFromdoc(Testclass.java:19)
at Kapowmainn.main(Kapowmainn.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

Re: Opening a doc file with a Password

PostPosted: Thu Aug 08, 2013 7:36 am
by jason
You need to add the log4j jar to your classpath. Are you doing this in an IDE? In any case, Google how to do that :-)

Re: Opening a doc file with a Password

PostPosted: Thu Aug 08, 2013 8:20 am
by Will Nye
I downloaded the log4j jar file and added it to the class path. I was just using org.apache.* to be sure anything that needed to be imported was. I did add it to the build path. Also, I'm using eclipse, would that make any difference?

Re: Opening a doc file with a Password

PostPosted: Thu Aug 08, 2013 1:00 pm
by jason
Eclipse is fine, but log4j is not on the classpath for the relevant "run configuration" if you are getting that error. You need to add it.

Re: Opening a doc file with a Password

PostPosted: Mon Aug 12, 2013 2:55 pm
by Will Nye
Jason, sorry for asking but do you know what could be causing the log4j:WARN No appenders could be found for logger (org.docx4j.openpackaging.io.LoadFromZipNG) error?

http://stackoverflow.com/questions/1813 ... erly-issue

The password for the file is t3mpw0rdh0undpw0rd (my docx file I'm attempting to read from)

Thanks again!

Re: Opening a doc file with a Password

PostPosted: Mon Aug 12, 2013 7:11 pm
by jason
You've got log4j on your classpath, which is good; you now need log4j.properties or log4j.xml on your classpath.

In Eclipse, in your run configuration, you can add the folder containing that file to your build path. (Classpath tab, click User Entries, then click advanced)

While you are there, on the arguments tab, under VM arguments, make sure you have

-Dlog4j.configuration=log4j.xml

For an actual log4j config file, see https://github.com/plutext/docx4j/blob/ ... /log4j.xml

Finally, not sure whether you've seen http://stackoverflow.com/questions/1813 ... erly-issue (post by a different username, but same issue, and same password), but that user was trying to open an OpenOffice/LibreOffice file, not an OpenXML file...

Re: Opening a doc file with a Password

PostPosted: Wed Aug 14, 2013 3:45 pm
by Will Nye
Thanks, that was the issue. I downloaded MS office and saved it in word. For some reason there was an issue using open office when saving as a docx file. However, I am still unable to read from a password protected document following the same format you had suggested load(final java.io.File docxFile, String password).

Here is the code snippet and what the console displays

String inputfilepath = System.getProperty("user.dir") + "\\" + "test.docx";


// Load the docx
WordprocessingMLPackage wordMLPackage=null;
try {
wordMLPackage = (WordprocessingMLPackage)OpcPackage.load(new java.io.File(inputfilepath), "t3mpw0rdh0undpw0rd");
} catch (Docx4JException e1) {
System.out.println("Unable to call load method from OpcPackage");
e1.printStackTrace();
System.exit(0);
}


log4j:WARN No appenders could be found for logger (org.docx4j.openpackaging.packages.OpcPackage).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/POIFSFileSystem
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:304)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:249)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:195)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:178)
at Testclass.readFromdoc(Testclass.java:23)
at Kapowmainn.main(Kapowmainn.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.poifs.filesystem.POIFSFileSystem
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more

Re: Opening a doc file with a Password

PostPosted: Wed Aug 14, 2013 4:54 pm
by jason
You didn't attach your docx, but in any case, your problem is that you are missing another dependency!

docx4j won't work if dependencies are missing from your classpath

If you are using 2.8.1, you should include all the files in http://www.docx4java.org/docx4j/docx4j-2.8.1/

Your immediate problem here is that you are missing poi*.jar

Re: Opening a doc file with a Password

PostPosted: Fri Aug 16, 2013 2:16 pm
by Will Nye
Thanks so much for your patience, I was able to read from an encrypted docx file.

If anyone is curious how I did it for future reference.

I downloaded apache's common/codec jar and like Jason said the poi.jar file

Added them to the build path. My class to read from a password protected docx file looks like this:

import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;




public class Testclass {

public void readFromdoc(){

String inputfilepath = System.getProperty("user.dir") + "\\" + "yourDocument.docx";


// Load the docx
WordprocessingMLPackage wordMLPackage=null;
try {
wordMLPackage = (WordprocessingMLPackage)OpcPackage.load(new java.io.File(inputfilepath), "yourPassword");
} catch (Docx4JException e1) {
System.out.println("Unable to call load method from OpcPackage");
e1.printStackTrace();
System.exit(0);
}
MainDocumentPart main = wordMLPackage.getMainDocumentPart();
try {
main.addAltChunk(AltChunkType.WordprocessingML, new FileInputStream(inputfilepath) );
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
System.exit(0);
} catch (Docx4JException e) {
System.out.println("Docx4JException");
e.printStackTrace();
System.exit(0);
}
List<Object> content = main.getContent();
System.out.println(content.get(0));
}

}