Subject: Query on Removing Specific Font Formatting in Aspose.Words
Hello,
I would like to know if Aspose.Words offers a way to remove specific font formatting from the inline formatting applied to a Run node.
For example, in the existing inline formatting, the following properties are specified:
- Style
- Font Name
- Font Size
My goal is to keep the Font Name and Font Size while removing the Style. I haven’t found a mechanism to remove or unset specific font formatting. Instead, it appears that I can only overwrite the specific formatting with some default value, but we do not want to apply any default formatting either.
Existing formatting:
<w:rPr>
<w:rStyle w:val="PlaceholderText"/>
<w:rFonts w:ascii="Aptos" w:hAnsi="Aptos" w:cs="ADLaM Display"/>
<w:sz w:val="36"/>
<w:szCs w:val="36"/>
</w:rPr>
The expectation here is that after transformation, my OpenXML for specific inline properties should look as below:
<w:rPr>
<w:rFonts w:ascii="Aptos" w:hAnsi="Aptos" w:cs="ADLaM Display"/>
<w:sz w:val="36"/>
<w:szCs w:val="36"/>
</w:rPr>
I attempted to achieve this using the following logic. Could you please share your thoughts on the given approach?
private void ResetFontFormatting(Run runNode, Run referenceRunNode)
{
var targetFont = runNode.Font;
bool retainStyle = true;
bool retainColor = true;
if (targetFont.StyleIdentifier == StyleIdentifier.PlaceholderText)
{
retainStyle = false;
}
if (targetFont.Color.Name.Equals(GREY_COLOR, StringComparison.OrdinalIgnoreCase))
{
retainColor = false;
}
if(retainStyle && retainColor)
{
return;
}
var referenceFont = referenceRunNode.Font;
targetFont.ClearFormatting();
if(retainColor)
{
targetFont.Color = referenceFont.Color;
}
if(retainStyle)
{
targetFont.Style = referenceFont.Style;
targetFont.StyleIdentifier = referenceFont.StyleIdentifier;
}
// Assign all font properties from referenceFont to targetFont if they differ
if (targetFont.Bold != referenceFont.Bold) targetFont.Bold = referenceFont.Bold;
if (targetFont.Italic != referenceFont.Italic) targetFont.Italic = referenceFont.Italic;
if (targetFont.Underline != referenceFont.Underline) targetFont.Underline = referenceFont.Underline;
if (targetFont.StrikeThrough != referenceFont.StrikeThrough) targetFont.StrikeThrough = referenceFont.StrikeThrough;
if (targetFont.DoubleStrikeThrough != referenceFont.DoubleStrikeThrough) targetFont.DoubleStrikeThrough = referenceFont.DoubleStrikeThrough;
if (targetFont.Engrave != referenceFont.Engrave) targetFont.Engrave = referenceFont.Engrave;
if (targetFont.Emboss != referenceFont.Emboss) targetFont.Emboss = referenceFont.Emboss;
if (targetFont.Hidden != referenceFont.Hidden) targetFont.Hidden = referenceFont.Hidden;
if (targetFont.Size != referenceFont.Size) targetFont.Size = referenceFont.Size;
if (!targetFont.Name.Equals(referenceFont.Name)) targetFont.Name = referenceFont.Name;
if (!targetFont.NameAscii.Equals(referenceFont.NameAscii)) targetFont.NameAscii = referenceFont.NameAscii;
if (!targetFont.NameBi.Equals(referenceFont.NameBi)) targetFont.NameBi = referenceFont.NameBi;
if (!targetFont.NameFarEast.Equals(referenceFont.NameFarEast)) targetFont.NameFarEast = referenceFont.NameFarEast;
if (!targetFont.NameOther.Equals(referenceFont.NameOther)) targetFont.NameOther = referenceFont.NameOther;
if (targetFont.Spacing != referenceFont.Spacing) targetFont.Spacing = referenceFont.Spacing;
if (targetFont.Scaling != referenceFont.Scaling) targetFont.Scaling = referenceFont.Scaling;
if (targetFont.Position != referenceFont.Position) targetFont.Position = referenceFont.Position;
if (targetFont.Kerning != referenceFont.Kerning) targetFont.Kerning = referenceFont.Kerning;
if (!targetFont.HighlightColor.Equals(referenceFont.HighlightColor)) targetFont.HighlightColor = referenceFont.HighlightColor;
if (targetFont.Subscript != referenceFont.Subscript) targetFont.Subscript = referenceFont.Subscript;
if (targetFont.Superscript != referenceFont.Superscript) targetFont.Superscript = referenceFont.Superscript;
if (targetFont.TextEffect != referenceFont.TextEffect) targetFont.TextEffect = referenceFont.TextEffect;
if (targetFont.Bidi != referenceFont.Bidi) targetFont.Bidi = referenceFont.Bidi;
if (targetFont.LocaleId != referenceFont.LocaleId) targetFont.LocaleId = referenceFont.LocaleId;
if (targetFont.LocaleIdBi != referenceFont.LocaleIdBi) targetFont.LocaleIdBi = referenceFont.LocaleIdBi;
if (targetFont.LocaleIdFarEast != referenceFont.LocaleIdFarEast) targetFont.LocaleIdFarEast = referenceFont.LocaleIdFarEast;
if (targetFont.NoProofing != referenceFont.NoProofing) targetFont.NoProofing = referenceFont.NoProofing;
if (targetFont.Shadow != referenceFont.Shadow) targetFont.Shadow = referenceFont.Shadow;
if (targetFont.Outline != referenceFont.Outline) targetFont.Outline = referenceFont.Outline;
if (targetFont.SmallCaps != referenceFont.SmallCaps) targetFont.SmallCaps = referenceFont.SmallCaps;
if (targetFont.AllCaps != referenceFont.AllCaps) targetFont.AllCaps = referenceFont.AllCaps;
if (targetFont.ComplexScript != referenceFont.ComplexScript) targetFont.ComplexScript = referenceFont.ComplexScript;
}
Thank you!