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();
}
}
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();
}
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);
}
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"));
}
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();
}
Aggregations