1

我有一个用于文本格式的面板,我必须得到 sub- 和 superscript 工作,但不能通过将 \sub 或 \super 插入 RTF 来完成(RTF 是我最终需要的)。这就是我如何通过插入 \sub 来设置上标:

public void SetSuperscript()
{
    ITextSelection selectedText = _textbox.Document.Selection;
    if (selectedText != null)
    {
        ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
        charFormatting.Superscript= FormatEffect.On;
        selectedText.CharacterFormat = charFormatting;
    }
}

现在我知道我也可以这样做:

public void SetSuperscript()
{
    ITextSelection selectedText = _textbox.Document.Selection;
    if (selectedText != null)
    {
        ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
        charFormatting.Size /= 2;
        charFormatting.Position = charFormatting.Size;
        selectedText.CharacterFormat = charFormatting;
    }
}

它工作得一样好。问题在于创建下标时:

public void SetSubscript()
{
    ITextSelection selectedText = _textbox.Document.Selection;
    if (selectedText != null)
    {
        ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
        charFormatting.Size /= 2;
        charFormatting.Position = -charFormatting.Size;
        selectedText.CharacterFormat = charFormatting;
    }
}

上面的代码抛出异常“值不在预期范围内”。这是由这一行引起的: charFormatting.Position = -charFormatting.Size; 我在那里分配了一个负值(根据文档这没关系),因为我需要选择的文本比普通文本低一半的高度。我究竟做错了什么?

丹尼尔

4

0 回答 0