Search in sources :

Example 11 with HWPFDocument

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

the class TestBugs method test51678And51524.

/**
     * Bug 51678 - Extracting text from Bug51524.zip is slow Bug 51524 -
     * PapBinTable constructor is slow
     */
@Test
public void test51678And51524() throws IOException {
    // TODO: refactor into something nicer!
    if (System.getProperty("poi.test.remote") != null) {
        String href = "http://domex.nps.edu/corp/files/govdocs1/007/007488.doc";
        HWPFDocument hwpfDocument = HWPFTestDataSamples.openRemoteFile(href);
        WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
        try {
            wordExtractor.getText();
        } finally {
            wordExtractor.close();
        }
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) Test(org.junit.Test)

Example 12 with HWPFDocument

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

the class TestPictures method test58804_1.

@Ignore("This bug is not fixed yet")
@Test
public void test58804_1() throws Exception {
    HWPFDocument docA = HWPFTestDataSamples.openSampleFile("58804_1.doc");
    expectImages(docA, 1);
    HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
    OutputStream out = new FileOutputStream("/tmp/58804_1_out.doc");
    try {
        docB.write(out);
    } finally {
        out.close();
    }
    expectImages(docB, 1);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with HWPFDocument

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

the class TestBugs method testHWPFSections.

@Test
@SuppressWarnings("SuspiciousNameCombination")
public void testHWPFSections() {
    HWPFDocument document = HWPFTestDataSamples.openSampleFile("Bug53453Section.doc");
    Range overallRange = document.getOverallRange();
    int numParas = overallRange.numParagraphs();
    for (int i = 0; i < numParas; i++) {
        Paragraph para = overallRange.getParagraph(i);
        int numSections = para.numSections();
        for (int j = 0; j < numSections; j++) {
            Section section = para.getSection(j);
            if (para.text().trim().equals("Section1")) {
                assertSection1Margin(section);
            } else if (para.text().trim().equals("Section2")) {
                assertSection2Margin(section);
                // Change the margin widths
                this.section2BottomMargin = (int) (1.5 * AbstractWordUtils.TWIPS_PER_INCH);
                this.section2TopMargin = (int) (1.75 * AbstractWordUtils.TWIPS_PER_INCH);
                this.section2LeftMargin = (int) (0.5 * AbstractWordUtils.TWIPS_PER_INCH);
                this.section2RightMargin = (int) (0.75 * AbstractWordUtils.TWIPS_PER_INCH);
                section.setMarginBottom(this.section2BottomMargin);
                section.setMarginLeft(this.section2LeftMargin);
                section.setMarginRight(this.section2RightMargin);
                section.setMarginTop(this.section2TopMargin);
            }
        }
    }
    // Save away and re-read the document to prove the chages are permanent
    document = HWPFTestDataSamples.writeOutAndReadBack(document);
    overallRange = document.getOverallRange();
    numParas = overallRange.numParagraphs();
    for (int i = 0; i < numParas; i++) {
        Paragraph para = overallRange.getParagraph(i);
        int numSections = para.numSections();
        for (int j = 0; j < numSections; j++) {
            Section section = para.getSection(j);
            if (para.text().trim().equals("Section1")) {
                // No changes to the margins in Section1
                assertSection1Margin(section);
            } else if (para.text().trim().equals("Section2")) {
                // The margins in Section2 have kept the new settings.
                assertSection2Margin(section);
            }
        }
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 14 with HWPFDocument

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

the class TestPictures method testPictureWithAlternativeText.

@Test
public void testPictureWithAlternativeText() {
    HWPFDocument document = HWPFTestDataSamples.openSampleFile("Picture_Alternative_Text.doc");
    PicturesTable pictureTable = document.getPicturesTable();
    Picture picture = pictureTable.getAllPictures().get(0);
    assertEquals("This is the alternative text for the picture.", picture.getDescription());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) Test(org.junit.Test)

Example 15 with HWPFDocument

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

the class TestPictures method testFloatingPictures.

/**
     * In word you can have floating or fixed pictures.
     * Fixed have a  in place with an offset to the
     *  picture data.
     * Floating have a  in place, which references a
     *   which has the offset. More than one can
     *  reference the same 
     */
@Test
public void testFloatingPictures() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("FloatingPictures.doc");
    PicturesTable pictures = doc.getPicturesTable();
    // There are 19 images in the picture, but some are
    //  duplicate floating ones
    assertEquals(17, pictures.getAllPictures().size());
    int plain8s = 0;
    int escher8s = 0;
    int image1s = 0;
    Range r = doc.getRange();
    for (int np = 0; np < r.numParagraphs(); np++) {
        Paragraph p = r.getParagraph(np);
        for (int nc = 0; nc < p.numCharacterRuns(); nc++) {
            CharacterRun cr = p.getCharacterRun(nc);
            if (pictures.hasPicture(cr)) {
                image1s++;
            } else if (pictures.hasEscherPicture(cr)) {
                escher8s++;
            } else if (cr.text().startsWith("")) {
                plain8s++;
            }
        }
    }
    // Total is 20, as the 4 escher 8s all reference
    //  the same regular image
    assertEquals(16, image1s);
    assertEquals(4, escher8s);
    assertEquals(0, plain8s);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) 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