use of org.apache.poi.xwpf.usermodel.XWPFTable 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();
}
use of org.apache.poi.xwpf.usermodel.XWPFTable in project tika by apache.
the class XWPFWordExtractorDecorator method extractIBodyText.
private void extractIBodyText(IBody bodyElement, XWPFListManager listManager, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
for (IBodyElement element : bodyElement.getBodyElements()) {
if (element instanceof XWPFParagraph) {
XWPFParagraph paragraph = (XWPFParagraph) element;
extractParagraph(paragraph, listManager, xhtml);
}
if (element instanceof XWPFTable) {
XWPFTable table = (XWPFTable) element;
extractTable(table, listManager, xhtml);
}
if (element instanceof XWPFSDT) {
extractSDT((XWPFSDT) element, xhtml);
}
}
}
use of org.apache.poi.xwpf.usermodel.XWPFTable in project Gargoyle by callakrsos.
the class MSWord method addToTable.
/**
* 테이블을 추가한다.
*
* List의 로우는 각 행을 의미. TreeSet은 테이블 컬럼Index 데이터를 의미.
*
* @param list
*/
public XWPFTable addToTable(List<List<String>> list, ITableCustomProperty property) /*
* ,
* ThFunction
* <
* ,
* U
* ,
* V
* ,
* R
* >
*/
{
if (list == null || list.isEmpty()) {
return null;
}
// -- OR --
// open an existing empty document with styles already defined
// XWPFDocument doc = new XWPFDocument(new
// FileInputStream("base_document.docx"));
int rowSize = list.size();
int colSize = list.get(0).size();
XWPFTable table = doc.createTable(rowSize, colSize);
// Set the table style. If the style is not defined, the table style
// will become "Normal".
{
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");
}
/* Table Width조절을 위한 작업 */
{
CTTbl ctTbl = table.getCTTbl();
CTTblPr tblPr = ctTbl.getTblPr();
CTTblWidth tblW = tblPr.getTblW();
// 화면에 WIDTH를 딱 맞춤.
tblW.setW(BigInteger.valueOf(5000));
tblW.setType(STTblWidth.PCT);
}
// Get a list of the rows in the table
// List<XWPFTableRow> rows = table.getRows();
int rowCt = 0;
int colCt = 0;
for (List<String> set : list) {
XWPFTableRow row = table.getRow(rowCt);
CTTrPr trPr = row.getCtRow().addNewTrPr();
// set row height; units = twentieth of a point, 360 = 0.25"
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(/* 360 */
240));
// get the cells in this row
List<XWPFTableCell> cells = row.getTableCells();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
if (cells.size() == colCt) {
row.createCell();
}
// get a table cell properties element (tcPr)
XWPFTableCell cell = cells.get(colCt);
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
// set vertical alignment to "center"
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
// create cell color element
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
// header row
ctshd.setFill("FFFE99");
} else if (rowCt % 2 == 0) {
// even row
// FFFFFF : 흰색
ctshd.setFill("D3DFEE");
} else {
// odd row
ctshd.setFill("EDF2F8");
}
// get 1st paragraph in cell's paragraph list
XWPFParagraph para = cell.getParagraphs().get(0);
// create a run to contain the content
// 2015.3.17 fix
// XWPFRun rh = para.createRun();
KrXWPFRun rh = new KrXWPFRun(para.createRun());
rh.setFontFamily(fontName);
// style cell as desired
if (colCt == colSize - 1) {
// last column is 10pt Courier
rh.setFontSize(DEFAULT_FONT_SIZE);
}
String content = it.next();
if (rowCt == 0) {
// header row
rh.setText(content);
rh.setFontSize(H5);
rh.setBold(true);
para.setAlignment(ParagraphAlignment.CENTER);
} else if (rowCt % 2 == 0) {
// even row
addTableText(rh, content);
rh.setSubscript(VerticalAlign.BASELINE);
para.setAlignment(ParagraphAlignment.LEFT);
} else {
addTableText(rh, content);
// odd row
rh.setSubscript(VerticalAlign.BASELINE);
para.setAlignment(ParagraphAlignment.LEFT);
}
colCt++;
}
// for cell
colCt = 0;
rowCt++;
}
if (property != null) {
property.doCustom(table);
}
return table;
}
use of org.apache.poi.xwpf.usermodel.XWPFTable in project poi by apache.
the class SimpleTable method createSimpleTable.
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
try {
XWPFTable table = doc.createTable(3, 3);
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
// table cells have a list of paragraphs; there is an initial
// paragraph created when the cell is created. If you create a
// paragraph in the document to put in the cell, it will also
// appear in the document following the table, which is probably
// not the desired result.
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
table.getRow(2).getCell(2).setText("only text");
OutputStream out = new FileOutputStream("simpleTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
use of org.apache.poi.xwpf.usermodel.XWPFTable in project poi by apache.
the class SimpleTable method createStyledTable.
/**
* Create a table with some row and column styling. I "manually" add the
* style name to the table, but don't check to see if the style actually
* exists in the document. Since I'm creating it from scratch, it obviously
* won't exist. When opened in MS Word, the table style becomes "Normal".
* I manually set alternating row colors. This could be done using Themes,
* but that's left as an exercise for the reader. The cells in the last
* column of the table have 10pt. "Courier" font.
* I make no claims that this is the "right" way to do it, but it worked
* for me. Given the scarcity of XWPF examples, I thought this may prove
* instructive and give you ideas for your own solutions.
* @throws Exception
*/
public static void createStyledTable() throws Exception {
// Create a new document from scratch
XWPFDocument doc = new XWPFDocument();
try {
// -- OR --
// open an existing empty document with styles already defined
//XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
// Create a new table with 6 rows and 3 columns
int nRows = 6;
int nCols = 3;
XWPFTable table = doc.createTable(nRows, nCols);
// Set the table style. If the style is not defined, the table style
// will become "Normal".
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");
// Get a list of the rows in the table
List<XWPFTableRow> rows = table.getRows();
int rowCt = 0;
int colCt = 0;
for (XWPFTableRow row : rows) {
// get table row properties (trPr)
CTTrPr trPr = row.getCtRow().addNewTrPr();
// set row height; units = twentieth of a point, 360 = 0.25"
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(360));
// get the cells in this row
List<XWPFTableCell> cells = row.getTableCells();
// add content to each cell
for (XWPFTableCell cell : cells) {
// get a table cell properties element (tcPr)
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
// set vertical alignment to "center"
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
// create cell color element
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
// header row
ctshd.setFill("A7BFDE");
} else if (rowCt % 2 == 0) {
// even row
ctshd.setFill("D3DFEE");
} else {
// odd row
ctshd.setFill("EDF2F8");
}
// get 1st paragraph in cell's paragraph list
XWPFParagraph para = cell.getParagraphs().get(0);
// create a run to contain the content
XWPFRun rh = para.createRun();
// style cell as desired
if (colCt == nCols - 1) {
// last column is 10pt Courier
rh.setFontSize(10);
rh.setFontFamily("Courier");
}
if (rowCt == 0) {
// header row
rh.setText("header row, col " + colCt);
rh.setBold(true);
para.setAlignment(ParagraphAlignment.CENTER);
} else {
// other rows
rh.setText("row " + rowCt + ", col " + colCt);
para.setAlignment(ParagraphAlignment.LEFT);
}
colCt++;
}
// for cell
colCt = 0;
rowCt++;
}
// for row
// write the file
OutputStream out = new FileOutputStream("styledTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
Aggregations