Table column Removal - Documentation not present


#1

Hi Team,

I was trying to see example for removing a column which is mentioned in https://docs.aspose.com/display/wordsjava/Working+with+Columns+and+Rows.

I can’t see any example for “Below example shows how to remove a column from a table in a document.”

Can you please let me know where I can find it?

Regards,
Sandesh


#2

@sandesh.dsouza,

This particular example code is being fetched from this Gist and is correctly visible in article page on our end. It looks to be some network problem on your end or some browser’s plugin is blocking it.

Do you see problem with only this code snippet, or all the code samples are not visible on your end?

I have also copied below the example code from the article for your reference:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Table.Document.doc");
Table table = (Table)doc.getChild(NodeType.TABLE, 1, true);

// Get the third column from the table and remove it.
Column column = Column.fromIndex(table, 2);
column.remove();

doc.save(dataDir + "Table.RemoveColumn Out.doc");

#3

I don’t see any any code samples. Also, where is Column class present? I tried to import it but cannot find it. Is it in Aspose.words library?

Regards,
Sandesh


#4

@sandesh.dsouza,

You can get complete examples from the following link:

I have also copied below the Column class code:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
**
 * Represents a facade object for a column of a table in a Microsoft Word
 * document.
 */
public class Column {
	
	private int mColumnIndex;
	private Table mTable;
	
	private Column(Table table, int columnIndex) {
		if (table == null)
			throw new IllegalArgumentException("table");

		mTable = table;
		mColumnIndex = columnIndex;
	}

	/**
	 * Returns a new column facade from the table and supplied zero-based index.
	 */
	public static Column fromIndex(Table table, int columnIndex) {
		return new Column(table, columnIndex);
	}

	/**
	 * Returns the cells which make up the column.
	 */
	public Cell[] getCells() {
		ArrayList<Cell> columnCells = getColumnCells();
		return (Cell[]) columnCells.toArray(new Cell[columnCells.size()]);
	}

	/**
	 * Returns the index of the given cell in the column.
	 */
	public int indexOf(Cell cell) {
		return getColumnCells().indexOf(cell);
	}

	/**
	 * Inserts a brand new column before this column into the table.
	 * @throws Exception 
	 */
	public Column insertColumnBefore() throws Exception {
		Cell[] columnCells = getCells();

		if (columnCells.length == 0)
			throw new IllegalArgumentException("Column must not be empty");

		// Create a clone of this column.
		for (Cell cell : columnCells)
			cell.getParentRow().insertBefore(cell.deepClone(false), cell);

		// This is the new column.
		Column column = new Column(columnCells[0].getParentRow().getParentTable(), mColumnIndex);

		// We want to make sure that the cells are all valid to work with (have at least one paragraph).
		for (Cell cell : column.getCells())
			cell.ensureMinimum();

		// Increase the index which this column represents since there is now one extra column infront.
		mColumnIndex++;

		return column;
	}

	/**
	 * Removes the column from the table.
	 */
	public void remove() {
		for (Cell cell : getCells())
			cell.remove();
	}

	/**
	 * Returns the text of the column.
	 */
	public String toTxt() throws Exception {
		StringBuilder builder = new StringBuilder();

		for (Cell cell : getCells())
			builder.append(cell.toString(SaveFormat.TEXT));

		return builder.toString();
	}

	/**
	 * Provides an up-to-date collection of cells which make up the column
	 * represented by this facade.
	 */
	private ArrayList<Cell> getColumnCells() {
		ArrayList<Cell> columnCells = new ArrayList<Cell>();

		for (Row row : mTable.getRows()) {
			Cell cell = row.getCells().get(mColumnIndex);
			if (cell != null)
				columnCells.add(cell);
		}

		return columnCells;
	}
} 

Please let me know if I can be of any further assistance.