Search in sources :

Example 71 with HWPFDocument

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

the class TestBugs method test57603SevenRowTable.

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void test57603SevenRowTable() throws Exception {
    HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("57603-seven_columns.doc");
    HWPFDocument hwpfDocument2 = HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument);
    assertNotNull(hwpfDocument2);
    hwpfDocument2.close();
    hwpfDocument.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 72 with HWPFDocument

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

the class TestBugs method test47286.

/**
     * [FAILING] Bug 47286 - Word documents saves in wrong format if source
     * contains form elements
     */
@SuppressWarnings("deprecation")
@Test
public void test47286() throws IOException {
    // Fetch the current text
    HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.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("Bug47286.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", ""));
    assertEquals(doc1.getCharacterTable().getTextRuns().size(), doc2.getCharacterTable().getTextRuns().size());
    List<PlexOfField> expectedFields = doc1.getFieldsTables().getFieldsPLCF(FieldsDocumentPart.MAIN);
    List<PlexOfField> actualFields = doc2.getFieldsTables().getFieldsPLCF(FieldsDocumentPart.MAIN);
    assertEquals(expectedFields.size(), actualFields.size());
    assertTableStructures(doc1.getRange(), doc2.getRange());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) PlexOfField(org.apache.poi.hwpf.model.PlexOfField) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) Test(org.junit.Test)

Example 73 with HWPFDocument

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

the class TestWordExtractor method testWithHeader.

@Test
public void testWithHeader() throws IOException {
    // Non-unicode
    HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
    WordExtractor extractor1 = new WordExtractor(doc1);
    assertEquals("First header column!\tMid header Right header!\n", extractor1.getHeaderText());
    assertContains(extractor1.getText(), "First header column!");
    extractor1.close();
    doc1.close();
    // Unicode
    HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
    WordExtractor extractor2 = new WordExtractor(doc2);
    assertEquals("This is a simple header, with a € euro symbol in it.\n\n", extractor2.getHeaderText());
    assertContains(extractor2.getText(), "This is a simple header");
    extractor2.close();
    doc2.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Test(org.junit.Test)

Example 74 with HWPFDocument

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

the class TestBookmarksTables method testDeleteRange.

public void testDeleteRange() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("pageref.doc");
    Range range = new Range(27, 41, doc);
    range.delete();
    assertEquals(0, doc.getBookmarks().getBookmarksCount());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Range(org.apache.poi.hwpf.usermodel.Range)

Example 75 with HWPFDocument

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

the class TestBookmarksTables method testReplaceTextAfter.

public void testReplaceTextAfter() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("pageref.doc");
    Bookmark bookmark = doc.getBookmarks().getBookmark(0);
    Range range = new Range(bookmark.getStart(), bookmark.getEnd(), doc);
    range.replaceText("1destin2ation3", true);
    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)

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