Can we place image along with aligned text in header in word doc using openxml in C#

Suresh A 1 Reputation point

I have created a word doc using open xml in C#. In that word doc header we need to place one image in align right with specific dimensions i.e. (new DW.Extent() { Cx = 1000000L, Cy = 220000L }) and some text in middle. I have created a header only with the image and created another header for the text and appended the both as single header. while making both headers as single header it is consuming extra space in between the headers and image is going to top and image below text is coming.

If it is possible to arrange in single header for both image and text because we need to arrange the text and image in same row. If it is not possible then is there any header length decreasing properties in open xml c#.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
34,991 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Leon Laude 85,566 Reputation points


    Word development is currently not supported in the Q&A forums, the supported products are listed over here (more to be added later on).

    You can ask the experts in the dedicated Word for Developers forum over here:


    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,

    1 person found this answer helpful.
    0 comments No comments

  2. David 156 Reputation points

    Is this what you want?

    The above effect can be achieved by using Free Spire.Doc, which is a free C# class library for Word. Install it via NuGet, and you can add image along with aligned text to the header of an existing Word document using the code snippet below.

    using Spire.Doc;  
    using Spire.Doc.Documents;  
    using Spire.Doc.Fields;  
    using System.Drawing;  
    namespace AddHeaderImageAndText  
        class Program  
            static void Main(string[] args)  
                //Create an instance of Document class  
                Document document = new Document();  
                //Load a Word document  
                //Get the first section of Word Document  
                Section section = document.Sections[0];  
                //Get header via HeadersFooters.Header property   
                HeaderFooter header = section.HeadersFooters.Header;  
                //Add a paragraph and set paragraph alignment style  
                Paragraph textPara = header.AddParagraph();  
                textPara.Format.HorizontalAlignment = HorizontalAlignment.Center;  
                textPara.Format.AfterSpacing = 0;  
                //Append text and set font name, size, color,etc.  
                TextRange textrange = textPara.AppendText("Your Company Name" + "\nYour Website"+"\nYour Telephone Number");  
                textrange.CharacterFormat.FontName = "Arial";  
                textrange.CharacterFormat.FontSize = 10;  
                textrange.CharacterFormat.TextColor = Color.DodgerBlue;  
                //Add another paragraph to hold image  
                Paragraph imagePara = header.AddParagraph();  
                DocPicture headerImage = imagePara.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\R-C.jpg"));  
                //Set image position  
                headerImage.TextWrappingStyle = TextWrappingStyle.InFrontOfText;  
                headerImage.HorizontalPosition = 422;  
                headerImage.VerticalOrigin = VerticalOrigin.TopMarginArea;  
                headerImage.VerticalPosition = 0;  
                //Save to file  
                document.SaveToFile("AddHeader.docx", FileFormat.Docx);  
    1 person found this answer helpful.
    0 comments No comments

  3. Emily Hua-MSFT 27,461 Reputation points

    @Suresh A ,

    Please refer to Leon's reply.

    As the issue is not supported on Q&A forum, I would modify the tag to be "not-supported".

    Thanks for your understanding.

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments