gpt4 book ai didi

c# - Migradoc 标题、具有多个文本对齐方式的段落

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:34 25 4
gpt4 key购买 nike

我在我的 Migradoc 文档中使用了页眉。现在我只是在页眉中添加一个段落,但它并不真正符合我的需要,因为我想让我们说两个具有不同对齐方式的文本。例如:

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

在我使用两个文本框并将它们添加到该部分之前(不是作为页眉)。这在某种程度上起作用但不是正确的选择,因为我想在每一页上打印文本,这就是标题的用途。所以问题是如何在一个段落中以不同方式对齐两个文本,或者如何让两个段落出现在标题中的相同高度。

我希望有人能提供帮助。

干杯

最佳答案

保持简单:使用制表位

执行此操作的最佳方法与您在大多数文字处理工具中所做的相同:使用右对齐的制表位,放置在页面的右边缘,“左”文本居中 -对齐。这非常简单,但我无法在任何地方找到“完整”解决方案,所以这就是您需要的:

// 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”/wrapper 类来将这些东西封装到;这是我此处代码中的 documentWrapper。此外,我不会向我的应用程序的其余部分公开任何 PDFSharp/MigraDoc 类型,因此我使用 ReportMeta 作为传达设置的方式。

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

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

这里真正重要的是我正在存储 CurrentPageWidth,这在设置我们的制表位时变得非常重要。 CurrentPageWidth 属性只是一个 MigraDoc Unit 类型。我可以通过使用 MigraDoc 的 PageSetup.GetPageSize 和我选择的 PageFormat 来确定这是什么。

结果

enter image description here

关于c# - Migradoc 标题、具有多个文本对齐方式的段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com