Search in sources :

Example 6 with StyleSheet

use of org.apache.poi.hwpf.model.StyleSheet in project poi by apache.

the class TestBug49820 method test.

public void test() throws IOException {
    HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug49820.doc");
    Range documentRange = doc.getRange();
    StyleSheet styleSheet = doc.getStyleSheet();
    // JUnit asserts
    assertLevels(documentRange, styleSheet, 0, 0, 0);
    assertLevels(documentRange, styleSheet, 1, 1, 1);
    assertLevels(documentRange, styleSheet, 2, 2, 2);
    assertLevels(documentRange, styleSheet, 3, 3, 3);
    assertLevels(documentRange, styleSheet, 4, 4, 4);
    assertLevels(documentRange, styleSheet, 5, 5, 5);
    assertLevels(documentRange, styleSheet, 6, 6, 6);
    assertLevels(documentRange, styleSheet, 7, 7, 7);
    assertLevels(documentRange, styleSheet, 8, 8, 8);
    assertLevels(documentRange, styleSheet, 9, 9, 9);
    assertLevels(documentRange, styleSheet, 10, 9, 0);
    assertLevels(documentRange, styleSheet, 11, 9, 4);
// output to console
/*for (int i=0; i<documentRange.numParagraphs(); i++) {
      Paragraph par = documentRange.getParagraph(i);
      int styleLvl = styleSheet.getParagraphStyle(par.getStyleIndex()).getLvl();
      int parLvl = par.getLvl();
      System.out.println("Style level: " + styleLvl + ", paragraph level: " + parLvl + ", text: " + par.text());
    }*/
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) StyleSheet(org.apache.poi.hwpf.model.StyleSheet)

Example 7 with StyleSheet

use of org.apache.poi.hwpf.model.StyleSheet in project poi by apache.

the class Paragraph method newParagraph.

@Internal
public static Paragraph newParagraph(Range parent, PAPX papx) {
    HWPFDocumentCore doc = parent._doc;
    ListTables listTables = doc.getListTables();
    StyleSheet styleSheet = doc.getStyleSheet();
    ParagraphProperties properties = new ParagraphProperties();
    properties.setIstd(papx.getIstd());
    properties = newParagraph_applyStyleProperties(styleSheet, papx, properties);
    properties = ParagraphSprmUncompressor.uncompressPAP(properties, papx.getGrpprl(), 2);
    if (properties.getIlfo() != 0 && listTables != null) {
        LFO lfo = null;
        try {
            lfo = listTables.getLfo(properties.getIlfo());
        } catch (NoSuchElementException exc) {
            log.log(POILogger.WARN, "Paragraph refers to LFO #", properties.getIlfo(), " that does not exists");
        }
        if (lfo != null) {
            final ListLevel listLevel = listTables.getLevel(lfo.getLsid(), properties.getIlvl());
            if (listLevel != null && listLevel.getGrpprlPapx() != null) {
                properties = ParagraphSprmUncompressor.uncompressPAP(properties, listLevel.getGrpprlPapx(), 0);
                // reapply style and local PAPX properties
                properties = newParagraph_applyStyleProperties(styleSheet, papx, properties);
                properties = ParagraphSprmUncompressor.uncompressPAP(properties, papx.getGrpprl(), 2);
            }
        }
    }
    if (properties.getIlfo() > 0)
        return new ListEntry(papx, properties, parent);
    return new Paragraph(papx, properties, parent);
}
Also used : HWPFDocumentCore(org.apache.poi.hwpf.HWPFDocumentCore) StyleSheet(org.apache.poi.hwpf.model.StyleSheet) LFO(org.apache.poi.hwpf.model.LFO) ListTables(org.apache.poi.hwpf.model.ListTables) NoSuchElementException(java.util.NoSuchElementException) ListLevel(org.apache.poi.hwpf.model.ListLevel) Internal(org.apache.poi.util.Internal)

Example 8 with StyleSheet

use of org.apache.poi.hwpf.model.StyleSheet in project poi by apache.

the class Range method insertBefore.

/**
	 * Inserts a paragraph into the front of this range. The paragraph will
	 * contain one character run that has the default properties for the
	 * paragraph's style.
	 *
	 * It is necessary for the text to end with the character '\r'
	 *
	 * @param props
	 *            The paragraph's properties.
	 * @param styleIndex
	 *            The index of the paragraph's style in the style sheet.
	 * @param text
	 *            The text to insert.
	 * @return A newly inserted paragraph.
     * @deprecated POI 3.8 beta 4. Use code shall not work with {@link ParagraphProperties}
	 */
@Deprecated
private Paragraph insertBefore(ParagraphProperties props, int styleIndex, String text) {
    initAll();
    StyleSheet ss = _doc.getStyleSheet();
    ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex);
    CharacterProperties baseChp = ss.getCharacterStyle(styleIndex);
    byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle);
    byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE];
    LittleEndian.putShort(withIndex, 0, (short) styleIndex);
    System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length);
    SprmBuffer buf = new SprmBuffer(withIndex, 2);
    _doc.getParagraphTable().insert(_parStart, _start, buf);
    insertBefore(text, baseChp);
    return getParagraph(0);
}
Also used : StyleSheet(org.apache.poi.hwpf.model.StyleSheet) SprmBuffer(org.apache.poi.hwpf.sprm.SprmBuffer)

Example 9 with StyleSheet

use of org.apache.poi.hwpf.model.StyleSheet in project poi by apache.

the class TestRangeProperties method testAsciiStyling.

public void testAsciiStyling() {
    Range r = a.getRange();
    Paragraph p1 = r.getParagraph(0);
    Paragraph p7 = r.getParagraph(6);
    assertEquals(1, p1.numCharacterRuns());
    assertEquals(1, p7.numCharacterRuns());
    CharacterRun c1 = p1.getCharacterRun(0);
    CharacterRun c7 = p7.getCharacterRun(0);
    // No Calibri
    assertEquals("Times New Roman", c1.getFontName());
    assertEquals("Arial Black", c7.getFontName());
    assertEquals(22, c1.getFontSize());
    assertEquals(32, c7.getFontSize());
    // This document has 15 styles
    assertEquals(15, a.getStyleSheet().numStyles());
    // Ensure none of the paragraphs refer to one that isn't there,
    //  and none of their character runs either
    // Also check all use the default style
    StyleSheet ss = a.getStyleSheet();
    for (int i = 0; i < a.getRange().numParagraphs(); i++) {
        Paragraph p = a.getRange().getParagraph(i);
        int styleIndex = p.getStyleIndex();
        assertTrue(styleIndex < 15);
        assertEquals("Normal", ss.getStyleDescription(styleIndex).getName());
    }
    for (int i = 0; i < a.getRange().numCharacterRuns(); i++) {
        CharacterRun c = a.getRange().getCharacterRun(i);
        int styleIndex = c.getStyleIndex();
        assertTrue(styleIndex < 15);
        assertEquals("Normal", ss.getStyleDescription(styleIndex).getName());
    }
}
Also used : StyleSheet(org.apache.poi.hwpf.model.StyleSheet)

Aggregations

StyleSheet (org.apache.poi.hwpf.model.StyleSheet)9 SprmBuffer (org.apache.poi.hwpf.sprm.SprmBuffer)4 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)2 PAPX (org.apache.poi.hwpf.model.PAPX)2 NoSuchElementException (java.util.NoSuchElementException)1 HWPFDocumentCore (org.apache.poi.hwpf.HWPFDocumentCore)1 LFO (org.apache.poi.hwpf.model.LFO)1 ListLevel (org.apache.poi.hwpf.model.ListLevel)1 ListTables (org.apache.poi.hwpf.model.ListTables)1 Internal (org.apache.poi.util.Internal)1 Test (org.junit.Test)1