Java Code Examples for org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP#addNewFldSimple()

The following examples show how to use org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP#addNewFldSimple() . 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: TocTest.java    From poi-tl with Apache License 2.0 7 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();
}
 
Example 2
Source File: TOCRenderPolicy.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
    XWPFRun run = ((RunTemplate) eleTemplate).getRun();
    run.setText("", 0);

    XWPFParagraph tocPara = (XWPFParagraph) run.getParent();
    CTP ctP = tocPara.getCTP();

    CTSimpleField toc = ctP.addNewFldSimple();
    toc.setInstr("TOC \\o");
    toc.setDirty(STOnOff.TRUE);
}
 
Example 3
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Insert the given {@link MPagination}.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph} to modify
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param mPagination
 *            the {@link MPagination}
 * @return the last inserted {@link XWPFParagraph}
 */
private XWPFParagraph insertMPagination(XWPFParagraph paragraph, XWPFRun run, MPagination mPagination) {
    final XWPFParagraph res;
    switch (mPagination) {
        case newColumn:
            insertFieldRunReplacement(paragraph, run, "").addBreak(BreakType.COLUMN);
            res = paragraph;
            break;

        case newParagraph:
            res = createNewParagraph(generatedDocument, (XWPFParagraph) run.getParent());
            break;

        case newPage:
            insertFieldRunReplacement(paragraph, run, "").addBreak(BreakType.PAGE);
            res = paragraph;
            break;

        case newTableOfContent:
            CTP ctP = paragraph.getCTP();
            CTSimpleField toc = ctP.addNewFldSimple();
            toc.setInstr("TOC \\h");
            toc.setDirty(STOnOff.TRUE);
            res = paragraph;
            break;

        case newTextWrapping:
            run.addBreak(BreakType.TEXT_WRAPPING);
            res = paragraph;
            break;

        case ligneBreak:
            run.addBreak();
            res = paragraph;
            break;

        default:
            throw new IllegalStateException("Not supported MPagination.");
    }

    return res;
}