Search in sources :

Example 41 with HWPFDocument

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

the class TestHWPFWrite method testInvalidInPlaceWriteNPOIFS.

@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteNPOIFS() throws Exception {
    // Can't work for Read-Only files
    NPOIFSFileSystem fs = new NPOIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true);
    HWPFDocument doc = new HWPFDocument(fs.getRoot());
    try {
        doc.write();
    } finally {
        doc.close();
        fs.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Test(org.junit.Test)

Example 42 with HWPFDocument

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

the class TestHWPFWrite method testWriteStream.

/**
     * Write to a stream
     */
@Test
public void testWriteStream() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("SampleDoc.doc");
    Range r = doc.getRange();
    assertEquals("I am a test document\r", r.getParagraph(0).text());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.write(baos);
    doc.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    doc = new HWPFDocument(bais);
    r = doc.getRange();
    assertEquals("I am a test document\r", r.getParagraph(0).text());
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 43 with HWPFDocument

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

the class TestBugs method test51604.

/**
     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
     * release from download site )
     */
@Test
public void test51604() {
    HWPFDocument document = HWPFTestDataSamples.openSampleFile("Bug51604.doc");
    Range range = document.getRange();
    int numParagraph = range.numParagraphs();
    int counter = 0;
    for (int i = 0; i < numParagraph; i++) {
        Paragraph paragraph = range.getParagraph(i);
        int numCharRuns = paragraph.numCharacterRuns();
        for (int j = 0; j < numCharRuns; j++) {
            CharacterRun charRun = paragraph.getCharacterRun(j);
            String text = charRun.text();
            charRun.replaceText(text, "+" + (++counter));
        }
    }
    document = HWPFTestDataSamples.writeOutAndReadBack(document);
    String text = document.getDocumentText();
    assertEqualsIgnoreNewline("+1+2+3+4+5+6+7+8+9+10+11+12", text);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 44 with HWPFDocument

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

the class TestWordToTextConverter method testBug47731.

/**
     * [FAILING] Bug 47731 - Word Extractor considers text copied from some
     * website as an embedded object
     */
public void testBug47731() throws Exception {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47731.doc");
    String foundText = WordToTextConverter.getText(doc);
    assertTrue(foundText.contains("Soak the rice in water for three to four hours"));
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 45 with HWPFDocument

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

the class TestWordExtractor method testDifferentPOIFS.

/**
     * Tests that we can work with both {@link POIFSFileSystem}
     *  and {@link NPOIFSFileSystem}
     */
@Test
public void testDifferentPOIFS() throws Exception {
    // Open the two filesystems
    File file = docTests.getFile("test2.doc");
    InputStream is = new FileInputStream(file);
    OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
    is.close();
    NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
    DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
    // Open directly 
    for (DirectoryNode dir : files) {
        @SuppressWarnings("resource") WordExtractor extractor = new WordExtractor(dir);
        assertEqualsTrim(p_text1_block, extractor.getText());
    // extractor.close();
    }
    // Open via a HWPFDocument
    for (DirectoryNode dir : files) {
        HWPFDocument doc = new HWPFDocument(dir);
        WordExtractor extractor = new WordExtractor(doc);
        assertEqualsTrim(p_text1_block, extractor.getText());
        extractor.close();
    }
    npoifs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

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