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,
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);
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”)
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-IN</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:DoNotOptimizeForBrowser/>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–>
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 codebuilder.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()
/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}<![endif]–> builder.EndTable()
doc.Save(“d:\abc.doc”, SaveFormat.Doc)
Hi Jeevan,
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,
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");
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 Apose.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,
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");
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,
Hi Jeevan,
Hi Awais ,
Hi Jeevan,
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();
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");
}
Hi Philo,
Hi,
Hi Philo,
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");
Thanks a lot, that worked out!