I'm using the new namespace DocumentFormat.OpenXml.Wordprocessing
to manipulate Word files. A bookmark is set manually and the program populates it with a specific content. This works as supposed to.
The challenge is to align the style of the inserted content with whatever is the parent's setting. For the sake of this question, let's assume there always is a 0th child being RunProperties
etc.
BookmarkStart mark = ...;
OpenXmlElement parent = mark.Parent!;
bool keepKilling;
do
{
OpenXmlElement? killme = mark.NextSibling();
parent.RemoveChild(killme);
keepKilling = killme is not null and not BookmarkEnd;
}
while (keepKilling);
RunProperties props = new RunProperties();
if (mark.PreviousSibling<Run>() is not null)
props = mark.PreviousSibling<Run>()
.ChildElements[0] as RunProperties;
//props = mark.PreviousSibling<Run>()
// .ChildElements[0].CloneNode(true) as RunProperties;
Run replacement = new Run(new Text("smack!"), props);
parent.ReplaceChild(replacement, mark);
I've checked the docs for RunPropertiesDefault giving me the ugly Times New Roman everywhere, RunProperties showing how to specify a custom outer XML but not how to "snatch" from the whatever-is-around, RunStyle explaning how the styles are compounded etc.
I seem not to get the "simple" style as imposed by the sourounding styles. I tried to pass nulls, default constructed values etc. Nothing helped, so I suspect I'm missing the philosphy of how it's supposed to be set.
Most of the hits are very unrelated but similarly phrased making it difficult to find relevant resources.