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

Mon Jan 01, 2024 8:59 pm

例如我要把章节2 段落33 34 35 36 37 38的内容 移动到章节3 段落88的位置 如何实现?

jiao130456
 
Posts: 8
Joined: Mon Oct 09, 2023 9:29 am

Tue Jan 02, 2024 6:49 am

您好,

感谢您的询问。
我把完整的代码放在下面供您参考:
Code: Select all
// 创建一个新的文档对象
Document doc = new Document();

// 从文件中加载文档
doc.LoadFromFile("test.docx");

// 获取章节2
Spire.Doc.Section section2 = doc.Sections[1];

// 获取章节3
Spire.Doc.Section section3 = doc.Sections[2];

// 创建一个用于存储要移动的段落的列表
List<Paragraph> paragraphsToMove = new List<Paragraph>();

// 遍历章节2的第33到第38个段落,并将它们添加到要移动的段落列表中
for (int i = 32; i <= 37; i++)
{
    Paragraph paragraph = section2.Paragraphs[i];
    paragraphsToMove.Add(paragraph);
}

// 如果章节3的段落数量大于等于88
if (section3.Paragraphs.Count >= 88)
{
    // 设置目标索引为87
    int targetIndex = 87;

    // 遍历要移动的段落列表
    foreach (Paragraph paragraph in paragraphsToMove)
    {
        // 将段落插入到章节3的目标索引位置
        section3.Paragraphs.Insert(targetIndex, paragraph);

        // 更新目标索引
        targetIndex++;
    }
}

// 遍历章节2的第37到第33个段落
for (int j = 37; j >= 33; j--)
{
    // 如果章节2的段落数量大于当前索引
    if (section2.Paragraphs.Count > j)
    {
        // 从章节2中移除当前索引处的段落
        section2.Paragraphs.RemoveAt(j);
    }
}
// 将修改后的文档保存为output.docx文件格式
doc.SaveToFile("output.docx", FileFormat.Docx);

// 关闭文档对象
doc.Close();

如果问题仍然存在,请提供您的word文档以帮助我们进行进一步调查,您可以在此处附上或通过电子邮件发送给我们([email protected])。提前感谢。

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to 中文技术支持