Search in sources :

Example 36 with HWPFDocument

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

the class TestSprms method reload.

private static HWPFDocument reload(HWPFDocument hwpfDocument) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    hwpfDocument.write(baos);
    return new HWPFDocument(new ByteArrayInputStream(baos.toByteArray()));
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 37 with HWPFDocument

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

the class TestSprms method testSprmPJc.

/**
     * Test correct processing of "sprmPJc" by uncompressor
     */
public void testSprmPJc() throws IOException {
    InputStream resourceAsStream = POIDataSamples.getDocumentInstance().openResourceAsStream("Bug49820.doc");
    HWPFDocument hwpfDocument = new HWPFDocument(resourceAsStream);
    resourceAsStream.close();
    assertEquals(1, hwpfDocument.getStyleSheet().getParagraphStyle(8).getJustification());
    hwpfDocument = reload(hwpfDocument);
    assertEquals(1, hwpfDocument.getStyleSheet().getParagraphStyle(8).getJustification());
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 38 with HWPFDocument

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

the class TestBug50075 method test.

public void test() {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug50075.doc");
    Range range = doc.getRange();
    assertEquals(1, range.numParagraphs());
    ListEntry entry = (ListEntry) range.getParagraph(0);
    LFO override = doc.getListTables().getLfo(entry.getIlfo());
    ListLevel level = doc.getListTables().getLevel(override.getLsid(), entry.getIlvl());
    // the bug reproduces, if this call fails with NullPointerException
    level.getNumberText();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) LFO(org.apache.poi.hwpf.model.LFO) ListLevel(org.apache.poi.hwpf.model.ListLevel)

Example 39 with HWPFDocument

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

the class TestHWPFWrite method testInvalidInPlaceWriteInputStream.

@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteInputStream() throws IOException {
    // Can't work for InputStream opened files
    InputStream is = SAMPLES.openResourceAsStream("SampleDoc.doc");
    HWPFDocument doc = new HWPFDocument(is);
    is.close();
    try {
        doc.write();
    } finally {
        doc.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 40 with HWPFDocument

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

the class TestHWPFWrite method testInPlaceWrite.

/**
     * Writing to the file we opened from - note, uses a temp file to avoid
     * changing our test files!
     */
@Test
public void testInPlaceWrite() throws Exception {
    // Setup as a copy of a known-good file
    final File file = TempFile.createTempFile("TestDocument", ".doc");
    InputStream inputStream = SAMPLES.openResourceAsStream("SampleDoc.doc");
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        try {
            IOUtils.copy(inputStream, outputStream);
        } finally {
            outputStream.close();
        }
    } finally {
        inputStream.close();
    }
    // Open from the temp file in read-write mode
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(file, false);
    HWPFDocument doc = new HWPFDocument(poifs.getRoot());
    Range r = doc.getRange();
    assertEquals("I am a test document\r", r.getParagraph(0).text());
    // Change
    r.replaceText("X XX a test document\r", false);
    // Save in-place, close, re-open and check
    doc.write();
    doc.close();
    poifs.close();
    poifs = new NPOIFSFileSystem(file);
    doc = new HWPFDocument(poifs.getRoot());
    r = doc.getRange();
    assertEquals("X XX a test document\r", r.getParagraph(0).text());
    doc.close();
    poifs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) 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