Formating Shape in Table Cell

I want to insert Rectangle shape in Table cell in word

I have added in cell but I want it to be vertically aligned center …

please help me how to align vertically center shape in table cell.

Please see attached “Actual.png” and “Expected.png”

Regards ,
Jeevan Joshi.

Hi Jeevan,

Thanks for your inquiry.

I think, in your case, you can simply set the vertical alignment of text in the cell by using the following code snippet:

Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.Width = 30;
shape.Height = 10;
shape.FillColor = Color.Red;
Cell cell = doc.FirstSection.Body.Tables[0].FirstRow.FirstCell;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.AppendChild(shape);

I hope, this helps.

Best regards,

Hi Awais

that not solved my problem.

I am using VB.Net here my code . please see

Dim doc As New Document("D:\DocumentBuilderDemo.doc")

Dim builder As New DocumentBuilder(doc) 
builder.MoveToDocumentEnd()
builder.InsertBreak(BreakType.SectionBreakNewPage)
builder.PageSetup.PaperSize = PaperSize.A4
builder.PageSetup.RightMargin = 10.0
Dim table As Table = builder.StartTable()
builder.RowFormat.Borders.Top.LineStyle = LineStyle.Single
builder.RowFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.InsertCell()
builder.Font.Size = GetFont().Size
builder.Font.Name = GetFont().Name
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#4f81db")
builder.Write("ABC") 
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#ffffff")
builder.Write("PQR")
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#ffffff")
builder.Write("LMN")
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#ffffff")
builder.Write("LMN")
builder.EndRow()
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center
Dim s As New Aspose.Words.Drawing.Shape(doc, Drawing.ShapeType.Rectangle) 
s.Width = 100.0
s.Height = 10.0
s.FillColor = System.Drawing.ColorTranslator.FromHtml("#4f81db")
s.VerticalAlignment = Drawing.VerticalAlignment.Center
Dim p As New System.Drawing.Point(1, 100)
s.CoordOrigin = p
s.DistanceTop = 10
Dim cel As Cell
cel = doc.FirstSection.Body.Tables(0).FirstRow.FirstCell ' on this line I get error 
doc.FirstSection.Body.Tables.count is 0
cel.FirstParagraph.AppendChild(s)
'builder.InsertNode(s) previous code 
builder.InsertCell() 
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.Write("456")
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.Write("789")
builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#ffffff")
builder.Write("LMN")
builder.EndRow()
builder.EndTable()
doc.Save("d:\abc.doc", SaveFormat.Doc)

Hi Jeevan,

Thanks for the additional information. Unfortunately, I was unable to place a ShapeType.Rectangle shape object at the middle of a Table Cell. But, could you please attach your target Word document showing the desired behavior here for testing. I will investigate as to how you are expecting your final document to be generated like. You can use Microsoft Word to create your target Word document. I will then provide you code to achieve what you’re looking for. Thanks for your cooperation.

Best regards,

Hi Awais ,

Please see attached abc.doc in that I have added blue rectangle like that I want .

height of rectangle is fixed but width not it will change dynamically in each row.

and also Height of each table row is also not fixed it will keep changing as content text of increase or decreases so padding , position not usefull.

actually I want to implement Flat-bar (of any Graph) whose value is in percent (%) which will fetched from DataBase.

Please give me any alternative to achieve this with e.g and code .

Regards ,
Jeevan Joshi.

Hi Jeevan,

Thanks for the additional information. Please find attached a couple of input/output documents. The input document contains an empty Table and the following code inserts a Rectangle Shape at the middle of first Cell.

Document doc = new Document(@"C:\Temp\in.docx");
Cell cell = doc.Sections[0].Body.Tables[0].Rows[0].FirstCell;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.ParentRow.RowFormat.HeightRule = HeightRule.Exactly;
cell.ParentRow.RowFormat.Height = 144;
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.FillColor = Color.Red;
shape.Width = 50;
shape.Height = 50;
shape.WrapType = WrapType.Inline;
cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
cell.FirstParagraph.AppendChild(shape);
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
doc.Save(@"C:\Temp\out.docx");

I hope, this helps.

PS: The rectangle Shape will remain at the middle of Cell even if you increase/decrease the Row height.

Best regards,

Hi Awais ,

Thanks for your reply .

This not solved my problem

  1. Table structure is not fixed , table will generated dynamically
  2. You have set row height , in my case it is dynamic as text increases or decreases cell height changes. and according to that Shape should vertically remain align center and horizontally left .

I have attached one sample " ABC.doc " which is generated from HTML (We generate MSword doc like this ), I want same should be achieved using Aspose.Words

Graph and Scale(which at bottom) should come as it is .

Please give me any alternative to achieve this with e.g and code .

Regards ,
Jevan Joshi

Hi Jeevan,

Thanks for the additional information. May be you can simulate centralizing the rectangle Shape in the Table Cell by using the following code snippet:

Document doc = new Document(@"C:\Temp\ABC - Copy.doc");
// This creates an enumerator which is used to "walk" the elements of a rendered document.
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Get reference to the Cell where you want to insert the Shape
Cell cell = doc.Sections[0].Body.Tables[0].Rows[5].Cells[4];
// Move to the CELL span. 
if (enumerator.MoveNode(cell.FirstParagraph))
{
    // Move to the parent ROW entity
    if (enumerator.MoveParent(LayoutEntityType.Row))
    {
        // Move to the target Cell entity
        if (enumerator.MoveLastChild())
        {
            // Get the dimensions of Cell node
            double left = enumerator.Rectangle.Left;
            double height = enumerator.Rectangle.Height;
            Shape shape = new Shape(doc, ShapeType.Rectangle);
            shape.FillColor = Color.Red;
            shape.WrapType = WrapType.None;
            shape.Width = 50;
            shape.Height = 50;
            // Adjust the (x,y) location of Shape w.r.t. First Paragarph
            shape.Left = 0;
            shape.Top = (height / 2) - (shape.Height / 2);
            shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Character;
            shape.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
            cell.FirstParagraph.AppendChild(shape);
        }
    }
}
doc.Save(@"C:\Temp\out.doc");

PS: sample documents are attached.

Best regards,

Hi Awais** ,

This havent solved my problem.

Initially also shape is not in vertically center . After generating Doc file if we add text then also it not get vertically aligned center.

Expected : I expect as I add text to one cell row height get increased , Other cell text remains aligned center , but in case of shapes it not .

please see my code

builder.InsertCell()
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center
builder.CellFormat.Width = (60 * 0.75)
Dim s As New Aspose.Words.Drawing.Shape(doc, Drawing.ShapeType.Rectangle)
s.Width = (80 * 1.55)
s.WrapType = Drawing.WrapType.Square
s.Height = 8.0
s.FillColor = System.Drawing.ColorTranslator.FromHtml("#4f81db")
builder.InsertNode(s)
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center

Is their any way to get height (initially height is not set) of Row after makeing builder.EndRow() OR builder.EndTable()

Note :- if any other work around is their to add bar-graph to cell then please give me that.

Regards ,
Jeevan Joshi

Hi Jeevan,

Thanks for the additional information. I am checking with this scenario and will get back to you soon.

Best regards,

Hi Jeevan,

Thanks for your patience.

But, in your expected document you attached to this post, if you enter content in the first cell of second Row, it won’t remain aligned vertically at the middle. Also, if you increase the Row height using Microsoft Word the rectangle Shape will not remain vertically aligned at centre (it remains fixed at it’s original position). I think, this is the expected behaviour and Aspose.Words does the right thing.

If we can help you with anything else, please feel free to ask.

Best regards,

Hi Awais ,

No it is not working exactly as I needed .

In previous post I uploaded one doc file, How which we currently exporting HTML to MSWORD (Now also I am uploading “sample.doc”)

In this file you can see in every row last cell horizontal-bar is displaying which is aligned vertically middle what ever may be row height . And you can check that by adding or removing text to any cell , horizontal-bar remains vertically middle (i.e it changes position) OR by increasing or reducing row height/cell width by dragging row/cell border you can see horizontal-bar remains vertically middle (i.e it changes position)

in short I want horizontal-bar should remain vertically middle (In what ever case)

Note : You can check above actions by doing in Sample.doc OR you can also check by creating one MSWORD document add one table of 2 or more columns add shape(Rectangle) to any column and then add text to other any cell so that it increases row height you will find that shape(Rectangle) remains vertically middle (i.e it changes position)

Regards ,
Jeevan Joshi

Hi Jeevan,

Thanks for the additional information. There are no Shape objects in your Sample.doc you attached here; the horizontal-bar you are referring to are actually grey shaded Table nodes. You can achieve the same using Aspose.Words; please see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
builder.CellFormat.Borders.Right.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Left.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.Width = (60 * 0.75);
builder.InsertCell();
// Insert Table to simulate the appearance of a rectangle
Table rectangle = builder.StartTable();
Cell cell = builder.InsertCell();
builder.EndRow();
builder.EndTable();
cell.CellFormat.ClearFormatting();
cell.CellFormat.Shading.BackgroundPatternColor = Color.Gray;
cell.ParentRow.RowFormat.ClearFormatting();
cell.ParentRow.RowFormat.Height = 8.0;
rectangle.PreferredWidth = PreferredWidth.FromPoints(80 * 1.55);
/* Continue building your main Table */
builder.Writeln("some text");
builder.EndRow();
builder.EndTable();

I hope, this helps.

Best regards,

Hi,

Well actually if you create document shape sticks to the top. Have tried everything, so my test code looks like:

[Test]
public void Test()
{
    var doc = new Document();
    var db = new DocumentBuilder(doc);
    var table = db.StartTable();
    var cell = db.InsertCell();

    cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
    cell.ParentRow.RowFormat.HeightRule = HeightRule.Exactly;
    cell.ParentRow.RowFormat.Height = 144;

    var shape = new Shape(doc, ShapeType.Rectangle)
    {
        FillColor = Color.Red,
        Width = 50,
        Height = 50,
        WrapType = WrapType.Inline
    };

    cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    cell.FirstParagraph.AppendChild(shape);
    cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

    db.EndRow();
    db.EndTable();

    doc.Save(@"C:\\out.docx");
}

And output is red square in top of cell:

http://oi46.tinypic.com/16h2f5.jpg

So is there a way to center Shape in Table Cell?

Hi Philo,

Thanks for your inquiry. As suggested here, please try inserting a floating Shape in the middle of a Cell by specifying the exact (x, y) position. I hope, this helps.

Best regards,

Hi,

Well, to calculate shape position manually is chm…
In any case it works vertically but not horizontally as row width is not know at time of insert shape. In most cases table has more than one column and column width is not know unit table is filled. So with code:

public void ShapeInsideCell()
{
    var doc = new Document();
    var db = new DocumentBuilder(doc);
    var table = db.StartTable();
    var cell = db.InsertCell();

    cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
    cell.ParentRow.RowFormat.HeightRule = HeightRule.Exactly;
    cell.ParentRow.RowFormat.Height = 144;

    var shape = new Shape(doc, ShapeType.Rectangle)
    {
        FillColor = Color.Red,
        Width = 50,
        Height = 50,
        WrapType = WrapType.None,
    };

    shape.Top = (cell.ParentRow.RowFormat.Height - shape.Height) / 2;

    cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    cell.FirstParagraph.AppendChild(shape);
    cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

    db.EndRow();
    db.EndTable();

    doc.Save(@"C:\out.docx");
}

Question how to CENTER shape inside table cell still remains open as result is still not satisfactory:

Hi Philo,

Thanks for your inquiry. I think in this case you can simply use Shape.HorizontalAlignment property to specify that the Shape shall be centred with respect to the horizontal alignment base. Please modify your code as follows:

var doc = new Document();
var db = new DocumentBuilder(doc);
var table = db.StartTable();
var cell = db.InsertCell();
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.ParentRow.RowFormat.HeightRule = HeightRule.Exactly;
cell.ParentRow.RowFormat.Height = 144;
var shape = new Shape(doc, ShapeType.Rectangle)
{
    FillColor = Color.Red,
    Width = 50,
    Height = 50,
    WrapType = WrapType.None,
    HorizontalAlignment = HorizontalAlignment.Center
};
shape.Top = (cell.ParentRow.RowFormat.Height - shape.Height) / 2;
//cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
cell.FirstParagraph.AppendChild(shape);
//cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
db.EndRow();
db.EndTable();
doc.Save(@"C:\Temp\out.docx");

I hope, this helps.

Best regards,

Thanks a lot, that worked out!