Search in sources :

Example 46 with HWPFDocument

use of org.apache.poi.hwpf.HWPFDocument in project poi by apache.

the class TestWordExtractor method testExtractFromEmbeded.

/**
	 * Test that we can get data from two different embedded word documents
	 */
@Test
public void testExtractFromEmbeded() throws IOException {
    InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls");
    POIFSFileSystem fs = new POIFSFileSystem(is);
    is.close();
    DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B7");
    DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B2");
    // Should have WordDocument and 1Table
    assertNotNull(dirA.getEntry("1Table"));
    assertNotNull(dirA.getEntry("WordDocument"));
    assertNotNull(dirB.getEntry("1Table"));
    assertNotNull(dirB.getEntry("WordDocument"));
    // Check each in turn
    HWPFDocument docA = new HWPFDocument(dirA);
    WordExtractor extractorA = new WordExtractor(docA);
    assertNotNull(extractorA.getText());
    assertTrue(extractorA.getText().length() > 20);
    assertEqualsTrim("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractorA.getText());
    assertEquals("Sample Doc 1", extractorA.getSummaryInformation().getTitle());
    assertEquals("Sample Test", extractorA.getSummaryInformation().getSubject());
    HWPFDocument docB = new HWPFDocument(dirB);
    WordExtractor extractorB = new WordExtractor(docB);
    assertNotNull(extractorB.getText());
    assertTrue(extractorB.getText().length() > 20);
    assertEqualsTrim("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", extractorB.getText());
    assertEquals("Sample Doc 2", extractorB.getSummaryInformation().getTitle());
    assertEquals("Another Sample Test", extractorB.getSummaryInformation().getSubject());
    extractorA.close();
    docA.close();
    extractorB.close();
    docB.close();
    fs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Test(org.junit.Test)

Example 47 with HWPFDocument

use of org.apache.poi.hwpf.HWPFDocument in project poi by apache.

the class TestWordExtractor method testFootnote.

@Test
public void testFootnote() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
    WordExtractor extractor = new WordExtractor(doc);
    assertExtractedContains(extractor.getFootnoteText(), "TestFootnote");
    // msonfcArabic
    assertEquals(0x00, doc.getRange().getSection(0).getFootnoteNumberingFormat());
    // rncCont
    assertEquals(0x00, doc.getRange().getSection(0).getFootnoteRestartQualifier());
    assertEquals(0, doc.getRange().getSection(0).getFootnoteNumberingOffset());
    assertEquals(1, doc.getFootnotes().getNotesCount());
    extractor.close();
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 48 with HWPFDocument

use of org.apache.poi.hwpf.HWPFDocument in project poi by apache.

the class TestLists method testIndentedText.

public void testIndentedText() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    Range r = doc.getRange();
    assertEquals(40, r.numParagraphs());
    assertEquals("Finally we want some indents, to tell the difference\r", r.getParagraph(34).text());
    assertEquals("Indented once\r", r.getParagraph(35).text());
    assertEquals("Indented twice\r", r.getParagraph(36).text());
    assertEquals("Indented three times\r", r.getParagraph(37).text());
    assertEquals("The end!\r", r.getParagraph(38).text());
    assertEquals(9, r.getParagraph(34).getLvl());
    assertEquals(9, r.getParagraph(35).getLvl());
    assertEquals(9, r.getParagraph(36).getLvl());
    assertEquals(9, r.getParagraph(37).getLvl());
    assertEquals(9, r.getParagraph(38).getLvl());
    assertEquals(9, r.getParagraph(39).getLvl());
    assertEquals(0, r.getParagraph(34).getIlvl());
    assertEquals(0, r.getParagraph(35).getIlvl());
    assertEquals(0, r.getParagraph(36).getIlvl());
    assertEquals(0, r.getParagraph(37).getIlvl());
    assertEquals(0, r.getParagraph(38).getIlvl());
    assertEquals(0, r.getParagraph(39).getIlvl());
// TODO Test the indent
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 49 with HWPFDocument

use of org.apache.poi.hwpf.HWPFDocument in project poi by apache.

the class TestBugs method test51671.

/**
     * [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem
     * throws a NullPointerException
     */
@Test
public void test51671() throws Exception {
    InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("empty.doc");
    NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
    try {
        HWPFDocument hwpfDocument = new HWPFDocument(npoifsFileSystem.getRoot());
        hwpfDocument.write(new ByteArrayOutputStream());
        hwpfDocument.close();
    } finally {
        npoifsFileSystem.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 50 with HWPFDocument

use of org.apache.poi.hwpf.HWPFDocument in project poi by apache.

the class TestLists method testBasics.

public void testBasics() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    Range r = doc.getRange();
    assertEquals(40, r.numParagraphs());
    assertEquals("Heading Level 1\r", r.getParagraph(0).text());
    assertEquals("This document has different lists in it for testing\r", r.getParagraph(1).text());
    assertEquals("The end!\r", r.getParagraph(38).text());
    assertEquals("\r", r.getParagraph(39).text());
    assertEquals(0, r.getParagraph(0).getLvl());
    assertEquals(9, r.getParagraph(1).getLvl());
    assertEquals(9, r.getParagraph(38).getLvl());
    assertEquals(9, r.getParagraph(39).getLvl());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Aggregations

HWPFDocument (org.apache.poi.hwpf.HWPFDocument)126 Test (org.junit.Test)66 InputStream (java.io.InputStream)15 FileInputStream (java.io.FileInputStream)10 Range (org.apache.poi.hwpf.usermodel.Range)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)7 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)7 WordExtractor (org.apache.poi.hwpf.extractor.WordExtractor)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PicturesTable (org.apache.poi.hwpf.model.PicturesTable)6 Bookmark (org.apache.poi.hwpf.usermodel.Bookmark)6 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)6 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 Transformer (javax.xml.transform.Transformer)4 DOMSource (javax.xml.transform.dom.DOMSource)4 Picture (org.apache.poi.hwpf.usermodel.Picture)4 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)4 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)4