How to align footer content in same line using MigraDoc?

167 Views Asked by At

I tried to add 3 footer contents to PDF using MigraDoc. Here the contents are not aligning in the same line.

MigraDoc.DocumentObjectModel.Paragraph paragraph = section.Footers.Primary.AddParagraph();
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph.AddText(Environment.NewLine + title);
paragraph.Format.Font.Size = 7;
paragraph.Format.Alignment = ParagraphAlignment.Left;
MigraDoc.DocumentObjectModel.Paragraph middle = section.Footers.Primary.AddParagraph();
middle.AddText(operatorName + " "+ methodToRun);
middle.Format.Font.Size = 7;
middle.Format.Alignment = ParagraphAlignment.Center;
MigraDoc.DocumentObjectModel.Paragraph created = section.Footers.Primary.AddParagraph();
created.AddText("Created: " + DateTime.Now.ToString("MMMM dd, yyyy HH:mm zzz"));
created.AddTab();
created.Format.Font.Size = 7;
created.AddText(" Page ");
created.AddPageField();
created.Format.Alignment = ParagraphAlignment.Right;

Using above code footer contents are not aligned in same line. Can anybody give a suggestion to align the footer contents in same line?

1

There are 1 best solutions below

1
I liked the old Stack Overflow On

If you want to have three elements in a single-line footer, you can achieve this by using tap stops.

Just remove all default tap stops, add a centre-aligned tab stop in the middle, a right-aligned tab stop at the right edge. Then add the contents to a single paragraph, separated by tab stops.

Sample code, assuming A4 page with default margins:

// Create the primary footer.
var footer = section.Footers.Primary;

// Add content to footer.
var paragraph = footer.AddParagraph();
paragraph.Format.TabStops.ClearAll();
paragraph.Format.AddTabStop("8cm", TabAlignment.Center);
paragraph.Format.AddTabStop("16cm", TabAlignment.Right);
paragraph.Format.Alignment = ParagraphAlignment.Left;
paragraph.AddText("Some Info (left)");
paragraph.AddTab();
paragraph.Add(new DateField() { Format = "yyyy/MM/dd HH:mm:ss" });
paragraph.AddTab();
paragraph.AddText("More Info ()right");