Hi,
I am having difficulty placing shapes into a table cell.It always gets inserted outside the table. The attached is what I am trying to achieve using Aspose Word (the square shape within a table cell). Any direction would help.
Thanks and Regards,
S
Hi
Thanks for your request. Could you please provide me your code, template document, current output and expected output? I will investigate the issue and provide you more information.
Best regards.
Hi Alexey,
Here’s what I have tried…
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = Color.Red;
builder.RowFormat.LeftIndent = 100;
// etc...
// Insert some table
for (int i = 0; i <5; i++)
{
for (int j = 0; j <5; j++)
{
Aspose.Words.Drawing.Shape shape = new Aspose.Words.Drawing.Shape(doc, ShapeType.Rectangle);
shape.AnchorLocked = true;
shape.Height = 20;
shape.Width = 20;
builder.InsertCell();
Aspose.Words.Paragraph par = new Aspose.Words.Paragraph(doc);
shape.AppendChild(par);
builder.MoveTo(par);
// Insert shape into the document
doc.FirstSection.Body.FirstParagraph.AppendChild(shape);
}
builder.EndRow();
}
builder.EndTable();
doc.Save("C:\\Inetpub\\wwwroot\\Report Templates\\test.doc");
I am also trying to do the same using Bookmarks but there is no method which inserts shape?
Tks,
S
Hi
Thank you for additional information. The problem occurs because you always insert the shape into the first paragraph of the document. Here is line from your code:
// Insert shape into the document
doc.FirstSection.Body.FirstParagraph.AppendChild(shape);
I think you should use code like the following:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = Color.Red;
builder.RowFormat.LeftIndent = 100;
// etc...
// Insert some table
for (int i = 0; i <5; i++)
{
for (int j = 0; j <5; j++)
{
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.AnchorLocked = true;
shape.Height = 20;
shape.Width = 20;
builder.InsertCell();
builder.InsertNode(shape);
}
builder.EndRow();
}
builder.EndTable();
doc.Save(@"Test185\out.doc");
Hope this helps.
Best regards.
Thank-you Alexey, That was very useful.
Hi Alexey,
Am having an issue with having to centre align the shapes horizontally on table cells. Attached is what I am trying to achieve and below is the code. Could you please suggest changes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table tblTemp = builder.StartTable();
builder.CellFormat.Borders.LineStyle = LineStyle.Dot;
builder.CellFormat.Borders.Color = Color.LightGray;
builder.CellFormat.TopPadding = 1.5;
builder.CellFormat.BottomPadding = 1.5;
builder.RowFormat.Height = 40;
builder.RowFormat.Alignment = RowAlignment.Center;
for (int i = 0; i <2; i++)
{
for (int j = 0; j <5; j++)
{
builder.InsertCell();
if (i == 0 && j == 0)
{
builder.Write("Leadership");
}
if (i == 1 && j == 0)
{
builder.Write("Team Fit");
}
if (i == 0 && j == 4)
{
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.FillColor = Color.SeaGreen;
shape.ZOrder = 1000;
shape.AnchorLocked = true;
shape.Height = 20;
shape.Width = 20;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Column;
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Default;
shape.VerticalAlignment = VerticalAlignment.Center;
builder.InsertNode(shape);
}
if (j == 1)
{
Shape shpLine = new Shape(doc, ShapeType.Line);
shpLine.Width = 250;
builder.InsertNode(shpLine);
}
}
builder.EndRow();
}
builder.EndTable();
foreach(Row row in tblTemp.Rows)
{
foreach(Cell cell in row.Cells)
{
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
if (cell.IsFirstCell)
{
cell.CellFormat.Width = 120.00;
cell.CellFormat.WrapText = true;
}
else
{
cell.CellFormat.Width = 67.00;
cell.CellFormat.WrapText = false;
cell.CellFormat.FitText = false;
}
}
}
doc.Save("C:\\Inetpub\\wwwroot\\Report Templates\\test.doc");
Hi
Thanks for your request. I think you can specify Top position, in this case. Please see the following code:
if (i == 0 && j == 4)
{
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.FillColor = Color.SeaGreen;
shape.ZOrder = 1000;
shape.AnchorLocked = true;
shape.Height = 20;
shape.Width = 20;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Column;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.Top = (builder.RowFormat.Height / 2) - (shape.Height / 2);
builder.InsertNode(shape);
}
if (j == 1)
{
Shape shpLine = new Shape(doc, ShapeType.Line);
shpLine.Top = builder.RowFormat.Height / 2;
shpLine.Width = 250;
builder.InsertNode(shpLine);
}
Hope this helps.
Best regards.
Perfect, Thanks a lot Alexey for the quick and accurate reply
A post was merged into an existing topic: Image overflow the table when using with shape in imageFieldMerging