Search in sources :

Example 1 with CTP

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

the class SimpleDocumentWithHeader method main.

public static void main(String[] args) {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r = p.createRun();
    r.setText("Some Text");
    r.setBold(true);
    r = p.createRun();
    r.setText("Goodbye");
    CTP ctP = CTP.Factory.newInstance();
    CTText t = ctP.addNewR().addNewT();
    t.setStringValue("header");
    pars = new XWPFParagraph[1];
    p = new XWPFParagraph(ctP, doc);
    pars[0] = p;
    XWPFHeaderFooterPolicy hfPolicy = doc.createHeaderFooterPolicy();
    hfPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
    ctP = CTP.Factory.newInstance();
    t = ctP.addNewR().addNewT();
    t.setStringValue("My Footer");
    pars[0] = new XWPFParagraph(ctP, doc);
    hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
    try {
        OutputStream os = new FileOutputStream(new File("header.docx"));
        doc.write(os);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) IOException(java.io.IOException) File(java.io.File) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 2 with CTP

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

the class XWPFHeaderFooterPolicy method buildHdrFtr.

/**
     * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
     * that the XWPFFooter or XWPFHeader object returned by calls to the createHeader(int, XWPFParagraph[])
     * and createFooter(int, XWPFParagraph[]) methods or the getXXXXXHeader/Footer methods where
     * headers or footers had been added to a document since it had been created/opened, returned
     * an object that contained no XWPFParagraph objects even if the header/footer itself did contain
     * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.newInstance();
     * created a brand new instance of the CTHDRFtr class which was then populated with data when
     * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
     * that had previoulsy been instantiated in the createHeader(int, XWPFParagraph[]) or
     * createFooter(int, XWPFParagraph[]) methods.
     */
private CTHdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper) {
    CTHdrFtr ftr = wrapper._getHdrFtr();
    if (paragraphs != null) {
        for (int i = 0; i < paragraphs.length; i++) {
            CTP p = ftr.addNewP();
            ftr.setPArray(i, paragraphs[i].getCTP());
        }
    //        } else {
    //            CTP p = ftr.addNewP();
    //            CTBody body = doc.getDocument().getBody();
    //            if (body.sizeOfPArray() > 0) {
    //                CTP p0 = body.getPArray(0);
    //                if (p0.isSetRsidR()) {
    //                    byte[] rsidr = p0.getRsidR();
    //                    byte[] rsidrdefault = p0.getRsidRDefault();
    //                    p.setRsidP(rsidr);
    //                    p.setRsidRDefault(rsidrdefault);
    //                }
    //            }
    //            CTPPr pPr = p.addNewPPr();
    //            pPr.addNewPStyle().setVal(pStyle);
    }
    return ftr;
}
Also used : CTHdrFtr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 3 with CTP

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

the class XWPFHeaderFooter method removeParagraph.

/**
     * Removes a specific paragraph from this header / footer
     *
     * @param paragraph - {@link XWPFParagraph} object to remove
     */
public void removeParagraph(XWPFParagraph paragraph) {
    if (paragraphs.contains(paragraph)) {
        CTP ctP = paragraph.getCTP();
        XmlCursor c = ctP.newCursor();
        c.removeXml();
        c.dispose();
        paragraphs.remove(paragraph);
        bodyElements.remove(paragraph);
    }
}
Also used : CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 4 with CTP

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

the class XWPFTableCell method setText.

public void setText(String text) {
    CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
    XWPFParagraph par = new XWPFParagraph(ctP, this);
    par.createRun().setText(text);
}
Also used : CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 5 with CTP

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

the class XWPFTableCell method insertNewParagraph.

/**
     * add a new paragraph at position of the cursor
     *
     * @param cursor The XmlCursor structure created with XmlBeans
     * @return the inserted paragraph
     */
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
    if (!isCursorInTableCell(cursor)) {
        return null;
    }
    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);
    p2.dispose();
    cursor.toEndToken();
    return newP;
}
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

CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)28 XmlCursor (org.apache.xmlbeans.XmlCursor)12 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)11 XmlObject (org.apache.xmlbeans.XmlObject)10 Test (org.junit.Test)10 CTPPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr)10 CTR (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR)4 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)4 BigInteger (java.math.BigInteger)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 POIXMLException (org.apache.poi.POIXMLException)2 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)2 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)2 CTOnOff (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff)2 CTSpacing (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing)2 CTLock (com.microsoft.schemas.office.office.CTLock)1 CTFormulas (com.microsoft.schemas.vml.CTFormulas)1 CTGroup (com.microsoft.schemas.vml.CTGroup)1