diff options
author | Mark Murphy <jmarkmurphy@apache.org> | 2016-10-30 18:49:16 +0000 |
---|---|---|
committer | Mark Murphy <jmarkmurphy@apache.org> | 2016-10-30 18:49:16 +0000 |
commit | 1c3d84671ec0fe6b1d909bc8010f44aa71b6a24a (patch) | |
tree | 95c8b9cff06cec0cb15c66daff5298e8f3f5fc65 /src/examples | |
parent | 6e685dad95a330c71bbd9ba44ef300d0e9575c88 (diff) | |
download | poi-1c3d84671ec0fe6b1d909bc8010f44aa71b6a24a.tar.gz poi-1c3d84671ec0fe6b1d909bc8010f44aa71b6a24a.zip |
57366: XWPFTable to Header / Footer
Task-Url: https://bz.apache.org/bugzilla/show_bug.cgi?id=57366
This update contains a breaking change
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1767175 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r-- | src/examples/src/org/apache/poi/xwpf/usermodel/BetterHeaderFooterExample.java | 8 | ||||
-rw-r--r-- | src/examples/src/org/apache/poi/xwpf/usermodel/HeaderFooterTable.java | 97 |
2 files changed, 99 insertions, 6 deletions
diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/BetterHeaderFooterExample.java b/src/examples/src/org/apache/poi/xwpf/usermodel/BetterHeaderFooterExample.java index bb67c5094a..99e7dd1ba5 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/BetterHeaderFooterExample.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/BetterHeaderFooterExample.java @@ -38,14 +38,10 @@ public class BetterHeaderFooterExample { // create header/footer functions insert an empty paragraph XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT); - p = head.getParagraphArray(0); - r = p.createRun(); - r.setText("header"); + head.createParagraph().createRun().setText("header"); XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT); - p = foot.getParagraphArray(0); - r = p.createRun(); - r.setText("footer"); + foot.createParagraph().createRun().setText("footer"); try { OutputStream os = new FileOutputStream(new File("header2.docx")); diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/HeaderFooterTable.java b/src/examples/src/org/apache/poi/xwpf/usermodel/HeaderFooterTable.java new file mode 100644 index 0000000000..cd1d21f48e --- /dev/null +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/HeaderFooterTable.java @@ -0,0 +1,97 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.math.BigInteger; + +import org.apache.poi.wp.usermodel.HeaderFooterType; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblLayoutType; + +public class HeaderFooterTable { + + 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(); + } + +} |