Questions about CellFormat

Hello,

In a cell, we have the possibility to get the padding for up, bottom, left and right sides using cell.getCellFormat().getLeftPadding(), etc.
However, when trying to recreate a cell with the exact same padding I got, using the setLeftPadding() method, the result is different.
From where does the difference come ?

I provide you an example of such problem (2 files : before, after).

Still about cells, I would like to gain the style of each border of a cell. Apparently, it looks like the method cell.getCellFormat().getBorders().getLineStyle() only returns the line style of the bottom side. And then, when trying to use the .setLineStyle(int i) with the previous result obtained, it applies the style to all 4 sides of the cell. Is there any possibility to get all styles for all 4 sides of the cell ?

Finally, when I use the paragraph.getParagraphFormat.setLineSpacing() and setLineSpacingRule() to a paragraph included into a cell, it appears that the top line of the paragraph is partially hidden. Could you explain me how must I set the space between lines in a paragraph within a cell, avoiding such problems ?

Thank you in advance

Jean-Pascal Lim

Hi Jean,

Thanks for your inquiry.

Yes, you are absolutely right about getting cell padding and borders. The issue with the table in After.doc comes from the page margins of the source document. As long as you are not going to specify the page setup margins, the new document with the table is going to have the default margins. This is how you can specify Left and Right margins from the source document in your final document:

String csPath = "C:\\Temp\\";
// Open source document and a document builder for it.
Document doc = new Document(csPath + "Before.doc");
DocumentBuilder sourceBuilder = new DocumentBuilder(doc);
// Get document builder initialized for final document
Document finalDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(finalDoc);
// Set left and right margin properties from source to final document.
builder.getPageSetup().setLeftMargin(sourceBuilder.getPageSetup().getLeftMargin());
builder.getPageSetup().setRightMargin(sourceBuilder.getPageSetup().getRightMargin());

Moreover, to set the style for each side of a cell, and set text with Font and Paragraph formatting applied, please have a look at the following code snippet:

// Get first table in the document.

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
// Loop through the rows in a table.

for (Row row : table.getRows())
{

    for (Cell cell : row.getCells())
    {

        // Get Cell traits and set them in new cell.</span>
        Cell newCell = builder.insertCell();

        newCell.getCellFormat().setLeftPadding(cell.getCellFormat().getLeftPadding());
        newCell.getCellFormat().setRightPadding(cell.getCellFormat().getRightPadding());
        newCell.getCellFormat().setTopPadding(cell.getCellFormat().getTopPadding());
        newCell.getCellFormat().setBottomPadding(cell.getCellFormat().getBottomPadding());

        newCell.getCellFormat().getBorders().getTop().setLineStyle(cell.getCellFormat().getBorders().getTop().getLineStyle());
        newCell.getCellFormat().getBorders().getBottom().setLineStyle(cell.getCellFormat().getBorders().getBottom().getLineStyle());
        newCell.getCellFormat().getBorders().getLeft().setLineStyle(cell.getCellFormat().getBorders().getLeft().getLineStyle());
        newCell.getCellFormat().getBorders().getRight().setLineStyle(cell.getCellFormat().getBorders().getRight().getLineStyle());

        // Write some text with default formatting.</span>
        builder.writeln("Text in cell - line 1");

        // Set font size to 18, set font to Calibri and set </span>
        // spacing after line to 12 points, then write some text.</span>
        builder.getFont().setSize(18);
        builder.getFont().setName("Calibri");
        builder.getFont().setColor(Color.RED);
        builder.getParagraphFormat().setSpaceAfter(12);
        builder.writeln("Formatted Text");

        // Clear font and paragraph formatting.</span>
        builder.getFont().clearFormatting();
        builder.getParagraphFormat().clearFormatting();

        // Set new line with some text and formatting.</span>
        builder.getFont().setName("Arial");
        builder.getFont().setColor(Color.BLUE);
        builder.writeln("Text in cell - line 3");

        // Clear font formatting.</span>
        builder.getFont().clearFormatting();
    }
    builder.endRow();

}

// Save the document in Word format.

finalDoc.save(csPath + "output.doc");

To have further idea about the usage of DocumentBuilder class, please have a look at the DocumentBuilder documentation reference here. You can also have a quick look at the how different styles are applied here. Attached is the output document which contains the exact same page margins as in Before.doc and contains a table with same cell borders and different formatting(including font type, color, size, linespacing, etc) applied on every line inside a cell.

Do let us now how does it go on your side. Please feel free to ask if you have any other question, we will be happy to help you.