gpt4 book ai didi

c# - MigraDoc 页眉和页脚

转载 作者:太空狗 更新时间:2023-10-30 00:31:16 27 4
gpt4 key购买 nike

我正在使用 MigraDoc 创建一个 PDF,我希望第一页并且只有第一页有页脚,每个后续页面(但不是第一页)都有页眉。我试验过 DifferentFirstPageHeaderFooter 但它没有给我所需的结果。我知道该设置的某种组合,以及添加页眉和页脚的正确位置,但我不知道是什么。我的代码基于 MigraDoc 发票样本。封面是一节,然后文档的其余部分是带分页符的一节。也许我需要把它分成每页一个部分?感谢您提供任何提示。

编辑

我得到了要显示的标题,但似乎有比我正在做的更好的方法来做到这一点。页脚根本没有显示。这是我添加它们的地方:

Document document = new Document();
Section section = document.AddSection();

section.PageSetup.DifferentFirstPageHeaderFooter = true;

Paragraph paragraph = section.Footers.Primary.AddParagraph();
paragraph.AddFormattedText(ReportName, TextFormat.Bold);
paragraph.AddText("\nCreated on ");
paragraph.AddFormattedText(CreateDate, TextFormat.Bold);
paragraph.AddFormattedText("\n" + Properties.Length, TextFormat.Bold);
paragraph.AddText(" Records");
paragraph.AddFormattedText("\n" + TurnoverPercent, TextFormat.Bold);
paragraph.AddText(" Turnover Rate");
paragraph.Format.Font.Size = 10;
paragraph.Format.Alignment = ParagraphAlignment.Center;

// Later, in a different method...
Section section = document.AddSection();

// Header image
Image image = section.Headers.Primary.AddImage(filename);
image.Height = "2.5cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

image = section.Headers.FirstPage.AddImage(filename);
image.Height = "2.5cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

我尝试将页脚段落添加到 Primary 和 FirstPage,但似乎没有什么不同。 DifferentPageHeaderFooter 只适用于该部分,对吧,而不是整个文档?

最佳答案

嗯,我已经弄明白了。似乎 DifferentFirstPageHeaderFooter 不仅仅适用于您设置它的部分,而是适用于每个部分。一旦我在每个部分适本地设置它,我的两个问题都解决了,页眉和页脚出现在我想要的地方。这是更新后的代码。

Section section = document.AddSection();

section.PageSetup.DifferentFirstPageHeaderFooter = true;

Paragraph paragraph = section.Footers.FirstPage.AddParagraph();
paragraph.AddFormattedText(ReportName, TextFormat.Bold);
paragraph.AddText("\nCreated on ");
paragraph.AddFormattedText(CreateDate, TextFormat.Bold);
paragraph.AddFormattedText("\n" + Properties.Length, TextFormat.Bold);
paragraph.AddText(" Records");
paragraph.AddFormattedText("\n" + TurnoverPercent, TextFormat.Bold);
paragraph.AddText(" Turnover Rate");
paragraph.Format.Font.Size = 10;
paragraph.Format.Alignment = ParagraphAlignment.Center;

// Later, in a different method...
Section section = document.AddSection();

// Need to do this even though we've never set this field on this section
section.PageSetup.DifferentFirstPageHeaderFooter = false;

// Header image
Image image = section.Headers.Primary.AddImage(filename);
image.Height = "2.5cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

关于c# - MigraDoc 页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378828/

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