public class PreferredWidth
extends java.lang.Object
To learn more, visit the Working with Tables documentation article.
Remarks:
Preferred width can be specified as a percentage, number of points or a special "none/auto" value.
The instances of this class are immutable.
Examples:
Shows how to set a table to auto fit to 50% of the width of the page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Cell #1");
builder.insertCell();
builder.write("Cell #2");
builder.insertCell();
builder.write("Cell #3");
table.setPreferredWidth(PreferredWidth.fromPercent(50.0));
doc.save(getArtifactsDir() + "DocumentBuilder.InsertTableWithPreferredWidth.docx");
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
| Modifier and Type | Field and Description |
|---|---|
static PreferredWidth |
AUTO
Returns an instance that represents the "preferred width is not specified" value.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object obj)
Determines whether the specified object is equal in value to the current object.
|
boolean |
equals(PreferredWidth other)
Determines whether the specified
PreferredWidth is equal in value to the current PreferredWidth. |
static PreferredWidth |
fromPercent(double percent)
A creation method that returns a new instance that represents a preferred width specified as a percentage.
|
static PreferredWidth |
fromPoints(double points)
A creation method that returns a new instance that represents a preferred width specified using a number of points.
|
int |
getType()
Gets the unit of measure used for this preferred width value.
|
double |
getValue()
Gets the preferred width value.
|
int |
hashCode() |
java.lang.String |
toString()
Returns a user-friendly string that displays the value of this object.
|
public static PreferredWidth AUTO
Examples:
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
public static PreferredWidth fromPercent(double percent)
Examples:
Shows how to set a table to auto fit to 50% of the width of the page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Cell #1");
builder.insertCell();
builder.write("Cell #2");
builder.insertCell();
builder.write("Cell #3");
table.setPreferredWidth(PreferredWidth.fromPercent(50.0));
doc.save(getArtifactsDir() + "DocumentBuilder.InsertTableWithPreferredWidth.docx");
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
percent - The value must be from 0 to 100.public static PreferredWidth fromPoints(double points)
Examples:
Shows how to use unit conversion tools while specifying a preferred width for a cell.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(ConvertUtil.inchToPoint(3.0)));
builder.insertCell();
Assert.assertEquals(216.0d, table.getFirstRow().getFirstCell().getCellFormat().getPreferredWidth().getValue());
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
points - The value must be from 0 to 22 inches (22 * 72 points).public int getType()
Examples:
Shows how to verify the preferred width type and value of a table cell.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
Cell firstCell = table.getFirstRow().getFirstCell();
Assert.assertEquals(PreferredWidthType.PERCENT, firstCell.getCellFormat().getPreferredWidth().getType());
Assert.assertEquals(11.16d, firstCell.getCellFormat().getPreferredWidth().getValue());
PreferredWidthType constants.public double getValue()
getType() property.
Examples:
Shows how to verify the preferred width type and value of a table cell.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
Cell firstCell = table.getFirstRow().getFirstCell();
Assert.assertEquals(PreferredWidthType.PERCENT, firstCell.getCellFormat().getPreferredWidth().getType());
Assert.assertEquals(11.16d, firstCell.getCellFormat().getPreferredWidth().getValue());
public boolean equals(PreferredWidth other)
PreferredWidth is equal in value to the current PreferredWidth.
Examples:
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
public boolean equals(java.lang.Object obj)
Examples:
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
Examples:
Shows how to set a preferred width for table cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// There are two ways of applying the "PreferredWidth" class to table cells.
// 1 - Set an absolute preferred width based on points:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
// 2 - Set a relative preferred width based on percent of the table's width:
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
builder.insertCell();
// A cell with no preferred width specified will take up the rest of the available space.
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
// Each configuration of the "PreferredWidth" property creates a new object.
Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
builder.getCellFormat().getPreferredWidth().hashCode());
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Automatically sized cell.");
doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
toString in class java.lang.Object