为了帮助大家在进行word文档合并时灵活地控制列表编号,Aspose.Words for .NET为大家提供了ImportFormatMode属性设置来进行相应的操作。在本文中,我们会给出两个合并前的文件(目标文件和源文件),每个文件都包含一个列表,每个列表都使用了名为 MyStyle的超链接样式,具体如下图:

目标文件:

源文件:

在MyStyle的超链接样式下,用ImportFormatMode.KeepSourceFormatting 保留源文件格式的列表编号的代码如下:

C#

Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.SourceList.doc");

// Append the content of the document so it flows continuously.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
dstDoc.Save(gDataDir + "TestFile.ListKeepSourceFormatting Out.doc");

Visual Basic

Dim dstDoc As New Document(gDataDir & "TestFile.DestinationList.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.SourceList.doc")

' Append the content of the document so it flows continuously.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting)
dstDoc.Save(gDataDir & "TestFile.ListKeepSourceFormatting Out.doc")

文件合并后效果图如下:

但是,如果我们在MyStyle的超链接样式下,使用ImportFormatMode.UseDestinationStyles 沿用目标文件格式的列表编号,会出现列表自动连续编号,如下图:

如果我们既想要沿用目标文件格式的列表编号,同时又想要防止列表的连续编号的话,我们需要运行以下代码来解决以上问题:

C#

Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.SourceList.doc");

// Set the source document to continue straight after the end of the destination document.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

// Keep track of the lists that are created.
Hashtable newLists = new Hashtable();

// Iterate through all paragraphs in the document.
foreach (Paragraph para in srcDoc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem)
    {
        int listId = para.ListFormat.List.ListId;

// Check if the destination document contains a list with this ID already. If it does then this may
        // cause the two lists to run together. Create a copy of the list in the source document instead.
        if (dstDoc.Lists.GetListByListId(listId) != null)
        {
            List currentList;
            // A newly copied list already exists for this ID, retrieve the stored list and use it on 
            // the current paragraph.
            if (newLists.Contains(listId))
            {
                currentList = (List)newLists[listId];
            }
            else
            {
                // Add a copy of this list to the document and store it for later reference.
                currentList = srcDoc.Lists.AddCopy(para.ListFormat.List);
                newLists.Add(listId, currentList);
            }

// Set the list of this paragraph  to the copied list.
            para.ListFormat.List = currentList;
        }
    }
}

// Append the source document to end of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);

// Save the combined document to disk.
dstDoc.Save(gDataDir + "TestFile.ListUseDestinationStyles Out.docx");

Visual Basic

Dim dstDoc As New Document(gDataDir & "TestFile.DestinationList.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.SourceList.doc")

' Set the source document to continue straight after the end of the destination document.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

' Keep track of the lists that are created.
Dim newLists As New Hashtable()

' Iterate through all paragraphs in the document.
For Each para As Paragraph In srcDoc.GetChildNodes(NodeType.Paragraph, True)
    If para.IsListItem Then
        Dim listId As Integer = para.ListFormat.List.ListId

' Check if the destination document contains a list with this ID already. If it does then this may
        ' cause the two lists to run together. Create a copy of the list in the source document instead.
        If dstDoc.Lists.GetListByListId(listId) IsNot Nothing Then
            Dim currentList As List
            ' A newly copied list already exists for this ID, retrieve the stored list and use it on 
            ' the current paragraph.
            If newLists.Contains(listId) Then
                currentList = CType(newLists(listId), List)
            Else
                ' Add a copy of this list to the document and store it for later reference.
                currentList = srcDoc.Lists.AddCopy(para.ListFormat.List)
                newLists.Add(listId, currentList)
            End If

' Set the list of this paragraph  to the copied list.
            para.ListFormat.List = currentList
        End If
    End If
Next para

' Append the source document to end of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles)

' Save the combined document to disk.
dstDoc.Save(gDataDir & "TestFile.ListUseDestinationStyles Out.docx")

代码运行后效果图如下:

Aspose.Words在word文档合并时如何控制列表编号相关推荐

  1. npoi操作word书签_100份Word文档合并只需3分钟?不用复制粘贴,简单到一学就会...

    办公中,我们时常会需要将很多份Word文档合并在一起,如果不会便捷的技巧光靠复制粘贴,那将会花费我们很多的时间和精力.所以,今天我们就是要教大家一个非常简单的方法,只用三分钟就能将上百份文档合并在一起 ...

  2. 用Aspose.Words 从Word文档中提取表格数据

    用Aspose.Words 从Word文档中提取表格数据 对于某些项目,开发人员需要从Word文档中提取数据并导出到数据库.最大的挑战是必须支持现有Word文档. 相同格式且带多个数据块的Word文档 ...

  3. word文档合并的几种方式

    Word文档合并几种方式 通过com.spire.doc包 具体参考地址:https://www.e-iceblue.cn/spiredocforjavaoperating/merge-word-do ...

  4. java word 文档合并_Java 合并Word文档

    概述 合并文档可以是将两个包含一定逻辑关系的文档合并成一个完整的文档,也可以是出于方便文档存储.管理的目的合并多个文档为一个文档.下面,就将以上文档操作需求,通过Java程序来实现Word文档合并.合 ...

  5. 如何让多个word文档合并成一个

    几千年没发过动态了,今天随便发发证明本人健在吧! 近两天有几个同学问我Word文档合并成一个,我属实有些懵. 首先建立一个空白文档,在[插入]里面找到[对象],然后选择[从文件中提取],之后按照顺序选 ...

  6. Word文档打印时,出现“错误!未找到引用源。”

    转自百度经验 Word文档打印时,出现"错误!未找到引用源."_百度经验 https://jingyan.baidu.com/article/9113f81b1a35f82b321 ...

  7. word文档合并单元格在什么地方

    word文档合并单元格在哪里 演示机型:华为matebook X系统版本:win10APP版本:word2016 1.打开所要编辑的表格,选中所想要合并的单元格. 2.选中单元格后,单击布局功能区,在 ...

  8. C# 实现将多个word文档合并成一个word文档的功能

    https://www.cnblogs.com/zhenzaizai/p/7782748.html 前段时间项目上遇到这么一个需求,需要将多个OCR识别的word文档合并成一个,于是就在网上找了找,自 ...

  9. Aspose.words 操作 word 文档

    Aspose.words 操作 word 文档 文章目录 Aspose.words 操作 word 文档 对文档文字进行替换 对文档插入文本.表格以及删除表格 对文档文字进行替换 /*** 对word ...

最新文章

  1. 九度oj 题目1374:所有员工年龄排序
  2. Ubuntu 用户安装 MATE
  3. 数学里的π究竟牛在哪里
  4. 计算机编程pdf百度云,计算机编程基础.pdf
  5. activiti5.13 框架 数据库设计说明书
  6. 如何在不接收返回值的情况下获取返回值?
  7. Pixel 3a 开箱及 Google Fi 服务评测视频
  8. CTF中遇到不知道文件类型_道家文化中你不知道的尖端科学
  9. Windows7 品牌机OEM原版光盘镜像下载大全
  10. sqlserver的文件导入到mysql_导入50G文件到mysql,然后再倒入sqlserver
  11. 人民搜索副总经理宫玉国离职
  12. 使用u盘如何装linux系统教程视频教程,如何使用u盘安装linux系统
  13. python 华泰股票交易接口_TradeApi 自带资金管理的A股程序化交易接口
  14. 【题解】【AcWing】1564. 哈希
  15. Django中引入bootstrap的方法
  16. 【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
  17. CSS之毗邻选择器与普通兄弟选择器的区别
  18. hive的窗口函数详解
  19. LindedList相关介绍
  20. CTF MISC图片隐写简单题学习思路总结(持续更新)

热门文章

  1. hbase数据库的一些基本操作(持续更新中)
  2. Linux环境下使用JFS文件系统介绍
  3. 前端web:响应式Web开发优缺点总结
  4. 一条命令在 Ubuntu 中安装所有基本的媒体编解码器
  5. 正交变换——来龙去脉
  6. 个人博客如何选择虚拟主机
  7. 刷车机器人_各位车主注意了!这样的洗车很伤爱车!99%的人都不知道!
  8. web开发框架_Web开发的最佳PHP框架
  9. 在群晖NAS上搭建导航页_通过Web Station搭建
  10. 基础篇(二).ARMv8寄存器(2)