Free Support Forum -

Questions about CellFormat


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.

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()) {

    <span style="color:green;">// Get Cell traits and set them in new cell.</span>
    <span style="color:#2b91af;">Cell</span> newCell = builder.insertCell();
    <span style="color:green;">// Write some text with default formatting.</span>
    builder.writeln(<span style="color:#a31515;">"Text in cell - line 1"</span>);
    <span style="color:green;">// Set font size to 18, set font to Calibri and set </span>
    <span style="color:green;">// spacing after line to 12 points, then write some text.</span>
    builder.getFont().setName(<span style="color:#a31515;">"Calibri"</span>);
    builder.getFont().setColor(<span style="color:#2b91af;">Color</span>.RED);
    builder.writeln(<span style="color:#a31515;">"Formatted Text"</span>);
    <span style="color:green;">// Clear font and paragraph formatting.</span>
    <span style="color:green;">// Set new line with some text and formatting.</span>
    builder.getFont().setName(<span style="color:#a31515;">"Arial"</span>);
    builder.getFont().setColor(<span style="color:#2b91af;">Color</span>.BLUE);
    builder.writeln(<span style="color:#a31515;">"Text in cell - line 3"</span>);
    <span style="color:green;">// Clear font formatting.</span>


// Save the document in Word format. + “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.