编程语言
首页 > 编程语言> > c# – MigraDoc页眉和页脚

c# – MigraDoc页眉和页脚

作者:互联网

我正在用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,它似乎没有什么区别. DifferentFirstPageHeaderFooter仅适用于部分,权限,而不是整个文档?

解决方法:

好吧,我已经弄清楚了.似乎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,pdf,migradoc
来源: https://codeday.me/bug/20190612/1223469.html