DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Android Cloud Apps with Azure
  • How to Build a React Native Chat App for Android
  • Designing a Blog Application Using Document Databases
  • Dynamic Watermarking of Bitmaps in Databases

Trending

  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Real-Time AI Inference at Scale Using Cloud Run, GPUs, and Vertex AI
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • YOLOv5 PyTorch Tutorial
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Work with Merged Cells in Word Documents Table inside Android Apps

How to Work with Merged Cells in Word Documents Table inside Android Apps

By 
David Zondray user avatar
David Zondray
·
Apr. 22, 15 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
6.4K Views

Join the DZone community and get the full member experience.

Join For Free
This technical tip shows how developers can work with merged cells in a Word documents inside Android applications. Several cells in a table can be merged together into a single cell. This is useful when crows require a title or large blocks of text which span across the width of the table. This can only be achieved by merging cells in the table into a single cell. Aspose.Words supports merged cells when working with all input formats including when importing HTML content. In Aspose.Words, merged cells are represented by CellFormat.HorizontalMerge and CellFormat.VerticalMerge. The CellFormat.HorizontalMerge property describes if the cell is part of a horizontal merge of cells. Likewise the CellFormat.VerticalMerge property describes if the cell is a part of a vertical merge of cells. The values of these properties are what define the merge behavior of cells.

  • The first cell in a sequence of merged cells will have CellMerge.First.
  • Any subsequent merged cells has CellMerge.Previous.
  • A cell which is not merged has CellMerge.None.

Sometimes when you load an existing document cells in a table will appear merged. However these can be in fact one long cell. Microsoft Word at times is known to export merged cells in this way. This can cause confusion when attempting to work with individual cells. There appears to be no particular pattern as to when this happens.

//Checking if a Cell is Merged

// Prints the horizontal and vertical merge type of a cell.

public void checkCellsMerged() throws Exception
{
    Document doc = new Document(getMyDir() + "Table.MergedCells.doc");

    // Retrieve the first table in the document.
    Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

    for (Row row : table.getRows())
    {
        for (Cell cell : row.getCells())
        {
            System.out.println(printCellMergeType(cell));
        }
    }

}

public String printCellMergeType(Cell cell)
{
    boolean isHorizontallyMerged = cell.getCellFormat().getHorizontalMerge() != CellMerge.NONE;
    boolean isVerticallyMerged = cell.getCellFormat().getVerticalMerge() != CellMerge.NONE;
    String cellLocation = MessageFormat.format("R{0}, C{1}", cell.getParentRow().getParentTable().indexOf(cell.getParentRow()) + 1, cell.getParentRow().indexOf(cell) + 1);

    if (isHorizontallyMerged && isVerticallyMerged)
        return MessageFormat.format("The cell at {0} is both horizontally and vertically merged", cellLocation);
    else if (isHorizontallyMerged)
        return MessageFormat.format("The cell at {0} is horizontally merged.", cellLocation);
    else if (isVerticallyMerged)
        return MessageFormat.format("The cell at {0} is vertically merged", cellLocation);
    else
        return MessageFormat.format("The cell at {0} is not merged", cellLocation);
}

//Merging Cells in a Table

//Creates a table with two rows with cells in the first row horizontally merged. 

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

builder.insertCell();
// This cell is merged to the previous and should be empty.
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.endRow();

builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write("Text in one cell.");

builder.insertCell();
builder.write("Text in another cell.");
builder.endRow();
builder.endTable();
 
//Example: Merging Cells Vertically

//Creates a table with two columns with cells merged vertically in the first column. 

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in one cell");
builder.endRow();

builder.insertCell();
// This cell is vertically merged to the cell above and should be empty.
builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in another cell");
builder.endRow();
builder.endTable();

//Merging all Cells in a Range

//A method which merges all cells of a table in the specified range of cells
/**
 * Merges the range of cells found between the two specified cells both horizontally and vertically. Can span over multiple rows.
 */
public static void mergeCells(Cell startCell, Cell endCell)
{
    Table parentTable = startCell.getParentRow().getParentTable();

    // Find the row and cell indices for the start and end cell.
    Point startCellPos = new Point(startCell.getParentRow().indexOf(startCell), parentTable.indexOf(startCell.getParentRow()));
    Point endCellPos = new Point(endCell.getParentRow().indexOf(endCell), parentTable.indexOf(endCell.getParentRow()));
    // Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell.
    Rectangle mergeRange = new Rectangle(Math.min(startCellPos.x, endCellPos.x), Math.min(startCellPos.y, endCellPos.y),
            Math.abs(endCellPos.x - startCellPos.x) + 1, Math.abs(endCellPos.y - startCellPos.y) + 1);

    for (Row row : parentTable.getRows())
    {
        for(Cell cell : row.getCells())
        {
            Point currentPos = new Point(row.indexOf(cell), parentTable.indexOf(row));

            // Check if the current cell is inside our merge range then merge it.
            if (mergeRange.contains(currentPos))
            {
                if (currentPos.x == mergeRange.x)
                    cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                else
                    cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

                if (currentPos.y == mergeRange.y)
                    cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                else
                    cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
            }
        }
    }
}
 
//Merging Cells between Two Cells

// Merges the range of cells between the two specified cells

// We want to merge the range of cells found inbetween these two cells.
Cell cellStartRange = table.getRows().get(2).getCells().get(2);
Cell cellEndRange = table.getRows().get(3).getCells().get(3);

// Merge all the cells between the two specified cells into one.
mergeCells(cellStartRange, cellEndRange);
 
Database Document Android (robot) app

Opinions expressed by DZone contributors are their own.

Related

  • Android Cloud Apps with Azure
  • How to Build a React Native Chat App for Android
  • Designing a Blog Application Using Document Databases
  • Dynamic Watermarking of Bitmaps in Databases

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook