Open-XML-SDK
Open-XML-SDK copied to clipboard
Wordprocessing: Extract AddImagePart() to interface
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 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
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.")
};
}
@twsouthwick, the type of request is somewhat related to what I suggested in #944.
This is available in 3.0.0, thank you.