在MigraDoc中将页码对齐到右角

我知道如何显示页码以及如何在页脚中对齐它们。 但是我的问题是我的页脚包含一些自定义文本,应该左对齐,页码应该对齐到右上角。

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

上面将为第1页生成“我的自定义页脚1”,我需要将页面nmuber放在页面的最右边。 我可以添加额外的空格或标签,但认为必须有一个干净的方法来实现这一点。 谢谢。

单个标签可以。 在最右侧位置创建右对齐选项卡。

您可以为页脚样式(推荐)或段落设置制表位。

修改样式的代码段:

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

保持简单:使用制表位

执行此操作的最佳方法与在大多数文字处理工具中执行的操作相同:使用右对齐制表符,位于页面的右边缘。 这很简单,但我找不到任何地方的“完整”解决方案,所以这就是你需要的:

 // 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来设置页面尺寸。

下一个挑战:动态获取页面宽度

首先,我真的很讨厌紧密耦合的代码(想想:扇入和扇出) ,所以即使我知道我的页面宽度是多少,即使是硬编码,我仍然想要只在一个地方硬编码,然后在其他地方引用那个地方。

我保留了一个自定义的“has-a”/包装类来保持这些内容的封装; 这是我的代码中的documentWrapper 。 另外,我没有将任何PDFSharp / MigraDoc类型暴露给我的应用程序的其余部分,因此我使用ReportMeta作为一种通信设置的方式。

现在为一些代码。 当我设置该部分时,我正在使用MigraDoc PageFormat来定义当前部分的页面大小:

 // 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来确定这是什么。

你可以尝试这样的事情:

  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);