Add image to the document header by using aspose.words in .NET

Hii Team,

I am trying to add a image to the document header by using the image ID. But my method is not working. could you please help me to complete it?

private static void AddHeader(Document document, System.Func<DependentContent, Task<ContentsDto>> GetDependency)
{
    var dependentContent = new DependentContent()
    {
        Id = "fa197b43-5b2a-4c53-bddd-3dad534f0284",
    };
    var content = GetDependency(dependentContent).Result;
    if (content.Found)
    {

        DocumentBuilder builder = new DocumentBuilder(document);

        Section currentSection = builder.CurrentSection;
        PageSetup pageSetup = currentSection.PageSetup;
        pageSetup.HeaderDistance = 20;
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

        //Initialize a Header Instance

        using (var inputStream = new MemoryStream(content.Data))
        {

            var inputImageFromStream = Image.FromStream(inputStream);

            Shape shape = builder.InsertImage(inputImageFromStream);

            shape.VerticalAlignment = VerticalAlignment.Center;

        }
    }
}

@nethmi You code is correct. However, in your code you insert image into the first page header, but if PageSetup.DifferentFirstPageHeaderFooter property is set to false this header will not be displayed. So to make code work, you should either enable this property, or use primary header insted:

pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);

or

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

Hii Team,
Now it’s working. Thank you very much

1 Like