Search in sources :

Example 81 with HWPFDocument

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

the class TestLists method testOrderedLists.

public void testOrderedLists() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    Range r = doc.getRange();
    assertEquals(40, r.numParagraphs());
    assertEquals("Next up is an ordered list:\r", r.getParagraph(5).text());
    assertEquals("Ordered list 1\r", r.getParagraph(6).text());
    assertEquals("OL 2\r", r.getParagraph(7).text());
    assertEquals("OL 3\r", r.getParagraph(8).text());
    assertEquals("Now for an un-ordered list with a different bullet style:\r", r.getParagraph(9).text());
    assertEquals(9, r.getParagraph(5).getLvl());
    assertEquals(9, r.getParagraph(6).getLvl());
    assertEquals(9, r.getParagraph(7).getLvl());
    assertEquals(9, r.getParagraph(8).getLvl());
    assertEquals(9, r.getParagraph(9).getLvl());
    assertEquals(0, r.getParagraph(5).getIlvl());
    assertEquals(0, r.getParagraph(6).getIlvl());
    assertEquals(0, r.getParagraph(7).getIlvl());
    assertEquals(0, r.getParagraph(8).getIlvl());
    assertEquals(0, r.getParagraph(9).getIlvl());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

Example 82 with HWPFDocument

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

the class TestOfficeDrawings method testWatermark.

/**
     * Tests watermark text extraction
     */
public void testWatermark() throws Exception {
    HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("watermark.doc");
    OfficeDrawing drawing = hwpfDocument.getOfficeDrawingsHeaders().getOfficeDrawings().iterator().next();
    EscherContainerRecord escherContainerRecord = drawing.getOfficeArtSpContainer();
    EscherOptRecord officeArtFOPT = escherContainerRecord.getChildById((short) 0xF00B);
    EscherComplexProperty gtextUNICODE = (EscherComplexProperty) officeArtFOPT.lookup(0x00c0);
    String text = StringUtil.getFromUnicodeLE(gtextUNICODE.getComplexData());
    assertEquals("DRAFT CONTRACT\0", text);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) EscherComplexProperty(org.apache.poi.ddf.EscherComplexProperty) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord)

Example 83 with HWPFDocument

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

the class TestBugs method test50936.

/**
     * Bug 50936 - Exception parsing MS Word 8.0 file
     */
@Test
public void test50936() throws Exception {
    String[] filenames = { "Bug50936_1.doc", "Bug50936_2.doc", "Bug50936_3.doc" };
    for (String filename : filenames) {
        HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile(filename);
        assertNotNull(filename, hwpfDocument.getPicturesTable().getAllPictures());
        hwpfDocument.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 84 with HWPFDocument

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

the class TestBugs method test47287.

/**
     * [RESOLVED FIXED] Bug 47287 - StringIndexOutOfBoundsException in
     * CharacterRun.replaceText()
     */
@Test
public void test47287() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47287.doc");
    String[] values = { "1-1", "1-2", "1-3", "1-4", "1-5", "1-6", "1-7", "1-8", "1-9", "1-10", "1-11", "1-12", "1-13", "1-14", "1-15" };
    int usedVal = 0;
    String PLACEHOLDER = "     ";
    Range r = doc.getRange();
    for (int x = 0; x < r.numSections(); x++) {
        Section s = r.getSection(x);
        for (int y = 0; y < s.numParagraphs(); y++) {
            Paragraph p = s.getParagraph(y);
            for (int z = 0; z < p.numCharacterRuns(); z++) {
                boolean isFound = false;
                // character run
                CharacterRun run = p.getCharacterRun(z);
                // character run text
                String text = run.text();
                String oldText = text;
                int c = text.indexOf("FORMTEXT ");
                if (c < 0) {
                    int k = text.indexOf(PLACEHOLDER);
                    if (k >= 0) {
                        text = text.substring(0, k) + values[usedVal] + text.substring(k + PLACEHOLDER.length());
                        usedVal++;
                        isFound = true;
                    }
                } else {
                    for (; c >= 0; c = text.indexOf("FORMTEXT ", c + "FORMTEXT ".length())) {
                        int k = text.indexOf(PLACEHOLDER, c);
                        if (k >= 0) {
                            text = text.substring(0, k) + values[usedVal] + text.substring(k + PLACEHOLDER.length());
                            usedVal++;
                            isFound = true;
                        }
                    }
                }
                if (isFound) {
                    run.replaceText(oldText, text, 0);
                }
            }
        }
    }
    String docText = r.text();
    assertContains(docText, "1-1");
    assertContains(docText, "1-12");
    assertNotContained(docText, "1-13");
    assertNotContained(docText, "1-15");
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 85 with HWPFDocument

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

the class TestBugs method testRegressionIn315beta2.

@Test
public void testRegressionIn315beta2() {
    HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("cap.stanford.edu_profiles_viewbiosketch_facultyid=4009&name=m_maciver.doc");
    assertNotNull(hwpfDocument);
}
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