use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestXWPFBugs method bug53475_aes256.
/**
* A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
* but the key can be 128/192/256 bits
*/
@Test
public void bug53475_aes256() throws Exception {
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
// Check the encryption details
EncryptionInfo info = new EncryptionInfo(filesystem);
assertEquals(16, info.getHeader().getBlockSize());
assertEquals(256, info.getHeader().getKeySize());
assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
// Check it can be decoded
Decryptor d = Decryptor.getInstance(info);
assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
// Check we can read the word document in that
InputStream dataStream = d.getDataStream(filesystem);
OPCPackage opc = OPCPackage.open(dataStream);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.getText();
assertNotNull(text);
// I know ... a stupid typo, maybe next time ...
assertEquals("The is a password protected document.", text.trim());
ex.close();
filesystem.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class XWPFTestDataSamples method writeOutAndReadBack.
public static XWPFDocument writeOutAndReadBack(XWPFDocument doc) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
doc.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return new XWPFDocument(bais);
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestXWPFWordExtractor method testFldSimpleContent.
/**
* The output should contain the values of simple fields, those specified
* with the fldSimple element (spec sec. 17.16.19)
*
* @throws IOException
*/
public void testFldSimpleContent() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FldSimple.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
String text = extractor.getText();
assertTrue(text.length() > 0);
assertContains(text, "FldSimple.docx");
extractor.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestXWPFWordExtractor method testParagraphHeader.
public void testParagraphHeader() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
assertContains(extractor.getText(), "Section 1");
assertContains(extractor.getText(), "Section 2");
assertContains(extractor.getText(), "Section 3");
extractor.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestXWPFWordExtractor method testGetWithHyperlinks.
public void testGetWithHyperlinks() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
// Now check contents
extractor.setFetchHyperlinks(false);
assertEquals("This is a test document.\nThis bit is in bold and italic\n" + "Back to normal\n" + "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" + "We have a hyperlink here, and another.\n", extractor.getText());
// One hyperlink is a real one, one is just to the top of page
extractor.setFetchHyperlinks(true);
assertEquals("This is a test document.\nThis bit is in bold and italic\n" + "Back to normal\n" + "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" + "We have a hyperlink <http://poi.apache.org/> here, and another.\n", extractor.getText());
extractor.close();
}
Aggregations