Search in sources :

Example 76 with HWPFDocument

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

the class TestBookmarksTables method testUpdateText.

public void testUpdateText() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("pageref.doc");
    Bookmark bookmark = doc.getBookmarks().getBookmark(0);
    Range range = new Range(bookmark.getStart(), bookmark.getEnd(), doc);
    range.replaceText("destination", "1destin2ation3");
    bookmark = doc.getBookmarks().getBookmark(0);
    assertEquals("userref", bookmark.getName());
    assertEquals(27, bookmark.getStart());
    assertEquals(41, bookmark.getEnd());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Bookmark(org.apache.poi.hwpf.usermodel.Bookmark) Range(org.apache.poi.hwpf.usermodel.Range)

Example 77 with HWPFDocument

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

the class TestBookmarksTables method test.

public void test() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("pageref.doc");
    Bookmarks bookmarks = doc.getBookmarks();
    assertEquals(1, bookmarks.getBookmarksCount());
    Bookmark bookmark = bookmarks.getBookmark(0);
    assertEquals("userref", bookmark.getName());
    assertEquals(27, bookmark.getStart());
    assertEquals(38, bookmark.getEnd());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Bookmarks(org.apache.poi.hwpf.usermodel.Bookmarks) Bookmark(org.apache.poi.hwpf.usermodel.Bookmark)

Example 78 with HWPFDocument

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

the class TestBookmarksTables method testReplaceTextBefore.

public void testReplaceTextBefore() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("pageref.doc");
    Bookmark bookmark = doc.getBookmarks().getBookmark(0);
    Range range = new Range(bookmark.getStart(), bookmark.getEnd(), doc);
    range.replaceText("1destin2ation3", false);
    bookmark = doc.getBookmarks().getBookmark(0);
    assertEquals("userref", bookmark.getName());
    assertEquals(27, bookmark.getStart());
    assertEquals(41, bookmark.getEnd());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Bookmark(org.apache.poi.hwpf.usermodel.Bookmark) Range(org.apache.poi.hwpf.usermodel.Range)

Example 79 with HWPFDocument

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

the class TestSavedByTable method testReadWrite.

/**
     * Tests reading in the entries, comparing them against the expected
     * entries. Then tests writing the document out and reading the entries yet
     * again.
     *
     * @throws Exception if an unexpected error occurs.
     */
@Test
public void testReadWrite() throws IOException {
    // This document is widely available on the internet as "blair.doc".
    // I tried stripping the content and saving the document but my version
    // of Word (from Office XP) strips this table out.
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("saved-by-table.doc");
    // Check what we just read.
    assertEquals("List of saved-by entries was not as expected", expected, doc.getSavedByTable().getEntries());
    // Now write the entire document out, and read it back in...
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    doc.write(byteStream);
    InputStream copyStream = new ByteArrayInputStream(byteStream.toByteArray());
    doc.close();
    HWPFDocument copy = new HWPFDocument(copyStream);
    // And check again.
    assertEquals("List of saved-by entries was incorrect after writing", expected, copy.getSavedByTable().getEntries());
    copy.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 80 with HWPFDocument

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

the class TestLists method testMultiLevelLists.

public void testMultiLevelLists() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Lists.doc");
    Range r = doc.getRange();
    assertEquals(40, r.numParagraphs());
    assertEquals("Multi-level un-ordered list:\r", r.getParagraph(12).text());
    assertEquals("ML 1:1\r", r.getParagraph(13).text());
    assertEquals("ML 1:2\r", r.getParagraph(14).text());
    assertEquals("ML 2:1\r", r.getParagraph(15).text());
    assertEquals("ML 2:2\r", r.getParagraph(16).text());
    assertEquals("ML 2:3\r", r.getParagraph(17).text());
    assertEquals("ML 3:1\r", r.getParagraph(18).text());
    assertEquals("ML 4:1\r", r.getParagraph(19).text());
    assertEquals("ML 5:1\r", r.getParagraph(20).text());
    assertEquals("ML 5:2\r", r.getParagraph(21).text());
    assertEquals("ML 2:4\r", r.getParagraph(22).text());
    assertEquals("ML 1:3\r", r.getParagraph(23).text());
    assertEquals("Multi-level ordered list:\r", r.getParagraph(24).text());
    assertEquals("OL 1\r", r.getParagraph(25).text());
    assertEquals("OL 2\r", r.getParagraph(26).text());
    assertEquals("OL 2.1\r", r.getParagraph(27).text());
    assertEquals("OL 2.2\r", r.getParagraph(28).text());
    assertEquals("OL 2.2.1\r", r.getParagraph(29).text());
    assertEquals("OL 2.2.2\r", r.getParagraph(30).text());
    assertEquals("OL 2.2.2.1\r", r.getParagraph(31).text());
    assertEquals("OL 2.2.3\r", r.getParagraph(32).text());
    assertEquals("OL 3\r", r.getParagraph(33).text());
    assertEquals("Finally we want some indents, to tell the difference\r", r.getParagraph(34).text());
    for (int i = 12; i <= 34; i++) {
        assertEquals(9, r.getParagraph(12).getLvl());
    }
    assertEquals(0, r.getParagraph(12).getIlvl());
    assertEquals(0, r.getParagraph(13).getIlvl());
    assertEquals(0, r.getParagraph(14).getIlvl());
    assertEquals(1, r.getParagraph(15).getIlvl());
    assertEquals(1, r.getParagraph(16).getIlvl());
    assertEquals(1, r.getParagraph(17).getIlvl());
    assertEquals(2, r.getParagraph(18).getIlvl());
    assertEquals(3, r.getParagraph(19).getIlvl());
    assertEquals(4, r.getParagraph(20).getIlvl());
    assertEquals(4, r.getParagraph(21).getIlvl());
    assertEquals(1, r.getParagraph(22).getIlvl());
    assertEquals(0, r.getParagraph(23).getIlvl());
    assertEquals(0, r.getParagraph(24).getIlvl());
    assertEquals(0, r.getParagraph(25).getIlvl());
    assertEquals(0, r.getParagraph(26).getIlvl());
    assertEquals(1, r.getParagraph(27).getIlvl());
    assertEquals(1, r.getParagraph(28).getIlvl());
    assertEquals(2, r.getParagraph(29).getIlvl());
    assertEquals(2, r.getParagraph(30).getIlvl());
    assertEquals(3, r.getParagraph(31).getIlvl());
    assertEquals(2, r.getParagraph(32).getIlvl());
    assertEquals(0, r.getParagraph(33).getIlvl());
    assertEquals(0, r.getParagraph(34).getIlvl());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument)

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