org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP Java Examples

The following examples show how to use org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP. 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: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link CTP} according to its input {@link CTP}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputParagraph
 *            the output {@link CTP}
 * @param inputParagraph
 *            the input {@link CTP}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, CTP outputParagraph, CTP inputParagraph) {
    final List<CTBookmark> oldBookmarks = inputParagraph.getBookmarkStartList();
    final List<CTBookmark> newBookmarks = outputParagraph.getBookmarkStartList();
    for (int bookmarkIndex = 0; bookmarkIndex < oldBookmarks.size(); bookmarkIndex++) {
        bookmarkManager.updateXmlObject(newBookmarks.get(bookmarkIndex), oldBookmarks.get(bookmarkIndex));
    }
    List<CTR> inputRuns = inputParagraph.getRList();
    final List<CTR> outputRuns = outputParagraph.getRList();
    for (int runIndex = 0; runIndex < inputRuns.size(); runIndex++) {
        final CTR inputRun = inputRuns.get(runIndex);
        final CTR outputRun = outputRuns.get(runIndex);
        final List<CTText> inputTexts = inputRun.getInstrTextList();
        final List<CTText> outputTexts = outputRun.getInstrTextList();
        for (int textIndex = 0; textIndex < inputTexts.size(); textIndex++) {
            bookmarkManager.updateXmlObject(outputTexts.get(textIndex), inputTexts.get(textIndex));
        }
    }
}
 
Example #3
Source File: IterableProcessor.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleIterable(IterableTemplate iterableTemplate, BodyContainer bodyContainer, Iterable<?> compute) {
    CTP startCtp = ((XWPFParagraph) iterableTemplate.getStartRun().getParent()).getCTP();
    CTP endCtp = ((XWPFParagraph) iterableTemplate.getEndRun().getParent()).getCTP();

    int startPos = bodyContainer.getPosOfParagraphCTP(startCtp);
    int endPos = bodyContainer.getPosOfParagraphCTP(endCtp);

    NumberingContinue numbringContinue = NumberingContinue.of(bodyContainer, startPos, endPos, iterableTemplate);
    IterableContext context = new IterableContext(startPos, endPos, numbringContinue);

    Iterator<?> iterator = compute.iterator();
    while (iterator.hasNext()) {
        next(iterableTemplate, bodyContainer, context, iterator.next());
    }

    // clear self iterable template
    for (int i = endPos - 1; i > startPos; i--) {
        bodyContainer.removeBodyElement(i);
    }
}
 
Example #4
Source File: MiniTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
public static void renderCell(XWPFTableCell cell, CellRenderData cellData, TableStyle rowStyle) {
    TableStyle cellStyle = (null == cellData.getCellStyle() ? rowStyle : cellData.getCellStyle());
    if (null != cellStyle && null != cellStyle.getBackgroundColor()) {
        cell.setColor(cellStyle.getBackgroundColor());
    }

    TextRenderData renderData = cellData.getCellText();
    if (StringUtils.isBlank(renderData.getText())) return;

    CTTc ctTc = cell.getCTTc();
    CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
    XWPFParagraph par = new XWPFParagraph(ctP, cell);
    StyleUtils.styleTableParagraph(par, cellStyle);

    TextRenderPolicy.Helper.renderTextRun(par.createRun(), renderData);
}
 
Example #5
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new paragraph and replaces the currentParagrap variable.
 * 
 * @param body
 *            the {@link IBody} to modify
 * @param srcParagraph
 *            the origin paragraph to copy the style from
 * @return the created {@link XWPFParagraph}
 */
private XWPFParagraph createNewParagraph(IBody body, XWPFParagraph srcParagraph) {
    final XWPFParagraph res;

    // create a new paragraph.
    res = createParagraph(body);
    CTP ctp = (CTP) srcParagraph.getCTP().copy();
    ctp.getRList().clear();
    ctp.getFldSimpleList().clear();
    ctp.getHyperlinkList().clear();
    res.getCTP().set(ctp);
    int runNb = res.getRuns().size();
    for (int i = 0; i < runNb; i++) {
        res.removeRun(i);
    }
    currentTemplateParagraph = srcParagraph;
    currentGeneratedParagraph = res;

    return res;
}
 
Example #6
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public int getPosOfParagraphCTP(CTP startCtp) {
    IBodyElement current;
    List<IBodyElement> bodyElements = headerFooter.getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph) current).getCTP().equals(startCtp)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #7
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 #8
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 #9
Source File: XWPFParagraphWrapper.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public CTSimpleField insertNewFldSimple(int paramInt) {
    CTP ctp = paragraph.getCTP();
    synchronized (ctp.monitor()) {
        // check_orphaned();
        CTSimpleField localCTSimpleField = null;
        localCTSimpleField = (CTSimpleField) ((CTPImpl) ctp).get_store().insert_element_user(RUN_QNAME_SET,
                FLDSIMPLE_QNAME, paramInt);
        return localCTSimpleField;
    }
}
 
Example #10
Source File: XWPFParagraphWrapper.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public CTR insertNewR(int paramInt) {
    CTP ctp = paragraph.getCTP();
    synchronized (ctp.monitor()) {
        // check_orphaned();
        CTR localCTR = null;
        localCTR = (CTR) ((CTPImpl) ctp).get_store().insert_element_user(RUN_QNAME_SET, R_QNAME, paramInt);
        return localCTR;
    }
}
 
Example #11
Source File: XWPFParagraphWrapper.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public CTMarkupRange insertNewBookmarkEnd(int paramInt) {
    CTP ctp = paragraph.getCTP();
    synchronized (ctp.monitor()) {
        // check_orphaned();
        CTMarkupRange local = null;
        local = (CTMarkupRange) ((CTPImpl) ctp).get_store().insert_element_user(RUN_QNAME_SET, BOOKMARK_END_QNAME,
                paramInt);
        return local;
    }
}
 
Example #12
Source File: XWPFParagraphWrapper.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public CTHyperlink insertNewHyperlink(int paramInt) {
    CTP ctp = paragraph.getCTP();
    synchronized (ctp.monitor()) {
        // check_orphaned();
        CTHyperlink localCTHyperlink = null;
        localCTHyperlink = (CTHyperlink) ((CTPImpl) ctp).get_store().insert_element_user(RUN_QNAME_SET, HYPER_QNAME,
                paramInt);
        return localCTHyperlink;
    }
}
 
Example #13
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public int getPosOfParagraphCTP(CTP startCtp) {
    IBodyElement current;
    List<IBodyElement> bodyElements = cell.getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph) current).getCTP().equals(startCtp)) { return i; }
        }
    }
    return -1;
}
 
Example #14
Source File: NiceXWPFDocument.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public int getPosOfParagraphCTP(CTP bodyObj) {
    IBodyElement current;
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph)current).getCTP().equals(bodyObj)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #15
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 #16
Source File: DocumentBodyContainer.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Override
public int getPosOfParagraphCTP(CTP startCtp) {
    return doc.getPosOfParagraphCTP(startCtp);
}
 
Example #17
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());
}
 
Example #18
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 #19
Source File: BodyContainer.java    From poi-tl with Apache License 2.0 votes vote down vote up
int getPosOfParagraphCTP(CTP startCtp);