Hi
In my code I am placing some shapes directly after some text and generating a Word document. But for some reason, I can’t seem to vertically center the shapes - they always look to be top aligned (I’ve tried setting the shape.Top property). It is just being created by code as such:
Dim shape = New Shape(_doc, ShapeType.Rectangle)
shape.Width = 10
shape.Height = 10
shape.WrapType = WrapType.Inline
shape.Top = 20
_docBuilder.InsertNode(shape)
Can someone help? I’ve attached what the output looks like.
Hi,
Attached is a sample template we use to populate our documents. We populate the TITLE1 with the title appropriate for our document and in the Rating1 bookmark space, we insert shapes based on data that is retrieved (its always 4 boxes/rectangles with just different fills based on the data).
In the first message above is how the Title and shapes appear, however the shapes need to vertically be centered (right now they look like they are top aligned).
Attached is a pic of how we want it to look.
Document doc = new Document(MyDir + @"ReportTemplate.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark bm = doc.Range.Bookmarks["FactorTitle1"];
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.Width = 10;
shape.Height = 10;
shape.WrapType = WrapType.None;
shape.BehindText = true;
builder.MoveTo(bm.BookmarkEnd);
builder.InsertNode(shape);
LayoutCollector c = new LayoutCollector(doc);
LayoutEnumerator e = new LayoutEnumerator(doc);
object bmPosition = c.GetEntity(bm.BookmarkEnd);
e.Current = bmPosition;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.Left = e.Rectangle.X;
shape.Top = e.Rectangle.Y - shape.Height/4;
doc.Save(MyDir + @"17.2.0.docx");
Hi,
Thanks - that worked quite well and the output is as expected!
There is another place in our document where we do the same thing - however the shapes need to be inserted into a cell in a table. But the shape seems to appear off (see attached screen shot).
We have written the code similar to this:
var doc = new Document();
var builder = new DocumentBuilder(doc);
var table = builder.StartTable();
builder.InsertCell();
builder.EndRow();
builder.EndTable();
var tableIndex = builder.CurrentSection.Body.Tables.IndexOf(table);
builder.MoveToCell(tableIndex, 0, 0, 0);
builder.Write("cell ");
var bookmarkName = Guid.NewGuid().ToString();
builder.StartBookmark(bookmarkName);
builder.EndBookmark(bookmarkName);
Bookmark bm = doc.Range.Bookmarks[bookmarkName];
builder.MoveTo(bm.BookmarkEnd);
var shapeSize = 10;
var shape = new Shape(doc, ShapeType.Rectangle)
{
Width = shapeSize,
Height = shapeSize,
WrapType = WrapType.None,
FillColor = Color.FromArgb(0, 44, 119),
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page
};
builder.InsertNode(shape);
LayoutCollector lc = new LayoutCollector(doc);
LayoutEnumerator le = new LayoutEnumerator(doc);
object bmPosition = lc.GetEntity(bm.BookmarkEnd);
le.Current = bmPosition;
shape.Left = le.Rectangle.Left;
shape.Top = le.Rectangle.Y - le.Rectangle.Height / 4;
doc.Save(“test.doc”);
Thanks!
var doc = new Document();
var builder = new DocumentBuilder(doc);
var table = builder.StartTable();
builder.InsertCell();
builder.EndRow();
builder.EndTable();
var tableIndex = builder.CurrentSection.Body.Tables.IndexOf(table);
builder.MoveToCell(tableIndex, 0, 0, 0);
builder.Write("cell ");
var bookmarkName = "bookmark";
builder.StartBookmark(bookmarkName);
builder.EndBookmark(bookmarkName);
Bookmark bm = doc.Range.Bookmarks[bookmarkName];
builder.MoveTo(bm.BookmarkEnd);
var shapeSize = 10;
var shape = new Shape(doc, ShapeType.Rectangle)
{
Width = shapeSize,
Height = shapeSize,
WrapType = WrapType.None,
FillColor = Color.FromArgb(0, 44, 119),
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
BehindText = true
};
builder.InsertNode(shape);
doc.UpdatePageLayout();
LayoutCollector lc = new LayoutCollector(doc);
LayoutEnumerator le = new LayoutEnumerator(doc);
bool isInCell = bm.BookmarkStart.GetAncestor(NodeType.Cell) != null;
if (isInCell)
{
var renderObject = lc.GetEntity(bm.BookmarkStart);
le.Current = renderObject;
RectangleF location = le.Rectangle;
le.MoveParent(LayoutEntityType.Cell);
RectangleF locationCell = le.Rectangle;
shape.Top = location.Y - locationCell.Y + location.Height / 4;
shape.Left = location.X - locationCell.X;
}
else
{
object bmPosition = lc.GetEntity(bm.BookmarkEnd);
le.Current = bmPosition;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.Left = le.Rectangle.X;
shape.Top = le.Rectangle.Y - shape.Height / 4;
}
doc.Save(MyDir + "17.2.0.docx");