Search in sources :

Example 16 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFHeaderFooter method insertNewParagraph.

/**
     * add a new paragraph at position of the cursor
     *
     * @param cursor
     * @return the inserted paragraph
     */
public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
    if (isCursorInHdrF(cursor)) {
        String uri = CTP.type.getName().getNamespaceURI();
        String localPart = "p";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTP p = (CTP) cursor.getObject();
        XWPFParagraph newP = new XWPFParagraph(p, this);
        XmlObject o = null;
        while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if ((!(o instanceof CTP)) || (CTP) o == p) {
            paragraphs.add(0, newP);
        } else {
            int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
            paragraphs.add(pos, newP);
        }
        int i = 0;
        XmlCursor p2 = p.newCursor();
        cursor.toCursor(p2);
        p2.dispose();
        while (cursor.toPrevSibling()) {
            o = cursor.getObject();
            if (o instanceof CTP || o instanceof CTTbl)
                i++;
        }
        bodyElements.add(i, newP);
        p2 = p.newCursor();
        cursor.toCursor(p2);
        cursor.toEndToken();
        p2.dispose();
        return newP;
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 17 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFHeaderFooter method insertNewTbl.

/**
     * @param cursor
     * @return the inserted table
     */
public XWPFTable insertNewTbl(final XmlCursor cursor) {
    if (isCursorInHdrF(cursor)) {
        String uri = CTTbl.type.getName().getNamespaceURI();
        String localPart = "tbl";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTTbl t = (CTTbl) cursor.getObject();
        XWPFTable newT = new XWPFTable(t, this);
        cursor.removeXmlContents();
        XmlObject o = null;
        while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if (!(o instanceof CTTbl)) {
            tables.add(0, newT);
        } else {
            int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
            tables.add(pos, newT);
        }
        int i = 0;
        XmlCursor cursor2 = t.newCursor();
        while (cursor2.toPrevSibling()) {
            o = cursor2.getObject();
            if (o instanceof CTP || o instanceof CTTbl) {
                i++;
            }
        }
        cursor2.dispose();
        bodyElements.add(i, newT);
        cursor2 = t.newCursor();
        cursor.toCursor(cursor2);
        cursor.toEndToken();
        cursor2.dispose();
        return newT;
    }
    return null;
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) XmlObject(org.apache.xmlbeans.XmlObject) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 18 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFFooter method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    super.onDocumentRead();
    FtrDocument ftrDocument = null;
    InputStream is = null;
    try {
        is = getPackagePart().getInputStream();
        ftrDocument = FtrDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        headerFooter = ftrDocument.getFtr();
        // parse the document with cursor and add
        // the XmlObject to its lists
        XmlCursor cursor = headerFooter.newCursor();
        cursor.selectPath("./*");
        while (cursor.toNextSelection()) {
            XmlObject o = cursor.getObject();
            if (o instanceof CTP) {
                XWPFParagraph p = new XWPFParagraph((CTP) o, this);
                paragraphs.add(p);
                bodyElements.add(p);
            }
            if (o instanceof CTTbl) {
                XWPFTable t = new XWPFTable((CTTbl) o, this);
                tables.add(t);
                bodyElements.add(t);
            }
            if (o instanceof CTSdtBlock) {
                XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
                bodyElements.add(c);
            }
        }
        cursor.dispose();
    } catch (Exception e) {
        throw new POIXMLException(e);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
Also used : InputStream(java.io.InputStream) XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) POIXMLException(org.apache.poi.POIXMLException) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FtrDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument) XmlCursor(org.apache.xmlbeans.XmlCursor) CTSdtBlock(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)

Example 19 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFFootnote method insertNewParagraph.

/**
     * add a new paragraph at position of the cursor
     *
     * @param cursor
     * @return the inserted paragraph
     * @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor)
     */
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
    if (isCursorInFtn(cursor)) {
        String uri = CTP.type.getName().getNamespaceURI();
        String localPart = "p";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTP p = (CTP) cursor.getObject();
        XWPFParagraph newP = new XWPFParagraph(p, this);
        XmlObject o = null;
        while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if ((!(o instanceof CTP)) || (CTP) o == p) {
            paragraphs.add(0, newP);
        } else {
            int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
            paragraphs.add(pos, newP);
        }
        int i = 0;
        XmlCursor p2 = p.newCursor();
        cursor.toCursor(p2);
        p2.dispose();
        while (cursor.toPrevSibling()) {
            o = cursor.getObject();
            if (o instanceof CTP || o instanceof CTTbl)
                i++;
        }
        bodyElements.add(i, newP);
        p2 = p.newCursor();
        cursor.toCursor(p2);
        cursor.toEndToken();
        p2.dispose();
        return newP;
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 20 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFHeaderFooter method readHdrFtr.

public void readHdrFtr() {
    bodyElements = new ArrayList<IBodyElement>();
    paragraphs = new ArrayList<XWPFParagraph>();
    tables = new ArrayList<XWPFTable>();
    // parse the document with cursor and add
    // the XmlObject to its lists
    XmlCursor cursor = headerFooter.newCursor();
    cursor.selectPath("./*");
    while (cursor.toNextSelection()) {
        XmlObject o = cursor.getObject();
        if (o instanceof CTP) {
            XWPFParagraph p = new XWPFParagraph((CTP) o, this);
            paragraphs.add(p);
            bodyElements.add(p);
        }
        if (o instanceof CTTbl) {
            XWPFTable t = new XWPFTable((CTTbl) o, this);
            tables.add(t);
            bodyElements.add(t);
        }
    }
    cursor.dispose();
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)31 XmlCursor (org.apache.xmlbeans.XmlCursor)14 XmlObject (org.apache.xmlbeans.XmlObject)13 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)11 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)5 BigInteger (java.math.BigInteger)4 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3 CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)3 InputStream (java.io.InputStream)2 POIXMLException (org.apache.poi.POIXMLException)2 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)2 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)2 XWPFBorderType (org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType)2 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)2 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)2 CTTblBorders (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders)2 CTTblGrid (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1