linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

Header not showing when I convert html to pdf in .net 6 using c#

Hi Team,

I am converting HTML to PDF using C#. I have applied a header and footer using the HeadersFooters property of Gembox.Document but after generating the PDF, the header is not showing.

Please find the below code:

ComponentInfo.SetLicense("FREE-LIMITED-KEY");
ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
byte[] buff = null;
var htmlLoadOptions = new HtmlLoadOptions();
using (var htmlStream = new MemoryStream(htmlLoadOptions.Encoding.GetBytes(_htmlString.ToString())))
{
    // Load input HTML text as stream.
    var document = DocumentModel.Load(htmlStream, htmlLoadOptions);
    Section section = document.Sections[0];
    PageSetup pageSetup = section.PageSetup;
    PageMargins pageMargins = pageSetup.PageMargins;
    pageSetup.Orientation = Orientation.Portrait;
    pageSetup.PaperType = PaperType.Letter;
    pageMargins.Left = 18;
    pageMargins.Right = 18;
    pageMargins.Top = 18;
    pageMargins.Bottom = 18;

    section.HeadersFooters.Add(
        new HeaderFooter(document, HeaderFooterType.HeaderDefault,
            new Paragraph(document, "Default Header")));

    section.HeadersFooters.Add(
        new HeaderFooter(document, HeaderFooterType.FooterDefault,
            new Paragraph(document,
                new Run(document, Error.TXT_LEGAL_INFORMATION + "                                                        " + "Page "),
                new Field(document, FieldType.Page),
                new Run(document, "/"),
                new Field(document, FieldType.NumPages))
           {
               ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Left },
           }));

    string path = AppDomain.CurrentDomain.BaseDirectory + "\\CurrentReport\\";
    if (!Directory.Exists(path))
    {
        DirectoryInfo di = Directory.CreateDirectory(path);
    }
    // Save output PDF file.
    document.Save(path + @"output.pdf");
}

Please let me know how to fix the issue.

Hi,

Do you perhaps have the following text in your document’s header:

This document was created using a Free version of GemBox.Document component.
To remove this message purchase Professional version.

Note that this is the limitation of the Trial mode, please read this example:

Let me know if you need anything else.

Regards,
Mario