Search in sources :

Example 1 with HdrDocument

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

the class XWPFHeader method onDocumentRead.

/**
     * reads the document
     *
     * @throws IOException
     */
@Override
protected void onDocumentRead() throws IOException {
    super.onDocumentRead();
    HdrDocument hdrDocument = null;
    InputStream is = null;
    try {
        is = getPackagePart().getInputStream();
        hdrDocument = HdrDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        headerFooter = hdrDocument.getHdr();
        // 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 (XmlException e) {
        throw new POIXMLException(e);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
Also used : HdrDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument) InputStream(java.io.InputStream) XmlException(org.apache.xmlbeans.XmlException) 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) XmlCursor(org.apache.xmlbeans.XmlCursor) CTSdtBlock(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)

Example 2 with HdrDocument

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

the class XWPFHeaderFooterPolicy method createHeader.

/**
     * Creates a new header of the specified type, to which the
     * supplied (and previously unattached!) paragraphs are
     * added to.
     */
public XWPFHeader createHeader(Enum type, XWPFParagraph[] pars) {
    XWPFHeader header = getHeader(type);
    if (header == null) {
        HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
        XWPFRelation relation = XWPFRelation.HEADER;
        int i = getRelationIndex(relation);
        XWPFHeader wrapper = (XWPFHeader) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
        wrapper.setXWPFDocument(doc);
        String pStyle = "Header";
        CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
        wrapper.setHeaderFooter(hdr);
        hdrDoc.setHdr(hdr);
        assignHeader(wrapper, type);
        header = wrapper;
    }
    return header;
}
Also used : XWPFRelation(org.apache.poi.xwpf.usermodel.XWPFRelation) HdrDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument) CTHdrFtr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader)

Aggregations

HdrDocument (org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument)2 InputStream (java.io.InputStream)1 POIXMLException (org.apache.poi.POIXMLException)1 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)1 XWPFRelation (org.apache.poi.xwpf.usermodel.XWPFRelation)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 XmlObject (org.apache.xmlbeans.XmlObject)1 CTHdrFtr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr)1 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)1 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)1 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)1