Search in sources :

Example 61 with HWPFDocument

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

the class TestLists method testWriteRead.

public void testWriteRead() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
    Range r = doc.getRange();
    // Check a couple at random
    assertEquals(4, r.getParagraph(21).getIlvl());
    assertEquals(1, r.getParagraph(22).getIlvl());
    assertEquals(0, r.getParagraph(23).getIlvl());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 62 with HWPFDocument

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

the class TestLists method testSpecificNumberedOrderedListFeatures.

public void testSpecificNumberedOrderedListFeatures() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    Range r = doc.getRange();
    //29 = OL 2.2.1
    for (int i = 26; i < 30; i++) {
        Paragraph p = r.getParagraph(i);
        assertTrue(p.isInList());
        HWPFList list = p.getList();
        ListLevel level = list.getLVL((char) p.getIlvl());
        assertFalse(level.isLegalNumbering());
        assertEquals(-1, level.getRestart());
    }
    Paragraph p = r.getParagraph(26);
    HWPFList list = p.getList();
    ListLevel level = list.getLVL((char) p.getIlvl());
    byte[] lvl = level.getLevelNumberingPlaceholderOffsets();
    assertEquals((byte) 1, lvl[0]);
    assertEquals((byte) 0, lvl[1]);
    p = r.getParagraph(27);
    list = p.getList();
    level = list.getLVL((char) p.getIlvl());
    lvl = level.getLevelNumberingPlaceholderOffsets();
    assertEquals((byte) 1, lvl[0]);
    assertEquals((byte) 3, lvl[1]);
    p = r.getParagraph(29);
    list = p.getList();
    level = list.getLVL((char) p.getIlvl());
    lvl = level.getLevelNumberingPlaceholderOffsets();
    assertEquals((byte) 1, lvl[0]);
    assertEquals((byte) 3, lvl[1]);
    assertEquals((byte) 5, lvl[2]);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) ListLevel(org.apache.poi.hwpf.model.ListLevel)

Example 63 with HWPFDocument

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

the class TestBorderCode method test.

public void test() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug49919.doc");
    range = doc.getRange();
    Paragraph par = findParagraph("Paragraph normal\r");
    assertEquals(0, par.getLeftBorder().getBorderType());
    assertEquals(0, par.getRightBorder().getBorderType());
    assertEquals(0, par.getTopBorder().getBorderType());
    assertEquals(0, par.getBottomBorder().getBorderType());
    par = findParagraph("Paragraph with border\r");
    assertEquals(18, par.getLeftBorder().getBorderType());
    assertEquals(17, par.getRightBorder().getBorderType());
    assertEquals(18, par.getTopBorder().getBorderType());
    assertEquals(17, par.getBottomBorder().getBorderType());
    assertEquals(15, par.getLeftBorder().getColor());
    par = findParagraph("Paragraph with red border\r");
    assertEquals(1, par.getLeftBorder().getBorderType());
    assertEquals(1, par.getRightBorder().getBorderType());
    assertEquals(1, par.getTopBorder().getBorderType());
    assertEquals(1, par.getBottomBorder().getBorderType());
    assertEquals(6, par.getLeftBorder().getColor());
    par = findParagraph("Paragraph with bordered words.\r");
    assertEquals(0, par.getLeftBorder().getBorderType());
    assertEquals(0, par.getRightBorder().getBorderType());
    assertEquals(0, par.getTopBorder().getBorderType());
    assertEquals(0, par.getBottomBorder().getBorderType());
    assertEquals(3, par.numCharacterRuns());
    CharacterRun chr = par.getCharacterRun(0);
    assertEquals(0, chr.getBorder().getBorderType());
    chr = par.getCharacterRun(1);
    assertEquals(1, chr.getBorder().getBorderType());
    assertEquals(0, chr.getBorder().getColor());
    chr = par.getCharacterRun(2);
    assertEquals(0, chr.getBorder().getBorderType());
    while (pos < range.numParagraphs() - 1) {
        par = range.getParagraph(pos++);
        if (par.isInTable())
            break;
    }
    assertEquals(true, par.isInTable());
    Table tab = range.getTable(par);
    // Border could be defined for the entire row, or for each cell, with the same visual appearance.
    assertEquals(2, tab.numRows());
    TableRow row = tab.getRow(0);
    assertEquals(1, row.getLeftBorder().getBorderType());
    assertEquals(1, row.getRightBorder().getBorderType());
    assertEquals(1, row.getTopBorder().getBorderType());
    assertEquals(1, row.getBottomBorder().getBorderType());
    assertEquals(2, row.numCells());
    TableCell cell = row.getCell(1);
    assertEquals(3, cell.getBrcTop().getBorderType());
    row = tab.getRow(1);
    cell = row.getCell(0);
    // 255 clears border inherited from row
    assertEquals(255, cell.getBrcBottom().getBorderType());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 64 with HWPFDocument

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

the class TestBug46610 method runExtract.

private static String runExtract(String sampleName) throws Exception {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile(sampleName);
    StringBuffer out = new StringBuffer();
    Range globalRange = doc.getRange();
    for (int i = 0; i < globalRange.numParagraphs(); i++) {
        Paragraph p = globalRange.getParagraph(i);
        out.append(p.text());
        out.append("\n");
        for (int j = 0; j < p.numCharacterRuns(); j++) {
            CharacterRun characterRun = p.getCharacterRun(j);
            characterRun.text();
        }
        doc.close();
    }
    return out.toString();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) CharacterRun(org.apache.poi.hwpf.usermodel.CharacterRun) Range(org.apache.poi.hwpf.usermodel.Range) Paragraph(org.apache.poi.hwpf.usermodel.Paragraph)

Example 65 with HWPFDocument

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

the class TestWordExtractor method testEndnote.

@Test
public void testEndnote() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("footnote.doc");
    WordExtractor extractor = new WordExtractor(doc);
    assertExtractedContains(extractor.getEndnoteText(), "TestEndnote");
    // msonfcLCRoman
    assertEquals(0x02, doc.getRange().getSection(0).getEndnoteNumberingFormat());
    // rncCont
    assertEquals(0x00, doc.getRange().getSection(0).getEndnoteRestartQualifier());
    assertEquals(0, doc.getRange().getSection(0).getEndnoteNumberingOffset());
    assertEquals(1, doc.getEndnotes().getNotesCount());
    extractor.close();
    doc.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) 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