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?

nasch
  • 5,330
  • 6
  • 31
  • 52
  • 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? – I liked the old Stack Overflow Feb 07 '15 at 19:27
  • 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. – nasch Feb 09 '15 at 17:23

2 Answers2

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;
nasch
  • 5,330
  • 6
  • 31
  • 52
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.

  • 1
    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. – nasch Feb 07 '15 at 15:40
  • 1
    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. – I liked the old Stack Overflow Feb 07 '15 at 19:31