3

I use a Header in my Migradoc documents. Right now I just add a Paragraph to the Header but it doesnt really fit my needs as I want to have lets say two texts that have a different Alignment. For example:

         Middle-ALigned.Text        Right-Aligned-Text
            second line                 second line

Before I used two textframes and added them to the section (not as Header). That worked somehow but cant be the right choice as I want to print the text on every page and thats what headers are for. So the question is how do I align the two texts differently in one paragraph or how do I get two paragraphs to appear on the same height in the header.

I hope someone can help with that.

Cheers

Pax Gordon
  • 31
  • 1
  • 4

2 Answers2

3

Keep it Simple: Use a Tab Stop

The best way to do this is the same as you would do in most word processing tools: with a right-aligned tab-stop, placed on the right margin of the page, with the "left" text, being center-aligned. This is pretty straight forward, but I couldn't find the "full" solution anywhere, so here's what you need:

// Grab the current section, and other settings
var section = documentWrapper.CurrentSection;
var footer = section.Footers.Primary;
var reportMeta = documentWrapper.AdminReport.ReportMeta;

// Format, then add the report date to the footer
var footerDate = string.Format("{0:MM/dd/yyyy}", reportMeta.ReportDate);
var footerP = footer.AddParagraph(footerDate);

// Add "Page X of Y" on the next tab stop.
footerP.AddTab();
footerP.AddText("Page ");
footerP.AddPageField();
footerP.AddText(" of ");
footerP.AddNumPagesField();

// The tab stop will need to be on the right edge of the page, just inside the margin
//  We need to figure out where that is
var tabStopPosition =
    documentWrapper.CurrentPageWidth
    - section.PageSetup.LeftMargin
    - section.PageSetup.RightMargin;

// Clear all existing tab stops, and add our calculated tab stop, on the right
footerP.Format.TabStops.ClearAll();
footerP.Format.TabStops.AddTabStop(tabStopPosition, TabAlignment.Right);

The hardest part of this, is figuring out what your tab stop position should be. Because I'm boring and really like encapsulation, I dynamically calculate the tab stop position, based on the page width, less the horizontal page margins. However, getting the current page width wasn't as easy as I'd thought it'd be, because I'm using PageFormat to set the page dimensions.

Next Challenge: Getting Your Page Width, Dynamically

I really hate having tightly coupled code (think: fan-in and fan-out), so even though I know at this point in time what my page width is, even to the point of hard-coding it, I still want to hard code it in only a single place, then refer to that one place everywhere else.

I keep a custom "has-a"/wrapper class to keep this stuff encapsulated into; That's documentWrapper in my code here. Additionally, I don't expose any of the PDFSharp/MigraDoc types to the rest of my application, so I'm using ReportMeta as a way to communicate settings.

Now for some code. When I setup the section, I'm using the MigraDoc PageFormat to define the size of my page for the current section:

// The tab stop will need to be on the right edge of the page, just inside the margin
//  We need to figure out where that is
var tabStopPosition =
    documentWrapper.CurrentPageWidth
    - section.PageSetup.LeftMargin
    - section.PageSetup.RightMargin;

// Clear all existing tab stops, and add our calculated tab stop, on the right
footerP.Format.TabStops.ClearAll();
footerP.Format.TabStops.AddTabStop(tabStopPosition / 2, TabAlignment.Center);
footerP.Format.TabStops.AddTabStop(tabStopPosition, TabAlignment.Right);
footerP.Format.Alignment = ParagraphAlignment.Center;

What's really important here, is that I'm storing the CurrentPageWidth, this becomes really important when setting up our tab stops. The CurrentPageWidth property, is simply a MigraDoc Unit type. I am able to determine what this is by using MigraDoc's PageSetup.GetPageSize with my chosen PageFormat.

Results

enter image description here

Community
  • 1
  • 1
Ivan Pointer
  • 181
  • 2
  • 7
0

You can add TextFrames to the header - then they will appear on every page.

Or use one paragraph as header. Use TabStops to position the text (as in Word there are left-aligned, center-aligned, and right-aligned TabStops. With TabStops you have to deal with the linebreaks like this:
(TabStop)Middle Text(TabStop)RightText(LineBreak)(TabStop)MT Second Line(TabStop)RT Second Line.

See also:
https://stackoverflow.com/a/29250830/1015447

Community
  • 1
  • 1