Ashutosh Ashutosh - 6 months ago 52
C# Question

Issue Aligning Text with the Image Using MigraDoc

I am having header in this format --

"Heading" "Image" "Title"

Below is the code snippet i am using to achieve this -

Paragraph header = section.Headers.Primary.AddParagraph("Heading");
header.Format.Font.Bold = true;
Image image = header.AddImage("../../Images/logo.png");
image.Height = Unit.FromMillimeter(6);
header.AddFormattedText("Title", TextFormat.NotBold);

I need to align my "Image" and "Title" in such a way that title is vertically centrally aligned with respect to image's height, how can i achieve this ?

Any pointers/code snippet is much appreciated.


You could use a table to fit all the information in a certain structure:

// create document
Document MigraDokument = new Document();

// create section. 
Section section = MigraDokument.AddSection();            
section.PageSetup.PageFormat = PageFormat.A4;

// create a table
Table t = section.AddTable();
// size to use for the image and the image cell in the table
int size = 6;

// create 3 columns
Column column_header = t.AddColumn("6cm");
column_header.Format.Alignment = ParagraphAlignment.Center;

Column column_image = t.AddColumn(Unit.FromMillimeter(size));
column_image.Format.Alignment = ParagraphAlignment.Center;

Column column_text = t.AddColumn("4cm");
column_text.Format.Alignment = ParagraphAlignment.Center;

// Add 1 row to fill it with the content
Row r = t.AddRow();

// add you Header
Paragraph header = r.Cells[0].AddParagraph("Heading");
header.Format.Font.Bold = true;

// add the image            
Image image = r.Cells[1].AddImage("../../logo.png"); 
image.Height = Unit.FromMillimeter(size);

// Add your Title

// allign all of them
r.Cells[0].VerticalAlignment = VerticalAlignment.Center;
r.Cells[1].VerticalAlignment = VerticalAlignment.Center;
r.Cells[2].VerticalAlignment = VerticalAlignment.Center;

In my document the result looks the following:

enter image description here