Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFParagraph#setStyle()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFParagraph#setStyle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Inserts the given {@link MParagraph}.
 *
 * @param body
 *            the {@link IBody} to insert the paragraph into
 * @param paragraph
 *            the {@link MTable} to insert
 * @param run
 *            the source {@link XWPFRun}
 * @return the last inserted {@link XWPFParagraph}
 */
private XWPFParagraph insertMParagraph(IBody body, MParagraph paragraph, XWPFRun run) {
    final XWPFParagraph newParagraph = createNewParagraph(body, (XWPFParagraph) run.getParent());

    if (paragraph.getStyleName() != null) {
        newParagraph.setStyle(paragraph.getStyleName());
    }
    if (paragraph.getHAlignment() != null) {
        newParagraph.setAlignment(getHAllignment(paragraph.getHAlignment()));
    }
    if (paragraph.getNumberingID() != null) {
        newParagraph.setNumID(BigInteger.valueOf(paragraph.getNumberingID()));
    }
    if (paragraph.getNumberingLevel() != null) {
        newParagraph.getCTP().getPPr().getNumPr().addNewIlvl()
                .setVal(BigInteger.valueOf(paragraph.getNumberingLevel()));
    }

    return insertObject(newParagraph, paragraph.getContents(), run);
}
 
Example 2
Source File: TocTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream("toc.docx"));

    XWPFStyles styles = doc.getStyles();
    System.out.println(styles);

    XWPFParagraph p = doc.createParagraph();
    XWPFRun run = p.createRun();
    run.setText("Heading 1");
    p.getCTP().addNewPPr();
    p.getCTP().getPPr().addNewPStyle();
    p.setStyle(HEADING1);

    XWPFParagraph tocPara = doc.createParagraph();
    CTP ctP = tocPara.getCTP();
    CTSimpleField toc = ctP.addNewFldSimple();
    toc.setInstr("TOC \\h");
    toc.setDirty(STOnOff.TRUE);

    System.out.println(doc.isEnforcedUpdateFields());

    // doc.enforceUpdateFields();

    // doc.createTOC();

    doc.write(new FileOutputStream("CreateWordBookmark.docx"));
    doc.close();
}