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

Suresh A 1 Reputation point
2020-09-21T14:07:06.687+00:00

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
    2020-09-21T14:23:41.047+00:00

    Hi,

    Word development is currently not supported in the Q&A forums, the supported products are listed over here https://learn.microsoft.com/en-us/answers/products (more to be added later on).

    You can ask the experts in the dedicated Word for Developers forum over here:
    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

    ----------

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

    Best regards,
    Leon

    1 person found this answer helpful.
    0 comments No comments

  2. David 156 Reputation points
    2022-02-07T06:57:06.9+00:00

    Is this what you want?
    171754-2022-02-07-144420.png

    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  
                document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");  
      
                //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
    2020-09-22T06:42:41.593+00:00

    @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