Search in sources :

Example 51 with HWPFDocument

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

the class TestBugs method test51604p2.

/**
     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
     * release from download site )
     */
@Test
public void test51604p2() throws Exception {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51604.doc");
    Range range = doc.getRange();
    int numParagraph = range.numParagraphs();
    replaceText(range, numParagraph);
    doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
    final FileInformationBlock fileInformationBlock = doc.getFileInformationBlock();
    int totalLength = 0;
    for (SubdocumentType type : SubdocumentType.values()) {
        final int partLength = fileInformationBlock.getSubdocumentTextStreamLength(type);
        assert (partLength >= 0);
        totalLength += partLength;
    }
    assertEquals(doc.getText().length(), totalLength);
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) FileInformationBlock(org.apache.poi.hwpf.model.FileInformationBlock) SubdocumentType(org.apache.poi.hwpf.model.SubdocumentType) Test(org.junit.Test)

Example 52 with HWPFDocument

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

the class TestBugs method test45473.

/**
     * Bug 45473 - HWPF cannot read file after save
     */
@Test
public void test45473() throws IOException {
    // Fetch the current text
    HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
    WordExtractor wordExtractor = new WordExtractor(doc1);
    final String text1;
    try {
        text1 = wordExtractor.getText().trim();
    } finally {
        wordExtractor.close();
        doc1.close();
    }
    // Re-load, then re-save and re-check
    doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
    HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
    WordExtractor wordExtractor2 = new WordExtractor(doc2);
    final String text2;
    try {
        text2 = wordExtractor2.getText().trim();
    } finally {
        wordExtractor2.close();
        doc1.close();
    }
    // the text in the saved document has some differences in line
    // separators but we tolerate that
    assertEqualsIgnoreNewline(text1.replaceAll("\n", ""), text2.replaceAll("\n", ""));
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) Test(org.junit.Test)

Example 53 with HWPFDocument

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

the class TestPictures method testEmbededDocumentIcon.

/**
     * When you embed another office document into Word, it stores
     *  a rendered "icon" picture of what that document looks like.
     * This image is re-created when you edit the embeded document,
     *  then used as-is to speed things up.
     * Check that we can properly read one of these
     */
@Test
public void testEmbededDocumentIcon() {
    // This file has two embeded excel files, an embeded powerpoint
    //   file and an embeded word file, in that order
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("word_with_embeded.doc");
    // Check we don't break loading the pictures
    doc.getPicturesTable().getAllPictures();
    PicturesTable pictureTable = doc.getPicturesTable();
    // Check the text, and its embeded images
    Paragraph p;
    Range r = doc.getRange();
    assertEquals(1, r.numSections());
    assertEquals(5, r.numParagraphs());
    p = r.getParagraph(0);
    assertEquals(2, p.numCharacterRuns());
    assertEquals("I have lots of embedded files in me\r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    p = r.getParagraph(1);
    assertEquals(5, p.numCharacterRuns());
    assertEquals(" EMBED Excel.Sheet.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    p = r.getParagraph(2);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED Excel.Sheet.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(5)));
    p = r.getParagraph(3);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED PowerPoint.Show.8  \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(5)));
    p = r.getParagraph(4);
    assertEquals(6, p.numCharacterRuns());
    assertEquals(" EMBED Word.Document.8 \\s \r", p.text());
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(0)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(1)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(2)));
    assertEquals(true, pictureTable.hasPicture(p.getCharacterRun(3)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(4)));
    assertEquals(false, pictureTable.hasPicture(p.getCharacterRun(5)));
    // Look at the pictures table
    List<Picture> pictures = pictureTable.getAllPictures();
    assertEquals(4, pictures.size());
    Picture picture = pictures.get(0);
    assertEquals("emf", picture.suggestFileExtension());
    assertEquals("0.emf", picture.suggestFullFileName());
    assertEquals("image/x-emf", picture.getMimeType());
    picture = pictures.get(1);
    assertEquals("emf", picture.suggestFileExtension());
    assertEquals("469.emf", picture.suggestFullFileName());
    assertEquals("image/x-emf", picture.getMimeType());
    picture = pictures.get(2);
    assertEquals("emf", picture.suggestFileExtension());
    assertEquals("8c7.emf", picture.suggestFullFileName());
    assertEquals("image/x-emf", picture.getMimeType());
    picture = pictures.get(3);
    assertEquals("emf", picture.suggestFileExtension());
    assertEquals("10a8.emf", picture.suggestFullFileName());
    assertEquals("image/x-emf", picture.getMimeType());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) Test(org.junit.Test)

Example 54 with HWPFDocument

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

the class TestPictures method testDifferentImages.

/**
	 * pngs and jpegs
	 */
@Test
public void testDifferentImages() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testPictures.doc");
    List<Picture> pics = doc.getPicturesTable().getAllPictures();
    assertNotNull(pics);
    assertEquals(7, pics.size());
    for (Picture pic : pics) {
        assertNotNull(pic.suggestFileExtension());
        assertNotNull(pic.suggestFullFileName());
    }
    assertEquals("jpg", pics.get(0).suggestFileExtension());
    assertEquals("image/jpeg", pics.get(0).getMimeType());
    assertEquals("jpg", pics.get(1).suggestFileExtension());
    assertEquals("image/jpeg", pics.get(1).getMimeType());
    assertEquals("png", pics.get(3).suggestFileExtension());
    assertEquals("image/png", pics.get(3).getMimeType());
    assertEquals("png", pics.get(4).suggestFileExtension());
    assertEquals("image/png", pics.get(4).getMimeType());
    assertEquals("wmf", pics.get(5).suggestFileExtension());
    assertEquals("image/x-wmf", pics.get(5).getMimeType());
    assertEquals("jpg", pics.get(6).suggestFileExtension());
    assertEquals("image/jpeg", pics.get(6).getMimeType());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 55 with HWPFDocument

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

the class TestNotesTables method test.

public void test() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("endingnote.doc");
    Notes notes = doc.getEndnotes();
    assertEquals(1, notes.getNotesCount());
    assertEquals(10, notes.getNoteAnchorPosition(0));
    assertEquals(0, notes.getNoteTextStartOffset(0));
    assertEquals(19, notes.getNoteTextEndOffset(0));
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Notes(org.apache.poi.hwpf.usermodel.Notes)

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