Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

Wordprocessing: Extract AddImagePart() to interface

Open m-gallesio opened this issue 3 years ago • 3 comments

I have ran into an usability problem while trying to add an image to a header in a DOCX file. We do have shared code for adding images, but it was tailored to the document body.

I have noticed that all of MainDocumentPart, HeaderPart and FooterPart have some AddImagePart(...) methods, but they are declared on the specific classes. Unless I am missing something this prevents defining a generalized method like AddImageToPart(MainDocumentPart | HeaderPart | FooterPart) without reflection.

This is not a strict necessity but I think extracting these methods to an interface would improve usability.

(This seems likely to be applicable to other parts of the library which I am not aware of / I do not use.)

m-gallesio avatar Nov 24 '21 10:11 m-gallesio

@m-gallesio I'm working on bringing the source generator to the repo so we can play around more easily with this kind of stuff. Probably a few months off, but that should enable prototyping some of this stuff

twsouthwick avatar Dec 02 '21 18:12 twsouthwick

Do not be in a hurry, this request is merely a small improvement I noticed was possible.

For the record this is a reduced version of the method I am currently using, which would be completely obsoleted by this change:

private ImagePart AddImagePart(OpenXmlPart part, ImagePartType imagePartType)
{
    return part switch
    {
        MainDocumentPart mdp => mdp.AddImagePart(imagePartType),
        HeaderPart hp => hp.AddImagePart(imagePartType),
        FooterPart fp => fp.AddImagePart(imagePartType),
        _ => throw new CustomException("Part type does not support adding image parts.")
    };
}

m-gallesio avatar Dec 03 '21 10:12 m-gallesio

@twsouthwick, the type of request is somewhat related to what I suggested in #944.

ThomasBarnekow avatar Dec 05 '21 16:12 ThomasBarnekow

This is available in 3.0.0, thank you.

m-gallesio avatar Nov 16 '23 09:11 m-gallesio