在 MigraDoc 中将页码与右角对齐 [英] Align Page Number to Right Corner in MigraDoc
问题描述
我知道如何显示页码以及如何在页脚中对齐它们.但是我的问题是我的页脚包含一些应该左对齐的自定义文本,并且页码应该与右角对齐.
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();
上面将为第 1 页生成我的自定义页脚 1",我需要页面 nmuber 位于页面的最右侧.我可以添加额外的空格或制表符,但认为必须有一种干净的方法来实现这一点.谢谢.
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.
推荐答案
保持简单:使用制表位
执行此操作的最佳方法与您在大多数文字处理工具中所做的相同:使用右对齐的制表位,放置在页面的右边距上.这很简单,但我找不到完整的"代码.任何地方的解决方案,这就是您需要的:
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);
其中最难的部分是弄清楚您的制表位位置应该是什么.因为我很无聊而且很喜欢封装,所以我根据页面宽度动态计算制表位位置,减去水平页边距.然而,获取当前页面宽度并不像我想象的那么容易,因为我使用 PageFormat
来设置页面尺寸.
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.
首先,我真的很讨厌紧密耦合的代码(想想:扇入和扇出),所以即使我现在知道我的页面宽度是多少,即使是硬编码它,我仍然想只在一个地方硬编码它,然后在其他地方引用那个地方.
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.
我保留了一个自定义的has-a"/wrapper 类来将这些东西封装到;这是我的代码中的 documentWrapper
.此外,我不会向应用程序的其余部分公开任何 PDFSharp/MigraDoc 类型,因此我使用 ReportMeta
作为传达设置的一种方式.
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.
现在是一些代码.当我设置该部分时,我使用 MigraDoc PageFormat
来定义当前部分的页面大小:
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;
}
这里真正重要的是我正在存储 CurrentPageWidth
,这在设置我们的制表位时变得非常重要.CurrentPageWidth
属性只是 MigraDoc Unit
类型.我可以通过使用 MigraDoc 的 PageSetup.GetPageSize
和我选择的 PageFormat
来确定这是什么.
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
.
这篇关于在 MigraDoc 中将页码与右角对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!