Search in sources :

Example 91 with HWPFDocument

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

the class TestWordExtractor method testWithFooter.

@Test
public void testWithFooter() throws IOException {
    // Non-unicode
    HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
    WordExtractor extractor1 = new WordExtractor(doc1);
    assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor1.getFooterText());
    assertContains(extractor1.getText(), "Footer Left");
    extractor1.close();
    doc1.close();
    // Unicode
    HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
    WordExtractor extractor2 = new WordExtractor(doc2);
    assertEquals("The footer, with Molière, has Unicode in it.\n", extractor2.getFooterText());
    assertContains(extractor2.getText(), "The footer, with");
    extractor2.close();
    doc2.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 92 with HWPFDocument

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

the class TestBug47563 method test.

private void test(int rows, int columns) throws Exception {
    // POI apparently can't create a document from scratch,
    // so we need an existing empty dummy document
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("empty.doc");
    Range range = doc.getRange();
    range.sanityCheck();
    Table table = range.insertTableBefore((short) columns, rows);
    table.sanityCheck();
    for (int rowIdx = 0; rowIdx < table.numRows(); rowIdx++) {
        TableRow row = table.getRow(rowIdx);
        row.sanityCheck();
        System.out.println("row " + rowIdx);
        for (int colIdx = 0; colIdx < row.numCells(); colIdx++) {
            TableCell cell = row.getCell(colIdx);
            cell.sanityCheck();
            System.out.println("column " + colIdx + ", num paragraphs " + cell.numParagraphs());
            Paragraph par = cell.getParagraph(0);
            par.sanityCheck();
            par.insertBefore("" + (rowIdx * row.numCells() + colIdx));
            par.sanityCheck();
            row.sanityCheck();
            table.sanityCheck();
            range.sanityCheck();
        }
    }
    String text = range.text();
    int mustBeAfter = 0;
    for (int i = 0; i < rows * columns; i++) {
        int next = text.indexOf(Integer.toString(i), mustBeAfter);
        assertTrue("Test with " + rows + "/" + columns + ": Should not find " + i + " but found it at " + next + " in " + text, next != -1);
        mustBeAfter = next;
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 93 with HWPFDocument

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

the class TestBug49820 method test.

public void test() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug49820.doc");
    Range documentRange = doc.getRange();
    StyleSheet styleSheet = doc.getStyleSheet();
    // JUnit asserts
    assertLevels(documentRange, styleSheet, 0, 0, 0);
    assertLevels(documentRange, styleSheet, 1, 1, 1);
    assertLevels(documentRange, styleSheet, 2, 2, 2);
    assertLevels(documentRange, styleSheet, 3, 3, 3);
    assertLevels(documentRange, styleSheet, 4, 4, 4);
    assertLevels(documentRange, styleSheet, 5, 5, 5);
    assertLevels(documentRange, styleSheet, 6, 6, 6);
    assertLevels(documentRange, styleSheet, 7, 7, 7);
    assertLevels(documentRange, styleSheet, 8, 8, 8);
    assertLevels(documentRange, styleSheet, 9, 9, 9);
    assertLevels(documentRange, styleSheet, 10, 9, 0);
    assertLevels(documentRange, styleSheet, 11, 9, 4);
// output to console
/*for (int i=0; i<documentRange.numParagraphs(); i++) {
      Paragraph par = documentRange.getParagraph(i);
      int styleLvl = styleSheet.getParagraphStyle(par.getStyleIndex()).getLvl();
      int parLvl = par.getLvl();
      System.out.println("Style level: " + styleLvl + ", paragraph level: " + parLvl + ", text: " + par.text());
    }*/
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) StyleSheet(org.apache.poi.hwpf.model.StyleSheet)

Example 94 with HWPFDocument

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

the class TestTextPieceTable method testAsciiParts.

/**
     * Check that we do the positions correctly when working with pure-ascii
     */
@Test
public void testAsciiParts() throws Exception {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
    TextPieceTable tbl = doc.getTextTable();
    // All ascii, so stored in one big lump
    assertEquals(1, tbl.getTextPieces().size());
    TextPiece tp = tbl.getTextPieces().get(0);
    assertEquals(0, tp.getStart());
    assertEquals(339, tp.getEnd());
    assertEquals(339, tp.characterLength());
    assertEquals(339, tp.bytesLength());
    assertStartsWith(tp.getStringBuilder().toString(), "This is a sample word document");
    // Save and re-load
    HWPFDocument docB = saveAndReload(doc);
    tbl = docB.getTextTable();
    assertEquals(1, tbl.getTextPieces().size());
    tp = tbl.getTextPieces().get(0);
    assertEquals(0, tp.getStart());
    assertEquals(339, tp.getEnd());
    assertEquals(339, tp.characterLength());
    assertEquals(339, tp.bytesLength());
    assertStartsWith(tp.getStringBuilder().toString(), "This is a sample word document");
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 95 with HWPFDocument

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

the class TestPictures method testPicturesWithTable.

@Test
public void testPicturesWithTable() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug44603.doc");
    List<Picture> pics = doc.getPicturesTable().getAllPictures();
    assertEquals(2, pics.size());
}
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