How to add a picture to the document header

I try to add a picture to the header of an word document (*.dotx).
If I use a quickparts with the following information {INCLUDEPICTURE {DOCPROPERTY prop_Header \* MERGEFORMAT} \* MERGEFORMAT} I get the path of the image in the docproperty. But I can’t see the image in the header.
Does anybody of you have an idea?

@Eniac2000

To add a picture to the header of a Word document using Aspose.Words, you can utilize the DocumentBuilder class, which provides a straightforward way to manipulate document elements, including headers and footers.

Here’s a step-by-step guide along with a code example to help you insert an image into the header:

  1. Create a Document: Start by creating a new document instance.
  2. Use DocumentBuilder: Initialize a DocumentBuilder object to facilitate the insertion of content.
  3. Move to Header: Use the MoveToHeaderFooter method to navigate to the header section where you want to insert the image.
  4. Insert Image: Use the InsertImage method to add the image to the header.

Here’s a sample code snippet demonstrating how to achieve this:

using Aspose.Words;
using Aspose.Words.Drawing;

class Program
{
    static void Main()
    {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Move to the primary header
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

        // Insert an image into the header
        builder.InsertImage("path/to/your/image.jpg", 
            RelativeHorizontalPosition.Page, 10, 
            RelativeVerticalPosition.Page, 10, 
            50, 50, WrapType.Through);

        // Save the document
        doc.Save("DocumentWithHeaderImage.docx");
    }
}

Explanation of the Code:

  • Document Creation: A new document is created using new Document().
  • DocumentBuilder Initialization: The DocumentBuilder is initialized to allow easy manipulation of the document.
  • Header Navigation: The MoveToHeaderFooter method is called with HeaderFooterType.HeaderPrimary to focus on the primary header.
  • Image Insertion: The InsertImage method is used to place the image at specified coordinates (10 points from the top and left margins) with a size of 50x50 points.
  • Document Saving: Finally, the document is saved to a file.

Make sure to replace "path/to/your/image.jpg" with the actual path to your image file.

This approach should allow you to successfully add an image to the header of your Word document. If you have any further questions or need additional assistance, feel free to ask!

Thank you but the path of the image should be stored in an docproperty and that should be part of a quickpart.

@Eniac2000 By default INCLUDEPICTURE fields are loaded as shapes. In your case you should explicitly specify to preserve it as a field:

LoadOptions opt = new LoadOptions();
opt.PreserveIncludePictureField = true;
Document doc = new Document(@"C:\Temp\in.docx", opt);
// Update document property, add it if required.
string propName = "test";
if (doc.CustomDocumentProperties[propName] ==null)
    doc.CustomDocumentProperties.Add(propName, "");
doc.CustomDocumentProperties[propName].Value = @"C:\Temp\test.png";
// Update fields.
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");