Search in sources :

Example 21 with HWPFDocument

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

the class TestProblems method testParagraphPAPXNoParent45877.

/**
     * Bug #45877 - problematic PAPX with no parent set
     */
@Test
public void testParagraphPAPXNoParent45877() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45877.doc");
    assertEquals(17, doc.getRange().numParagraphs());
    assertEquals("First paragraph\r", doc.getRange().getParagraph(0).text());
    assertEquals("After Crashing Part\r", doc.getRange().getParagraph(13).text());
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 22 with HWPFDocument

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

the class TestProblems method testTableIterator.

/**
     * Bug #48245 - don't include the text from the next cell in the current one
     */
@Test
public void testTableIterator() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("simple-table2.doc");
    Range r = doc.getRange();
    // Check the text is as we'd expect
    assertEquals(13, r.numParagraphs());
    assertEquals("Row 1/Cell 1", r.getParagraph(0).text());
    assertEquals("Row 1/Cell 2", r.getParagraph(1).text());
    assertEquals("Row 1/Cell 3", r.getParagraph(2).text());
    assertEquals("", r.getParagraph(3).text());
    assertEquals("Row 2/Cell 1", r.getParagraph(4).text());
    assertEquals("Row 2/Cell 2", r.getParagraph(5).text());
    assertEquals("Row 2/Cell 3", r.getParagraph(6).text());
    assertEquals("", r.getParagraph(7).text());
    assertEquals("Row 3/Cell 1", r.getParagraph(8).text());
    assertEquals("Row 3/Cell 2", r.getParagraph(9).text());
    assertEquals("Row 3/Cell 3", r.getParagraph(10).text());
    assertEquals("", r.getParagraph(11).text());
    assertEquals("\r", r.getParagraph(12).text());
    Paragraph p;
    // Take a look in detail at the first couple of
    // paragraphs
    p = r.getParagraph(0);
    assertEquals(1, p.numParagraphs());
    assertEquals(0, p.getStartOffset());
    assertEquals(13, p.getEndOffset());
    assertEquals(0, p._parStart);
    assertEquals(1, p._parEnd);
    p = r.getParagraph(1);
    assertEquals(1, p.numParagraphs());
    assertEquals(13, p.getStartOffset());
    assertEquals(26, p.getEndOffset());
    assertEquals(1, p._parStart);
    assertEquals(2, p._parEnd);
    p = r.getParagraph(2);
    assertEquals(1, p.numParagraphs());
    assertEquals(26, p.getStartOffset());
    assertEquals(39, p.getEndOffset());
    assertEquals(2, p._parStart);
    assertEquals(3, p._parEnd);
    // Now look at the table
    Table table = r.getTable(r.getParagraph(0));
    assertEquals(3, table.numRows());
    TableRow row;
    TableCell cell;
    row = table.getRow(0);
    assertEquals(0, row._parStart);
    assertEquals(4, row._parEnd);
    cell = row.getCell(0);
    assertEquals(1, cell.numParagraphs());
    assertEquals(0, cell._parStart);
    assertEquals(1, cell._parEnd);
    assertEquals(0, cell.getStartOffset());
    assertEquals(13, cell.getEndOffset());
    assertEquals("Row 1/Cell 1", cell.text());
    assertEquals("Row 1/Cell 1", cell.getParagraph(0).text());
    cell = row.getCell(1);
    assertEquals(1, cell.numParagraphs());
    assertEquals(1, cell._parStart);
    assertEquals(2, cell._parEnd);
    assertEquals(13, cell.getStartOffset());
    assertEquals(26, cell.getEndOffset());
    assertEquals("Row 1/Cell 2", cell.text());
    assertEquals("Row 1/Cell 2", cell.getParagraph(0).text());
    cell = row.getCell(2);
    assertEquals(1, cell.numParagraphs());
    assertEquals(2, cell._parStart);
    assertEquals(3, cell._parEnd);
    assertEquals(26, cell.getStartOffset());
    assertEquals(39, cell.getEndOffset());
    assertEquals("Row 1/Cell 3", cell.text());
    assertEquals("Row 1/Cell 3", cell.getParagraph(0).text());
    // Onto row #2
    row = table.getRow(1);
    assertEquals(4, row._parStart);
    assertEquals(8, row._parEnd);
    cell = row.getCell(0);
    assertEquals(1, cell.numParagraphs());
    assertEquals(4, cell._parStart);
    assertEquals(5, cell._parEnd);
    assertEquals(40, cell.getStartOffset());
    assertEquals(53, cell.getEndOffset());
    assertEquals("Row 2/Cell 1", cell.text());
    cell = row.getCell(1);
    assertEquals(1, cell.numParagraphs());
    assertEquals(5, cell._parStart);
    assertEquals(6, cell._parEnd);
    assertEquals(53, cell.getStartOffset());
    assertEquals(66, cell.getEndOffset());
    assertEquals("Row 2/Cell 2", cell.text());
    cell = row.getCell(2);
    assertEquals(1, cell.numParagraphs());
    assertEquals(6, cell._parStart);
    assertEquals(7, cell._parEnd);
    assertEquals(66, cell.getStartOffset());
    assertEquals(79, cell.getEndOffset());
    assertEquals("Row 2/Cell 3", cell.text());
    // Finally row 3
    row = table.getRow(2);
    assertEquals(8, row._parStart);
    assertEquals(12, row._parEnd);
    cell = row.getCell(0);
    assertEquals(1, cell.numParagraphs());
    assertEquals(8, cell._parStart);
    assertEquals(9, cell._parEnd);
    assertEquals(80, cell.getStartOffset());
    assertEquals(93, cell.getEndOffset());
    assertEquals("Row 3/Cell 1", cell.text());
    cell = row.getCell(1);
    assertEquals(1, cell.numParagraphs());
    assertEquals(9, cell._parStart);
    assertEquals(10, cell._parEnd);
    assertEquals(93, cell.getStartOffset());
    assertEquals(106, cell.getEndOffset());
    assertEquals("Row 3/Cell 2", cell.text());
    cell = row.getCell(2);
    assertEquals(1, cell.numParagraphs());
    assertEquals(10, cell._parStart);
    assertEquals(11, cell._parEnd);
    assertEquals(106, cell.getStartOffset());
    assertEquals(119, cell.getEndOffset());
    assertEquals("Row 3/Cell 3", cell.text());
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 23 with HWPFDocument

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

the class TestProblems method testProblemHeaderStories49936.

/**
     * Bug #49936 - Problems with reading the header out of the Header Stories
     */
@SuppressWarnings("deprecation")
@Test
public void testProblemHeaderStories49936() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("HeaderFooterProblematic.doc");
    HeaderStories hs = new HeaderStories(doc);
    assertEquals("", hs.getFirstHeader());
    assertEquals("\r", hs.getEvenHeader());
    assertEquals("", hs.getOddHeader());
    assertEquals("", hs.getFirstFooter());
    assertEquals("", hs.getEvenFooter());
    assertEquals("", hs.getOddFooter());
    WordExtractor ext = new WordExtractor(doc);
    assertEquals("\n", ext.getHeaderText());
    assertEquals("", ext.getFooterText());
    ext.close();
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) Test(org.junit.Test)

Example 24 with HWPFDocument

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

the class TestRangeDelete method testRangeDeleteAll.

/**
	 * Test that we can delete text (all instances of) from our Range with Unicode text.
	 */
public void testRangeDeleteAll() {
    HWPFDocument daDoc = HWPFTestDataSamples.openSampleFile(illustrativeDocFile);
    Range range = daDoc.getRange();
    assertEquals(1, range.numSections());
    Section section = range.getSection(0);
    assertEquals(5, section.numParagraphs());
    Paragraph para = section.getParagraph(2);
    String text = para.text();
    assertEquals(originalText, text);
    boolean keepLooking = true;
    while (keepLooking) {
        // Reload the range every time
        range = daDoc.getRange();
        int offset = range.text().indexOf(searchText);
        if (offset >= 0) {
            int absOffset = range.getStartOffset() + offset;
            Range subRange = new Range(absOffset, (absOffset + searchText.length()), range.getDocument());
            assertEquals(searchText, subRange.text());
            subRange.delete();
        } else {
            keepLooking = false;
        }
    }
    // we need to let the model re-calculate the Range before we use it
    range = daDoc.getRange();
    assertEquals(1, range.numSections());
    section = range.getSection(0);
    assertEquals(5, section.numParagraphs());
    para = section.getParagraph(0);
    text = para.text();
    assertEquals(introText, text);
    para = section.getParagraph(1);
    text = para.text();
    assertEquals(expectedText1, text);
    para = section.getParagraph(2);
    text = para.text();
    assertEquals(expectedText2, text);
    para = section.getParagraph(3);
    text = para.text();
    assertEquals(expectedText3, text);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 25 with HWPFDocument

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

the class TestRangeReplacement method testDocStructure.

/**
	 * Test (more "confirm" than test) that we have the general structure that we expect to have.
	 */
public void testDocStructure() {
    HWPFDocument daDoc = HWPFTestDataSamples.openSampleFile(illustrativeDocFile);
    Range range = daDoc.getRange();
    assertEquals(414, range.text().length());
    assertEquals(1, range.numSections());
    Section section = range.getSection(0);
    assertEquals(414, section.text().length());
    assertEquals(5, section.numParagraphs());
    Paragraph para = section.getParagraph(2);
    assertEquals(5, para.numCharacterRuns());
    String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() + para.getCharacterRun(2).text() + para.getCharacterRun(3).text() + para.getCharacterRun(4).text();
    assertEquals(originalText, text);
}
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