C# 文档页眉和页脚
我正在用MigraDoc创建一个PDF,我希望第一页和只有第一页有页脚,并且每个后续页面(但不是第一页)都有页眉。我已经尝试了C# 文档页眉和页脚,c#,pdf,migradoc,C#,Pdf,Migradoc,我正在用MigraDoc创建一个PDF,我希望第一页和只有第一页有页脚,并且每个后续页面(但不是第一页)都有页眉。我已经尝试了不同的firstpageheaderfooter,但它没有给我需要的结果。我知道有一些设置的组合,以及添加页眉和页脚的正确位置,但我不知道是什么。我的代码基于发票样本。封面是一个部分,文档的其余部分是一个带有分页符的部分。也许我需要把它分成一页一节?谢谢你的提示 编辑 我得到了头球,但似乎有比我现在做的更好的方法。页脚根本没有出现。下面是我要添加它们的地方: Docume
不同的firstpageheaderfooter
,但它没有给我需要的结果。我知道有一些设置的组合,以及添加页眉和页脚的正确位置,但我不知道是什么。我的代码基于发票样本。封面是一个部分,文档的其余部分是一个带有分页符的部分。也许我需要把它分成一页一节?谢谢你的提示
编辑
我得到了头球,但似乎有比我现在做的更好的方法。页脚根本没有出现。下面是我要添加它们的地方:
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;
我试着将页脚段落添加到第一页和第一页,但似乎没有什么不同
DifferentitFirstPageHeaderFooter
仅适用于部分,对不对,而不适用于整个文档?DifferentitFirstPageHeaderFooter
是您需要的
可能您的代码不正确-是的,我们希望看到一些代码。在看不到代码的情况下,我们如何帮助您
很多章节每个章节一页都可以,但这不是MigraDoc的用途。好吧,我已经弄明白了。似乎
differentitfirstpageheaderfooter
不仅适用于设置它的部分,而且适用于每个部分。一旦我在每个部分都设置了合适的位置,我的两个问题都解决了,页眉和页脚显示在我想要的地方。这是更新后的代码
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;
我添加了一些代码(虽然我上一个问题的答案没有显示代码,所以它并不总是必要的!)。你能解释一下页面和章节,以及它们应该如何工作吗?我发现他们的文档不是很全面。帮助页面告诉我们:“并不是所有的问题都能从包含代码中受益。但是,如果您的问题是您所编写的代码,那么您应该包含一些。”您的代码没有按照您的预期工作。第一页的页眉用于第一页。在您的情况下,主标题用于所有其他页面。如果同时设置它们,则所有页面上都会有标题。你使用的是1.50版beta版还是旧版?我已经猜出来了。似乎
differentitfirstpageheaderfooter
不仅适用于设置它的部分,而且适用于每个部分。一旦我在每个部分上设置了适当的属性,我的两个问题都得到了解决,页眉和页脚显示在我想要的地方。你为一个部分设置的属性会被下面的所有部分继承,直到它们被覆盖。是的,我发现了这一点。这只是文档中许多不清楚的事情之一。