Nmaster88
Nmaster88

Reputation: 1585

Custom Headers and footers for novacode DocX

I'm trying to create a docx document that has header and footer for all pages. For the header i want the image to ocuppy all of the header. Like the image below: header that i want for doc For the footer i want it to be on the left and at the right i want to have the number of the page. Like the image below: footer i want for doc

what i have right now is:

        using (var docx = DocX.Create(filename))
        {
            docx.AddHeaders();
            docx.AddFooters();
            var headerDefault = docx.Headers.odd;
            var footerDefault = docx.Footers.odd;
            Novacode.Paragraph hp = headerDefault.InsertParagraph();
            Novacode.Paragraph fp = footerDefault.InsertParagraph();
            Novacode.Image logoHeader = docx.AddImage(System.Web.HttpContext.Current.Server.MapPath("/Images/jpg/header_pdf.jpg"));
            Novacode.Image logoFooter = docx.AddImage(System.Web.HttpContext.Current.Server.MapPath("/Images/jpg/footer_pdf.jpg"));
            hp.AppendPicture(logoHeader.CreatePicture());
            fp.AppendPicture(logoFooter.CreatePicture());

The problem is that both the header and the footer get the margins of the rest of the document and even if i do

            docx.MarginTop = 0F;
            docx.MarginRight = 0F;
            docx.MarginBottom = 0F;
            docx.MarginLeft = 0F;

there will still be a top margin on the header and a bottom margin on the footer.

Does anyone have a solution? thanks

Upvotes: 1

Views: 2330

Answers (1)

eternity
eternity

Reputation: 1728

Late answer, but maybe it would be useful for someone - I faced the same problem. The problem is here:

Novacode.Paragraph hp = headerDefault.InsertParagraph();
Novacode.Paragraph fp = footerDefault.InsertParagraph();

Header and footer already have paragraph, you should just get it:

Novacode.Paragraph hp = headerDefault.Paragraphs.First();

So the margins appeared because there were two paragraphs, one of them was empty but had new line symbol.

Upvotes: 0

Related Questions