use of org.docx4j.wml.TblGridCol in project TranskribusCore by Transkribus.
the class DocxBuilder method getDocxTable.
private static Tbl getDocxTable(WordprocessingMLPackage wPMLpackage, boolean isWordBased, int rows, int cols, List<HashMap<Integer, TrpTableCellType>> allRows, int tablesize, MainDocumentPart mdp) throws Exception {
int writableWidthTwips = wPMLpackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols))).intValue();
Tbl table = TblFactory.createTable(0, 0, cellWidthTwips);
TblGrid tblGrid = factory.createTblGrid();
for (int i = 0; i < cols; i++) {
TblGridCol col = factory.createTblGridCol();
col.setW(BigInteger.valueOf(cellWidthTwips));
tblGrid.getGridCol().add(col);
}
table.setTblGrid(tblGrid);
int i = 0;
for (HashMap<Integer, TrpTableCellType> entry : allRows) {
Tr row = factory.createTr();
table.getContent().add(row);
i++;
int d = 0;
if (entry.keySet().size() != cols) {
logger.debug("size of entries does not match columns ");
}
for (Integer key : entry.keySet()) {
Tc cell = factory.createTc();
row.getContent().add(cell);
String rowSpan = null;
int colSpan = 1;
boolean mergedVertical = false;
int colsize = cellWidthTwips;
if (entry.get(key) != null) {
if (entry.get(key).getRowSpan() > 1) {
mergedVertical = true;
rowSpan = "restart";
}
// PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMaxX();
double maxX = entry.get(key).getBoundingBox().getMaxX();
// PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMinX();
double minX = entry.get(key).getBoundingBox().getMinX();
double colsizeRel = maxX - minX;
double colsizetmp = colsizeRel / (double) tablesize;
// logger.debug("colsizetmp " + colsizetmp);
colsize = (int) (writableWidthTwips * colsizetmp);
colSpan = entry.get(key).getColSpan();
// logger.debug("colsize " + colsize);
// logger.debug("text in this cell is " + entry.get(key).getUnicodeTextFromLines());
int colID = entry.get(key).getCol();
int rowID = entry.get(key).getRow();
} else {
// logger.debug("no cell for this column ");
mergedVertical = true;
}
applyGridSpan(cell, colSpan, rowSpan, colsize, mergedVertical);
P columnPara = factory.createP();
// P columnPara = (P) column.getContent().get(0);
cell.getContent().add(columnPara);
d++;
Text tx = factory.createText();
R run = factory.createR();
if (entry.get(key) != null) {
// old solution till now: tx.setValue(entry.get(key).getUnicodeTextFromLines());
if (entry.get(key).getUnicodeTextFromLines() != "") {
exportTextRegion(entry.get(key), isWordBased, columnPara, mdp);
}
}
run.getContent().add(tx);
columnPara.getContent().add(run);
}
}
return table;
}
use of org.docx4j.wml.TblGridCol in project docx4j-template by vindell.
the class Docx4J_例子2 method setTableGridCol.
// 设置整列宽度
/**
* @param tableWidthPercent
* 表格占页面宽度百分比
* @param widthPercent
* 各列百分比
*/
public void setTableGridCol(WordprocessingMLPackage wordPackage, ObjectFactory factory, Tbl table, double tableWidthPercent, double[] widthPercent) throws Exception {
int width = getWritableWidth(wordPackage);
int tableWidth = (int) (width * tableWidthPercent / 100);
TblGrid tblGrid = factory.createTblGrid();
for (int i = 0; i < widthPercent.length; i++) {
TblGridCol gridCol = factory.createTblGridCol();
gridCol.setW(BigInteger.valueOf((long) (tableWidth * widthPercent[i] / 100)));
tblGrid.getGridCol().add(gridCol);
}
table.setTblGrid(tblGrid);
TblPr tblPr = table.getTblPr();
if (tblPr == null) {
tblPr = factory.createTblPr();
}
TblWidth tblWidth = new TblWidth();
// 这一行是必须的,不自己设置宽度默认是auto
tblWidth.setType("dxa");
tblWidth.setW(new BigInteger(tableWidth + ""));
tblPr.setTblW(tblWidth);
table.setTblPr(tblPr);
}
use of org.docx4j.wml.TblGridCol in project docx4j-template by vindell.
the class Docx4J_简单例子2 method setTableGridCol.
// 设置整列宽度
public void setTableGridCol(WordprocessingMLPackage wordPackage, ObjectFactory factory, Tbl table, double[] widthPercent) throws Exception {
int width = getWritableWidth(wordPackage);
TblGrid tblGrid = factory.createTblGrid();
for (int i = 0; i < widthPercent.length; i++) {
TblGridCol gridCol = factory.createTblGridCol();
gridCol.setW(BigInteger.valueOf((long) (width * widthPercent[i] / 100)));
tblGrid.getGridCol().add(gridCol);
}
table.setTblGrid(tblGrid);
TblPr tblPr = table.getTblPr();
if (tblPr == null) {
tblPr = factory.createTblPr();
}
TblWidth tblWidth = new TblWidth();
// 这一行是必须的,不自己设置宽度默认是auto
tblWidth.setType("dxa");
tblWidth.setW(new BigInteger("" + width));
tblPr.setTblW(tblWidth);
table.setTblPr(tblPr);
}
use of org.docx4j.wml.TblGridCol in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method createTable.
/**
* @Description:创建表格(默认水平居中,垂直居中)
*/
public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception {
colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
rowNum = Math.max(1, rowNum);
Tbl tbl = new Tbl();
StringBuffer tblSb = new StringBuffer();
tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");
tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
// 上边框
tblSb.append("<w:tblBorders>");
tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 左边框
tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 下边框
tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 右边框
tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("</w:tblBorders>");
tblSb.append("</w:tblPr>");
TblPr tblPr = null;
tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
Jc jc = new Jc();
// 单元格居中对齐
jc.setVal(JcEnumeration.CENTER);
tblPr.setJc(jc);
tbl.setTblPr(tblPr);
// 设定各单元格宽度
TblGrid tblGrid = new TblGrid();
tbl.setTblGrid(tblGrid);
for (int i = 0; i < colsNum; i++) {
TblGridCol gridCol = new TblGridCol();
gridCol.setW(BigInteger.valueOf(widthArr[i]));
tblGrid.getGridCol().add(gridCol);
}
// 新增行
for (int j = 0; j < rowNum; j++) {
Tr tr = new Tr();
tbl.getContent().add(tr);
// 列
for (int i = 0; i < colsNum; i++) {
Tc tc = new Tc();
tr.getContent().add(tc);
TcPr tcPr = new TcPr();
TblWidth cellWidth = new TblWidth();
cellWidth.setType("dxa");
cellWidth.setW(BigInteger.valueOf(widthArr[i]));
tcPr.setTcW(cellWidth);
tc.setTcPr(tcPr);
// 垂直居中
setTcVAlign(tc, STVerticalJc.CENTER);
P p = new P();
PPr pPr = new PPr();
pPr.setJc(jc);
p.setPPr(pPr);
R run = new R();
p.getContent().add(run);
tc.getContent().add(p);
}
}
return tbl;
}
use of org.docx4j.wml.TblGridCol in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method addTrByIndex.
/**
* @Description: 在表格指定位置新增一行(默认按表格定义的列数添加)
*/
public void addTrByIndex(Tbl tbl, int index, STVerticalJc vAlign, JcEnumeration hAlign) {
TblGrid tblGrid = tbl.getTblGrid();
Tr tr = new Tr();
if (tblGrid != null) {
List<TblGridCol> gridList = tblGrid.getGridCol();
for (TblGridCol tblGridCol : gridList) {
Tc tc = new Tc();
setTcWidth(tc, tblGridCol.getW().toString());
if (vAlign != null) {
// 垂直居中
setTcVAlign(tc, vAlign);
}
P p = new P();
if (hAlign != null) {
PPr pPr = new PPr();
Jc jc = new Jc();
// 单元格居中对齐
jc.setVal(hAlign);
pPr.setJc(jc);
p.setPPr(pPr);
}
R run = new R();
p.getContent().add(run);
tc.getContent().add(p);
tr.getContent().add(tc);
}
} else {
// 大部分情况都不会走到这一步
Tr firstTr = getTblAllTr(tbl).get(0);
int cellSize = getTcCellSizeWithMergeNum(firstTr);
for (int i = 0; i < cellSize; i++) {
Tc tc = new Tc();
if (vAlign != null) {
// 垂直居中
setTcVAlign(tc, vAlign);
}
P p = new P();
if (hAlign != null) {
PPr pPr = new PPr();
Jc jc = new Jc();
// 单元格居中对齐
jc.setVal(hAlign);
pPr.setJc(jc);
p.setPPr(pPr);
}
R run = new R();
p.getContent().add(run);
tc.getContent().add(p);
tr.getContent().add(tc);
}
}
if (index >= 0 && index < tbl.getContent().size()) {
tbl.getContent().add(index, tr);
} else {
tbl.getContent().add(tr);
}
}
Aggregations