public class PreferredWidth
Preferred width can be specified as a percentage, number of points or a special "none/auto" value. The instances of this class are immutable. Example: Example:
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");
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 attribute 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");
Field Summary | ||
---|---|---|
static final PreferredWidth | AUTO | |
Returns an instance that represents the "preferred width is not specified" value.
|
Property Getters/Setters Summary | ||
---|---|---|
int | getType() | |
Gets the unit of measure used for this preferred width value.
The value of the property is PreferredWidthType integer constant. |
||
double | getValue() | |
Gets the preferred width value. The unit of measure is specified in the |
Method Summary | ||
---|---|---|
boolean | equals(PreferredWidth other) | |
Determines whether the specified PreferredWidth is equal in value to the current PreferredWidth.
|
||
boolean | equals(java.lang.Object obj) | |
Determines whether the specified object is equal in value to the current object.
|
||
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 | hashCode() | |
Serves as a hash function for this type.
|
||
java.lang.String | toString() | |
Returns a user-friendly string that displays the value of this object.
|
public static final PreferredWidth AUTO
Example:
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 attribute 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 int getType()
Example:
Shows how to verify the preferred width type of a table cell.Document doc = new Document(getMyDir() + "Tables.docx"); // Find the first table in the document Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); Cell firstCell = table.getFirstRow().getFirstCell(); Assert.assertEquals(PreferredWidthType.PERCENT, firstCell.getCellFormat().getPreferredWidth().getType()); Assert.assertEquals(11.16, firstCell.getCellFormat().getPreferredWidth().getValue());
public double getValue()
Example:
Shows how to verify the preferred width type of a table cell.Document doc = new Document(getMyDir() + "Tables.docx"); // Find the first table in the document Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); Cell firstCell = table.getFirstRow().getFirstCell(); Assert.assertEquals(PreferredWidthType.PERCENT, firstCell.getCellFormat().getPreferredWidth().getType()); Assert.assertEquals(11.16, firstCell.getCellFormat().getPreferredWidth().getValue());
public boolean equals(PreferredWidth other)
Example:
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 attribute 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)
Example:
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 attribute 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)
percent
- The value must be from 0 to 100.Example:
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 attribute 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");
Example:
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");
public static PreferredWidth fromPoints(double points)
points
- The value must be from 0 to 22 inches (22 * 72 points).Example:
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 attribute 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");
Example:
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());
public int hashCode()
Example:
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 attribute 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 java.lang.String toString()
Example:
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 attribute 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");