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

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFParagraph#getCTP() . 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: 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 2
Source File: StyleUtils.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void styleTableParagraph(XWPFParagraph par, TableStyle style) {
    if (null != par && null != style && null != style.getAlign()) {
        CTP ctp = par.getCTP();
        CTPPr CTPpr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
        CTJc jc = CTPpr.isSetJc() ? CTPpr.getJc() : CTPpr.addNewJc();
        jc.setVal(style.getAlign());
    }

}
 
Example 3
Source File: StyleUtils.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void styleParagraph(XWPFParagraph paragraph, Style style) {
    if (null == paragraph || null == style) return;
    CTP ctp = paragraph.getCTP();
    CTPPr pPr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
    CTParaRPr pr = pPr.isSetRPr() ? pPr.getRPr() : pPr.addNewRPr();
    StyleUtils.styleRpr(pr, style);
}
 
Example 4
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();
}
 
Example 5
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;
}
 
Example 6
Source File: NumberingContinue.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
public static NumberingContinue of(BodyContainer bodyContainer, int start, int end, IterableTemplate iterable) {
    if (start + 1 >= end) return new NumberingContinue();

    final List<IBodyElement> elements = bodyContainer.getBodyElements().subList(start + 1, end);
    if (elements.isEmpty()) return new NumberingContinue();

    CTNumPr first = null;
    int firstPos = -1;
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            CTP ctp = paragraph.getCTP();
            if (ctp.getPPr() != null && ctp.getPPr().getNumPr() != null) {
                CTNumPr numPr = ctp.getPPr().getNumPr();
                // find first
                if (null == first) {
                    first = numPr;
                    firstPos = bodyContainer.getPosOfParagraphCTP(ctp);
                } else {
                    // first is not unique
                    if ((Objects.equals(numPr.getIlvl().getVal(), first.getIlvl().getVal())
                            && Objects.equals(numPr.getNumId().getVal(), first.getNumId().getVal()))) {
                        first = null;
                        break;
                    }
                }
            }
        }
    }
    if (null == first) return new NumberingContinue();

    // the first is unique, if first inside other iterable section
    List<MetaTemplate> templates = iterable.getTemplates();
    for (MetaTemplate template : templates) {
        if (template instanceof IterableTemplate) {
            CTP startCtp = ((XWPFParagraph) ((IterableTemplate) template).getStartRun().getParent()).getCTP();
            CTP endCtp = ((XWPFParagraph) ((IterableTemplate) template).getEndRun().getParent()).getCTP();

            int startPos = bodyContainer.getPosOfParagraphCTP(startCtp);
            if (startPos >= firstPos) break;

            int endPos = bodyContainer.getPosOfParagraphCTP(endCtp);
            if (firstPos > startPos && firstPos < endPos) { return new NumberingContinue(); }
        }
    }
    return new NumberingContinue(first.getNumId().getVal());
}