We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table with picture and vertically centered text

Hi there,

I need to create a table that has a pretty big round picture in the middle cell, while left and right there is some text that needs to be vertically centered in their own cells.

So far I could create the table (by using “DocumentBuilder”), but the problem is the centering of the text to be shown at the middle of the picture on each side.

The attachment shows what I’m talking about. Imagine the red borders as invisible. They are drawn there just for the purpose of understanding.

Please help me,
Lucian.

Hi Lucian,

Thanks for your inquiry. Please use CellFormat.VerticalAlignment to set the vertical alignment of text in the cell as shown in following code example. Hope this helps you.

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
Cell cell = builder.insertCell();
builder.write("Some centered text");
builder.insertCell();
Shape shape = **new** Shape(doc, ShapeType.ELLIPSE);
shape.setHeight(150);
shape.setWidth(150);
shape.setWrapType(WrapType.INLINE);
builder.insertNode(shape);
builder.insertCell();
builder.write("Some centered text");
builder.endRow();
builder.endTable();
table.clearBorders();
doc.save(MyDir + "Out.docx");

Thank you Tahir,
it worked out perfectly!
Was mistakingly using

builder.getCellFormat().setVerticalAlignment(ParagraphAlignment.CENTER);

instead of

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

Classical Copy/Paste error.

One quick off topic question tho:
How would I insert a Shape.LINE just above the first “Some Cenrered Text”?

Thanks again!

Hi Lucian,

Thanks for your inquiry. Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi Thair,

thanks for the reply.

The attachment shows what I need (except the borders, but that’s no issue). The thing is I need it in footer. And there I cannot add a Line into the cell for whatever reason.

Greets,
Lucian.

Hi Lucian,

Thanks for sharing the detail. We are working over your query and will get back to you asap.

Thank you very much!

Hi Lucian,

Thanks for your patience. Please use the following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
Cell cell = builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(30));
Paragraph para = new Paragraph(doc);
Shape shape = new Shape(doc, ShapeType.LINE);
shape.setWidth(100);
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.LEFT_MARGIN);
shape.setHorizontalAlignment(HorizontalAlignment.LEFT);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
para.appendChild(shape);
cell.appendChild(para);
builder.moveTo(para);
builder.write("Some centered text");
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(40));
shape = new Shape(doc, ShapeType.ELLIPSE);
shape.setHeight(150);
shape.setWidth(150);
shape.setWrapType(WrapType.INLINE);
builder.insertNode(shape);
cell = builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(30));
cell.appendChild(para.deepClone(true));
builder.endRow();
builder.endTable();
table.clearBorders();
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2013);
doc.save(MyDir + "Out.docx");

Thank you very much Tahir, this helps me a lot.

One problem still remains: I cannot achieve this in a footer. I mean there, the lines are visible at the end of the footer.
Maybe this is a MS Word problem?

Greetings,
Lucian.

Hi Lucian,

Thanks for your inquiry. I have tested the scenario and have not found the shared issue. Please check the attached output document. It would be great if you please share following detail for investigation purposes.

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.