Search in sources :

Example 41 with XWPFDocument

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();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) ZipFile(java.util.zip.ZipFile) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 42 with XWPFDocument

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 43 with XWPFDocument

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();
}
Also used : XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Example 44 with XWPFDocument

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();
}
Also used : XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Example 45 with XWPFDocument

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();
}
Also used : XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Aggregations

XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)51 Test (org.junit.Test)15 FileOutputStream (java.io.FileOutputStream)11 File (java.io.File)9 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)9 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)9 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 FileInputStream (java.io.FileInputStream)4 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)3 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)3 XMLSlideShow (org.apache.poi.xslf.usermodel.XMLSlideShow)3 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)3 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)3 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ZipFile (java.util.zip.ZipFile)2 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2