MigraDoc headers and footers
Asked Answered
U

2

8

I'm creating a PDF with MigraDoc and I want the first page and only the first page to have a footer, and every subsequent page (but not the first page) to have a header. I've experimented with DifferentFirstPageHeaderFooter but it's not giving me the results I need. I know there's some combination of that setting, and the right place to add the headers and footers, but I don't know what. I'm basing my code on the MigraDoc invoice sample. The cover page is a section, and then the rest of the document is one section with page breaks. Maybe I need to break that into one section per page? Thanks for any tips.

EDIT

I got the header to show, but it seems like there is a better way to do it than what I'm doing. The footer is not showing up at all. Here's where I'm adding them:

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;

I tried adding the footer paragraph to Primary and FirstPage and it didn't seem to make a difference. DifferentFirstPageHeaderFooter applies only to the section, right, not the whole document?

Underbid answered 7/2, 2015 at 5:12 Comment(2)
The FirstPage header is used on the first page. In your case the Primary header is used on all other pages. If you set them both, you will have headers on all pages. Are you using version 1.50 beta or is it an older version?Iaria
Well, I've figured it out. It seems that DifferentFirstPageHeaderFooter does NOT apply only to the section you set it on, but to every section. Once I set it appropriately on each section both of my problems were solved, and the headers and footers showed up where I wanted.Underbid
U
11

Well, I've figured it out. It seems that DifferentFirstPageHeaderFooter does NOT apply only to the section you set it on, but to every section. Once I set it appropriately on each section both of my problems were solved, and the headers and footers showed up where I wanted. Here's the updated code.

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;
Underbid answered 9/2, 2015 at 17:26 Comment(2)
Properties you set for a section are inherited by all following sections - until they are overwritten.Iaria
Yeah I figured that out. Just one of many things that isn't clear from the documentation.Underbid
I
2

DifferentFirstPageHeaderFooter is what you need.

Probably your code is not correct - and yes, we want to see some code. How can we help you without seeing your code?

Many sections with one page per section would work - but that's not how MigraDoc is meant to be used.

Iaria answered 7/2, 2015 at 6:25 Comment(2)
I added some code (though my last question was answered without showing code, so it's not always necessary!). Could you explain about pages and sections, and how that's supposed to work? I find their documentation is not very thorough.Underbid
The help page tells us: "Not all questions benefit from including code. But if your problem is with code you've written, you should include some." Your code is not working as you expected.Iaria

© 2022 - 2024 — McMap. All rights reserved.