Align Page Number to Right Corner in MigraDoc

2019-06-13 18:41发布

问题:

I know how to show page numbers and how to align them in footer. However my problem is that my Footer contains some custom text which should be left aligned and page number should be aligned to right corner.

 string footer = "My custom footer";
 Paragraph footerParagraph = section.Footers.Primary.AddParagraph(footer);
 footerParagraph.AddTab();
 footerParagraph.AddPageField();

Above will generate "My custom footer 1" for page 1, I need page nmuber to be right at the right most corner of the page. I can add extra spaces or tab but thought there must be a clean way to achieve this. Thanks.

回答1:

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. 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

First, 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:

// Create, and set the new section
var section = documentWrapper.CurrentDocument.AddSection();
documentWrapper.CurrentSection = section;

// Some basic setup
section.PageSetup.PageFormat = PageFormat.Letter; // Here's my little bit of hard-coding
Unit pageWidth, pageHeight;
PageSetup.GetPageSize(PageFormat.Letter, out pageWidth, out pageHeight);

var reportMeta = documentWrapper.AdminReport.ReportMeta;
if (reportMeta.PageOrientation == AdminReportMeta.ORIENT_LANDSCAPE)
{
    section.PageSetup.Orientation = Orientation.Landscape;
    documentWrapper.CurrentPageWidth = pageHeight;
}
else
{
    section.PageSetup.Orientation = Orientation.Portrait;
    documentWrapper.CurrentPageWidth = pageWidth;
}

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.



回答2:

A single tab will do. Create a right-aligned tab at the right-most position.

You can set the tab stops for the footer style (recommended) or for the paragraph.

Code snippet modifying a style:

var style = document.Styles[StyleNames.Footer];
style.ParagraphFormat.TabStops.ClearAll();
style.ParagraphFormat.TabStops.AddTabStop(Unit.FromMillimeter(158), TabAlignment.Right);


回答3:

You could try something like this:

        Paragraph paragraph = new Paragraph();
        paragraph.Format.Alignment = ParagraphAlignment.Left;
        paragraph.AddText("My custom footer: ");
        Paragraph paragraph2 = new Paragraph();
        paragraph2.Format.Alignment = ParagraphAlignment.Right;
        paragraph2.AddText(" Page # ");
        paragraph2.AddPageField();

        section.Footers.Primary.Add(paragraph);
        section.Footers.Primary.Add(paragraph2);