mirror of https://github.com/apache/poi.git
#55953 Added methods to position a table
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1823920 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fc0cc15903
commit
6e67073692
|
@ -0,0 +1,54 @@
|
||||||
|
/* ====================================================================
|
||||||
|
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.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets alignment values allowed for Tables and Table Rows
|
||||||
|
*/
|
||||||
|
public enum TableRowAlign {
|
||||||
|
|
||||||
|
LEFT(1),
|
||||||
|
CENTER(2),
|
||||||
|
RIGHT(3);
|
||||||
|
|
||||||
|
private static Map<Integer, TableRowAlign> imap = new HashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (TableRowAlign p : values()) {
|
||||||
|
imap.put(p.getValue(), p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int value;
|
||||||
|
|
||||||
|
private TableRowAlign(int val) {
|
||||||
|
value = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TableRowAlign valueOf(int type) {
|
||||||
|
TableRowAlign err = imap.get(type);
|
||||||
|
if (err == null) throw new IllegalArgumentException("Unknown table row alignment: " + type);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ import org.apache.poi.util.NotImplemented;
|
||||||
import org.apache.poi.util.Removal;
|
import org.apache.poi.util.Removal;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
|
||||||
|
@ -39,6 +40,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -410,6 +412,39 @@ public class XWPFTable implements IBodyElement, ISDTContents {
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current table alignment or NULL
|
||||||
|
*
|
||||||
|
* @return Table Alignment as a {@link TableRowAlign} enum
|
||||||
|
*/
|
||||||
|
public TableRowAlign getTableAlignment() {
|
||||||
|
CTTblPr tPr = getTblPr(false);
|
||||||
|
return tPr == null ? null
|
||||||
|
: tPr.isSetJc() ? TableRowAlign.valueOf(tPr.getJc().getVal().intValue())
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set table alignment to specified {@link TableRowAlign}
|
||||||
|
*
|
||||||
|
* @param ha {@link TableRowAlign} to set
|
||||||
|
*/
|
||||||
|
public void setTableAlignment(TableRowAlign tra) {
|
||||||
|
CTTblPr tPr = getTblPr(true);
|
||||||
|
CTJc jc = tPr.isSetJc() ? tPr.getJc() : tPr.addNewJc();
|
||||||
|
jc.setVal(STJc.Enum.forInt(tra.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the table alignment attribute from a table
|
||||||
|
*/
|
||||||
|
public void removeTableAlignment() {
|
||||||
|
CTTblPr tPr = getTblPr(false);
|
||||||
|
if (tPr != null && tPr.isSetJc()) {
|
||||||
|
tPr.unsetJc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addColumn(XWPFTableRow tabRow, int sizeCol) {
|
private void addColumn(XWPFTableRow tabRow, int sizeCol) {
|
||||||
if (sizeCol > 0) {
|
if (sizeCol > 0) {
|
||||||
for (int i = 0; i < sizeCol; i++) {
|
for (int i = 0; i < sizeCol; i++) {
|
||||||
|
@ -1172,6 +1207,6 @@ public class XWPFTable implements IBodyElement, ISDTContents {
|
||||||
THIN_THICK_MEDIUM_GAP, THICK_THIN_MEDIUM_GAP, THIN_THICK_THIN_MEDIUM_GAP,
|
THIN_THICK_MEDIUM_GAP, THICK_THIN_MEDIUM_GAP, THIN_THICK_THIN_MEDIUM_GAP,
|
||||||
THIN_THICK_LARGE_GAP, THICK_THIN_LARGE_GAP, THIN_THICK_THIN_LARGE_GAP,
|
THIN_THICK_LARGE_GAP, THICK_THIN_LARGE_GAP, THIN_THICK_THIN_LARGE_GAP,
|
||||||
WAVE, DOUBLE_WAVE, DASH_SMALL_GAP, DASH_DOT_STROKED, THREE_D_EMBOSS, THREE_D_ENGRAVE,
|
WAVE, DOUBLE_WAVE, DASH_SMALL_GAP, DASH_DOT_STROKED, THREE_D_EMBOSS, THREE_D_ENGRAVE,
|
||||||
OUTSET, INSET
|
OUTSET, INSET;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,18 @@
|
||||||
==================================================================== */
|
==================================================================== */
|
||||||
package org.apache.poi.xwpf.usermodel;
|
package org.apache.poi.xwpf.usermodel;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
|
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
|
||||||
|
import org.junit.Test;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
|
||||||
|
@ -34,20 +39,13 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for XWPF Tables
|
* Tests for XWPF Tables
|
||||||
*/
|
*/
|
||||||
public class TestXWPFTable extends TestCase {
|
public class TestXWPFTable {
|
||||||
@Override
|
|
||||||
protected void setUp() {
|
|
||||||
/*
|
|
||||||
XWPFDocument doc = new XWPFDocument();
|
|
||||||
p = doc.createParagraph();
|
|
||||||
|
|
||||||
this.ctRun = CTR.Factory.newInstance();
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testConstructor() {
|
public void testConstructor() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
CTTbl ctTable = CTTbl.Factory.newInstance();
|
CTTbl ctTable = CTTbl.Factory.newInstance();
|
||||||
|
@ -70,6 +68,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testTblGrid() {
|
public void testTblGrid() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
CTTbl ctTable = CTTbl.Factory.newInstance();
|
CTTbl ctTable = CTTbl.Factory.newInstance();
|
||||||
|
@ -87,6 +86,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testGetText() {
|
public void testGetText() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
CTTbl table = CTTbl.Factory.newInstance();
|
CTTbl table = CTTbl.Factory.newInstance();
|
||||||
|
@ -106,7 +106,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testCreateRow() {
|
public void testCreateRow() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetWidth() {
|
public void testSetGetWidth() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
|
||||||
|
@ -162,6 +162,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetHeight() {
|
public void testSetGetHeight() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
|
||||||
|
@ -178,6 +179,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetMargins() {
|
public void testSetGetMargins() {
|
||||||
// instantiate the following class so it'll get picked up by
|
// instantiate the following class so it'll get picked up by
|
||||||
// the XmlBean process and added to the jar file. it's required
|
// the XmlBean process and added to the jar file. it's required
|
||||||
|
@ -206,6 +208,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetHBorders() {
|
public void testSetGetHBorders() {
|
||||||
// instantiate the following classes so they'll get picked up by
|
// instantiate the following classes so they'll get picked up by
|
||||||
// the XmlBean process and added to the jar file. they are required
|
// the XmlBean process and added to the jar file. they are required
|
||||||
|
@ -278,6 +281,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetVBorders() {
|
public void testSetGetVBorders() {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
@ -347,6 +351,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetTopBorders() {
|
public void testSetGetTopBorders() {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
@ -389,6 +394,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetBottomBorders() {
|
public void testSetGetBottomBorders() {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
@ -431,6 +437,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetLeftBorders() {
|
public void testSetGetLeftBorders() {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
@ -473,6 +480,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetRightBorders() {
|
public void testSetGetRightBorders() {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
@ -515,6 +523,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetRowBandSize() {
|
public void testSetGetRowBandSize() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
CTTbl ctTable = CTTbl.Factory.newInstance();
|
CTTbl ctTable = CTTbl.Factory.newInstance();
|
||||||
|
@ -529,6 +538,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetGetColBandSize() {
|
public void testSetGetColBandSize() {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
CTTbl ctTable = CTTbl.Factory.newInstance();
|
CTTbl ctTable = CTTbl.Factory.newInstance();
|
||||||
|
@ -543,6 +553,7 @@ public class TestXWPFTable extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testCreateTable() throws Exception {
|
public void testCreateTable() throws Exception {
|
||||||
// open an empty document
|
// open an empty document
|
||||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
|
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
|
||||||
|
@ -572,4 +583,23 @@ public class TestXWPFTable extends TestCase {
|
||||||
fail("Unable to close doc");
|
fail("Unable to close doc");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetGetTableAlignment() {
|
||||||
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
XWPFTable tbl = doc.createTable(1, 1);
|
||||||
|
tbl.setTableAlignment(TableRowAlign.LEFT);
|
||||||
|
assertEquals(TableRowAlign.LEFT, tbl.getTableAlignment());
|
||||||
|
tbl.setTableAlignment(TableRowAlign.CENTER);
|
||||||
|
assertEquals(TableRowAlign.CENTER, tbl.getTableAlignment());
|
||||||
|
tbl.setTableAlignment(TableRowAlign.RIGHT);
|
||||||
|
assertEquals(TableRowAlign.RIGHT, tbl.getTableAlignment());
|
||||||
|
tbl.removeTableAlignment();
|
||||||
|
assertNull(tbl.getTableAlignment());
|
||||||
|
try {
|
||||||
|
doc.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unable to close doc");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue