Search in sources :

Example 1 with XWPFHeaderFooterPolicy

use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project LAMSADE-tools by LAntoine.

the class SetCoordinates method setDetails.

/**
	 * Add the user details to the source file and return the result in the
	 * destination file
	 *
	 * @param source
	 * @param destinationFile
	 * @param user
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
public static void setDetails(UserDetails user) throws IOException, InvalidFormatException {
    String source = "com/github/lamsadetools/setCoordinates/papier_en_tete.docx";
    String destination = "com/github/lamsadetools/setCoordinates/papier_en_tete_CloneTest.docx";
    ClassLoader classLoader = SetCoordinates.class.getClassLoader();
    File sourceFile = new File(classLoader.getResource(source).getFile());
    File destinationFile = new File(classLoader.getResource(destination).getFile());
    copy(sourceFile, destinationFile);
    try (XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourceFile))) {
        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
        for (XWPFParagraph p : policy.getFirstPageHeader().getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if ((text != null) && text.contains("Prenom")) {
                        System.out.println("contains prenom");
                        text = text.replace("Prenom", user.getFirstName());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("Nom")) {
                        text = text.replace("Nom", user.getName());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("e-mail")) {
                        text = text.replace("e-mail", user.getEmail());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("tel.")) {
                        text = text.replace("tel.", user.getNumber());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("Fonction")) {
                        text = text.replace("Fonction", user.getFunction());
                        r.setText(text, 0);
                    }
                }
            }
        }
        try (FileOutputStream fos = new FileOutputStream(destinationFile)) {
            doc.write(fos);
        }
    }
    long start = System.currentTimeMillis();
    System.err.println("Generate papier_en_tete.html with " + (System.currentTimeMillis() - start) + "ms");
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) File(java.io.File)

Example 2 with XWPFHeaderFooterPolicy

use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy 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 3 with XWPFHeaderFooterPolicy

use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.

the class XWPFDocument method onDocumentRead.

@SuppressWarnings("deprecation")
@Override
protected void onDocumentRead() throws IOException {
    try {
        DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        ctDocument = doc.getDocument();
        initFootnotes();
        // parse the document with cursor and add
        // the XmlObject to its lists
        XmlCursor cursor = ctDocument.getBody().newCursor();
        cursor.selectPath("./*");
        while (cursor.toNextSelection()) {
            XmlObject o = cursor.getObject();
            if (o instanceof CTP) {
                XWPFParagraph p = new XWPFParagraph((CTP) o, this);
                bodyElements.add(p);
                paragraphs.add(p);
            } else if (o instanceof CTTbl) {
                XWPFTable t = new XWPFTable((CTTbl) o, this);
                bodyElements.add(t);
                tables.add(t);
            } else if (o instanceof CTSdtBlock) {
                XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
                bodyElements.add(c);
                contentControls.add(c);
            }
        }
        cursor.dispose();
        // Sort out headers and footers
        if (doc.getDocument().getBody().getSectPr() != null)
            headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
        // Create for each XML-part in the Package a PartClass
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            String relation = rp.getRelationship().getRelationshipType();
            if (relation.equals(XWPFRelation.STYLES.getRelation())) {
                this.styles = (XWPFStyles) p;
                this.styles.onDocumentRead();
            } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
                this.numbering = (XWPFNumbering) p;
                this.numbering.onDocumentRead();
            } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
                XWPFFooter footer = (XWPFFooter) p;
                footers.add(footer);
                footer.onDocumentRead();
            } else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
                XWPFHeader header = (XWPFHeader) p;
                headers.add(header);
                header.onDocumentRead();
            } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
                // TODO Create according XWPFComment class, extending POIXMLDocumentPart
                CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
                for (CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
                    comments.add(new XWPFComment(ctcomment, this));
                }
            } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
                settings = (XWPFSettings) p;
                settings.onDocumentRead();
            } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
                XWPFPictureData picData = (XWPFPictureData) p;
                picData.onDocumentRead();
                registerPackagePictureData(picData);
                pictures.add(picData);
            } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
                // Until we do, we do need to load the glossary child parts of it
                for (POIXMLDocumentPart gp : p.getRelations()) {
                    // Trigger the onDocumentRead for all the child parts
                    // Otherwise we'll hit issues on Styles, Settings etc on save
                    // TODO: Refactor this to not need to access protected method
                    // from other package! Remove the static helper method once fixed!!!
                    POIXMLDocumentPart._invokeOnDocumentRead(gp);
                }
            }
        }
        initHyperlinks();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) POIXMLException(org.apache.poi.POIXMLException) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) XmlObject(org.apache.xmlbeans.XmlObject)

Example 4 with XWPFHeaderFooterPolicy

use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.

the class XWPFDocument method createHeader.

/**
     * Create a header of the given type
     *
     * @param type {@link HeaderFooterType} enum
     * @return object of type {@link XWPFHeader}
     */
public XWPFHeader createHeader(HeaderFooterType type) {
    XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
    // TODO this needs to be migrated out into section code
    if (type == HeaderFooterType.FIRST) {
        CTSectPr ctSectPr = getSection();
        if (ctSectPr.isSetTitlePg() == false) {
            CTOnOff titlePg = ctSectPr.addNewTitlePg();
            titlePg.setVal(STOnOff.ON);
        }
    // } else if (type == HeaderFooterType.EVEN) {
    // TODO Add support for Even/Odd headings and footers
    }
    return hfPolicy.createHeader(STHdrFtr.Enum.forInt(type.toInt()));
}
Also used : XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)

Example 5 with XWPFHeaderFooterPolicy

use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.

the class XWPFDocument method createFooter.

/**
     * Create a footer of the given type
     *
     * @param type {@link HeaderFooterType} enum
     * @return object of type {@link XWPFFooter}
     */
public XWPFFooter createFooter(HeaderFooterType type) {
    XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
    // TODO this needs to be migrated out into section code
    if (type == HeaderFooterType.FIRST) {
        CTSectPr ctSectPr = getSection();
        if (ctSectPr.isSetTitlePg() == false) {
            CTOnOff titlePg = ctSectPr.addNewTitlePg();
            titlePg.setVal(STOnOff.ON);
        }
    // } else if (type == HeaderFooterType.EVEN) {
    // TODO Add support for Even/Odd headings and footers
    }
    return hfPolicy.createFooter(STHdrFtr.Enum.forInt(type.toInt()));
}
Also used : XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)

Aggregations

XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)17 Test (org.junit.Test)5 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)3 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 XWPFCommentsDecorator (org.apache.poi.xwpf.model.XWPFCommentsDecorator)2 IRunElement (org.apache.poi.xwpf.usermodel.IRunElement)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2 XWPFHyperlink (org.apache.poi.xwpf.usermodel.XWPFHyperlink)2 XWPFHyperlinkRun (org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlObject (org.apache.xmlbeans.XmlObject)2 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)2 CTSectPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr)2 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 QName (javax.xml.namespace.QName)1