Search in sources :

Example 1 with XWPFHeader

use of org.apache.poi.xwpf.usermodel.XWPFHeader in project poi by apache.

the class BetterHeaderFooterExample method main.

public static void main(String[] args) throws IOException {
    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");
    // create header/footer functions insert an empty paragraph
    XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT);
    head.createParagraph().createRun().setText("header");
    XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
    foot.createParagraph().createRun().setText("footer");
    OutputStream os = new FileOutputStream(new File("header2.docx"));
    doc.write(os);
    os.close();
    doc.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader)

Example 2 with XWPFHeader

use of org.apache.poi.xwpf.usermodel.XWPFHeader in project poi by apache.

the class HeaderFooterTable method main.

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    // Create a header with a 1 row, 3 column table
    // changes made for issue 57366 allow a new header or footer
    // to be created empty. This is a change. You will have to add
    // either a paragraph or a table to the header or footer for
    // the document to be considered valid.
    XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
    XWPFTable tbl = hdr.createTable(1, 3);
    // Set the padding around text in the cells to 1/10th of an inch
    int pad = (int) (.1 * 1440);
    tbl.setCellMargins(pad, pad, pad, pad);
    // Set table width to 6.5 inches in 1440ths of a point
    tbl.setWidth((int) (6.5 * 1440));
    // Can not yet set table or cell width properly, tables default to
    // autofit layout, and this requires fixed layout
    CTTbl ctTbl = tbl.getCTTbl();
    CTTblPr ctTblPr = ctTbl.addNewTblPr();
    CTTblLayoutType layoutType = ctTblPr.addNewTblLayout();
    layoutType.setType(STTblLayoutType.FIXED);
    // Now set up a grid for the table, cells will fit into the grid
    // Each cell width is 3120 in 1440ths of an inch, or 1/3rd of 6.5"
    BigInteger w = new BigInteger("3120");
    CTTblGrid grid = ctTbl.addNewTblGrid();
    for (int i = 0; i < 3; i++) {
        CTTblGridCol gridCol = grid.addNewGridCol();
        gridCol.setW(w);
    }
    // Add paragraphs to the cells
    XWPFTableRow row = tbl.getRow(0);
    XWPFTableCell cell = row.getCell(0);
    XWPFParagraph p = cell.getParagraphArray(0);
    XWPFRun r = p.createRun();
    r.setText("header left cell");
    cell = row.getCell(1);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header center cell");
    cell = row.getCell(2);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header right cell");
    // Create a footer with a Paragraph
    XWPFFooter ftr = doc.createFooter(HeaderFooterType.DEFAULT);
    p = ftr.createParagraph();
    r = p.createRun();
    r.setText("footer text");
    OutputStream os = new FileOutputStream(new File("headertable.docx"));
    doc.write(os);
    doc.close();
}
Also used : CTTblGridCol(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTTblLayoutType(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTTblGrid(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) File(java.io.File)

Example 3 with XWPFHeader

use of org.apache.poi.xwpf.usermodel.XWPFHeader in project poi by apache.

the class TestXWPFHeaderFooterPolicy method testCreate.

@SuppressWarnings("resource")
public void testCreate() throws Exception {
    XWPFDocument doc = new XWPFDocument();
    assertEquals(null, doc.getHeaderFooterPolicy());
    assertEquals(0, doc.getHeaderList().size());
    assertEquals(0, doc.getFooterList().size());
    XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
    assertNotNull(doc.getHeaderFooterPolicy());
    assertEquals(0, doc.getHeaderList().size());
    assertEquals(0, doc.getFooterList().size());
    // Create a header and a footer
    XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
    XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
    header.createParagraph().createRun().setText("Header Hello");
    footer.createParagraph().createRun().setText("Footer Bye");
    // Save, re-load, and check
    doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
    assertNotNull(doc.getHeaderFooterPolicy());
    assertEquals(1, doc.getHeaderList().size());
    assertEquals(1, doc.getFooterList().size());
    assertEquals("Header Hello\n", doc.getHeaderList().get(0).getText());
    assertEquals("Footer Bye\n", doc.getFooterList().get(0).getText());
}
Also used : XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader)

Example 4 with XWPFHeader

use of org.apache.poi.xwpf.usermodel.XWPFHeader 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

XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)4 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)3 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)2 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)2 BigInteger (java.math.BigInteger)1 XWPFRelation (org.apache.poi.xwpf.usermodel.XWPFRelation)1 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)1 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)1 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)1 CTHdrFtr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr)1 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)1 CTTblGrid (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid)1 CTTblGridCol (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol)1 CTTblLayoutType (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType)1 CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)1 HdrDocument (org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument)1