I’m trying to insert and center pictures using the builder in a flow.
If I do this pictures are still left aligned:
foreach (var picture in pictures)
{
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.InsertImage(picturePath, width / 2, height / 2);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.InsertBreak(BreakType.ParagraphBreak);
}
Using Aspose.Words.dll dotNET 23.30 (lib\net40)
What am I missing?
@mortenma You should modify your code like this:
foreach (var picture in pictures)
{
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.InsertImage(picturePath, width / 2, height / 2);
builder.Writeln();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
Also, there are two methods to center image - using paragraph alignment in case of inline image, or by specifying shape alignment, in case of floating image. For example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Center aligned inline image.
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.InsertImage(@"C:\Temp\test.png");
builder.Writeln();
// Floating image centered vertically and horizontally.
Shape img = builder.InsertImage(@"C:\Temp\test.png");
img.WrapType = WrapType.None;
img.RelativeVerticalPosition = RelativeVerticalPosition.Page;
img.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
img.VerticalAlignment= VerticalAlignment.Center;
img.HorizontalAlignment= HorizontalAlignment.Center;
doc.Save(@"C:\Temp\out.docx");
out.docx (9.1 KB)