为有中文需求的客户提供多渠道中文技术支持.

Mon Jan 12, 2026 12:59 am

您好,
我想咨询是否可以实现如下两个功能, 非常感谢.
1.我需要将所有段落文本取消如下图中的"自动编号"功能, 相当于手动点一下取消自动编号,编号文本会保留。
b.png

2.我需要将所有段落文本取消如下图中的"自动对齐网格"功能
a.png

相关的文件在附件

kervinLi
 
Posts: 10
Joined: Wed Oct 12, 2022 3:15 am

Mon Jan 12, 2026 8:35 am

您好,

感谢您的询问。
请参考以下代码进行设置:
Code: Select all
Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(@"Test.docx");

Spire.Doc.Section section = doc.Sections[0];
foreach (Paragraph para in section.Paragraphs)
{
    //设置文档网络对齐
    para.Format.SnapToGrid = false;

    string format = para.StyleName;
    String listtext = para.ListText;
    //移除自动编号
    para.ListFormat.RemoveList();

    //获取文本样式
    TextRange origrange=(TextRange)para.ChildObjects[0];
    CharacterFormat character=origrange.CharacterFormat;
    string fontname=character.FontName;
    float fontsize=character.FontSize;
    // 从后往前处理,避免索引变化问题
    if (para.ListFormat != null)
    {
        //添加编号文本
        TextRange textRange = new TextRange(doc);
        textRange.Text = listtext;
        //设置文本样式
        textRange.CharacterFormat.FontName = fontname;
        textRange.CharacterFormat.FontSize=fontsize;
        para.ChildObjects.Insert(0, textRange);
        para.ApplyStyle(format);

    }
}
string output = @"SetSnapToGrid.docx";
doc.SaveToFile(output, Spire.Doc.FileFormat.Docx2013);

期待您的测试反馈。
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 331
Joined: Mon Apr 14, 2025 3:33 am

Fri Jan 16, 2026 11:59 pm

非常感谢您的帮忙!

代码工作的很好,实现了我需要的功能。

kervinLi
 
Posts: 10
Joined: Wed Oct 12, 2022 3:15 am

Mon Jan 19, 2026 1:21 am

您好,

感谢您的反馈。
很高兴您的问题已经解决。如果您使用我们的产品中遇到任何其他问题,请随时与我们联系。
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 331
Joined: Mon Apr 14, 2025 3:33 am

Return to 中文技术支持