com.aspose.words

Class PreferredWidth

  • java.lang.Object
    • com.aspose.words.PreferredWidth
public class PreferredWidth 
extends java.lang.Object

Represents a value and its unit of measure that is used to specify the preferred width of a table or a cell.

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:

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");

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");
See Also:
Table.PreferredWidth, CellFormat.PreferredWidth

Field Summary
static final PreferredWidthAUTO
Returns an instance that represents the "preferred width is not specified" value.
 
Property Getters/Setters Summary
intgetType()
Gets the unit of measure used for this preferred width value. The value of the property is PreferredWidthType integer constant.
doublegetValue()
Gets the preferred width value. The unit of measure is specified in the Type property.
 
Method Summary
booleanequals(PreferredWidth other)
Determines whether the specified PreferredWidth is equal in value to the current PreferredWidth.
booleanequals(java.lang.Object obj)
Determines whether the specified object is equal in value to the current object.
static PreferredWidthfromPercent(double percent)
A creation method that returns a new instance that represents a preferred width specified as a percentage.
static PreferredWidthfromPoints(double points)
A creation method that returns a new instance that represents a preferred width specified using a number of points.
inthashCode()
Serves as a hash function for this type.
java.lang.StringtoString()
Returns a user-friendly string that displays the value of this object.
 

    • Field Detail

      • AUTO

        public static final PreferredWidth AUTO
        Returns an instance that represents the "preferred width is not specified" value.

        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");
    • Property Getters/Setters Detail

      • getType

        public int getType()
        
        Gets the unit of measure used for this preferred width value. The value of the property is PreferredWidthType integer constant.

        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());
      • getValue

        public double getValue()
        
        Gets the preferred width value. The unit of measure is specified in the Type property.

        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());
    • Method Detail

      • equals

        public boolean equals(PreferredWidth other)
        Determines whether the specified PreferredWidth is equal in value to the current PreferredWidth.

        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");
      • equals

        public boolean equals(java.lang.Object obj)
        Determines whether the specified object is equal in value to the current object.

        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");
      • fromPercent

        public static PreferredWidth fromPercent(double percent)
        A creation method that returns a new instance that represents a preferred width specified as a percentage.
        Parameters:
        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");
      • fromPoints

        public static PreferredWidth fromPoints(double points)
        A creation method that returns a new instance that represents a preferred width specified using a number of points.
        Parameters:
        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());
      • hashCode

        public int hashCode()
        Serves as a hash function for this type.

        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");
      • toString

        public java.lang.String toString()
        Returns a user-friendly string that displays the value of this object.

        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");