Removing Specific Font Formatting

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:

  1. Style
  2. Font Name
  3. 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!

@HirenT

To remove specific font formatting from a Run node in Aspose.Words while retaining certain properties like Font Name and Font Size, you can follow a structured approach. Unfortunately, there isn’t a direct method to unset a specific style, but you can achieve your goal by selectively applying the desired properties.

Here’s a refined version of your approach that focuses on removing the style while keeping the Font Name and Font Size intact:

private void ResetFontFormatting(Run runNode, Run referenceRunNode)
{
    var targetFont = runNode.Font;
    var referenceFont = referenceRunNode.Font;

    // Clear all formatting first
    targetFont.ClearFormatting();

    // Retain Font Name and Font Size from the reference Run
    targetFont.Name = referenceFont.Name;
    targetFont.Size = referenceFont.Size;

    // Optionally, you can also retain other properties if needed
    // For example, if you want to keep the color:
    targetFont.Color = referenceFont.Color;
}

Explanation:

  1. Clear Formatting: The ClearFormatting() method is called on the target font to remove all existing formatting, including styles.
  2. Retain Specific Properties: After clearing, you can set the Font Name and Font Size directly from the reference Run node.
  3. Optional Properties: If you want to retain additional properties (like color), you can set them as needed.

This method effectively allows you to remove the style while keeping the desired font properties intact, achieving the expected output you described.

If you have any further questions or need additional assistance, feel free to ask!

@HirenT Unfortunately, there is no public API to remove specific font attribute. You can only override the attribute value. By default, if style is not applied to the Run, default paragraph font style is used. So you can use the following code:

r.Font.StyleIdentifier = StyleIdentifier.DefaultParagraphFont;

If it is required to apply formatting of the style of the parent paragraph, then you can completely clear font formatting using Font.ClearFormatting() method.