Apose.word控件获取书签中的内容并复制到一个新的word文档中

简介: Apose.word控件获取书签中的内容并复制到一个新的word文档中

引言


最近项目中遇到了这么一个需求就是需要我获得上传上来的word文件中的内容,但是在开始的时候自己一点思路都没有,在之前的项目中遇到过对word操作的需求,所以初步的想法就是给word文件打上书签,然后利用书签获得里面的内容,这样就可以获得我们想要的内容。下面就给大家分享如何获得上传上来的word文件中的书签中的内容。并且将内容复制到另外一个word文档中。


首先看看目录结构


20160108231342102.gif



下面看一下代码


我们首先需要引入最新版的Apose.word.dll这个文件,下面的代码才可以正常运行。

namespace CopyBookmarkedText
{
    /// <summary>
    /// Shows how to copy bookmarked text from one document to another while preserving all content and formatting.
    /// 
    /// Does not cover all cases possible (bookmark can start/end in various places of a document
    /// making copying scenario more complex).
    /// 
    /// Supported scenarios at the moment are:
    /// 
    /// 1. Bookmark start and end are in the same section of the document, but in different paragraphs. 
    /// Complete paragraphs are copied.
    /// 
    /// </summary>
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main(string[] args)
        {
            string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;
            string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath;
            // Load the source document.
            Document srcDoc = new Document(dataDir + "Template.doc");
            // This is the bookmark whose content we want to copy.
            Bookmark srcBookmark = srcDoc.Range.Bookmarks["ntf010145060"];
            // We will be adding to this document.
            Document dstDoc = new Document();
            // Let's say we will be appending to the end of the body of the last section.
            CompositeNode dstNode = dstDoc.LastSection.Body;
            // It is a good idea to use this import context object because multiple nodes are being imported.
            // If you import multiple times without a single context, it will result in many styles created.
            NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);
            // Do it once.
            AppendBookmarkedText(importer, srcBookmark, dstNode);
            // Do it one more time for fun.
            AppendBookmarkedText(importer, srcBookmark, dstNode);
            // Save the finished document.
            dstDoc.Save(dataDir + "Template Out.doc");
        }
        /// <summary>
        /// Copies content of the bookmark and adds it to the end of the specified node.
        /// The destination node can be in a different document.
        /// </summary>
        /// <param name="importer">Maintains the import context </param>
        /// <param name="srcBookmark">The input bookmark</param>
        /// <param name="dstNode">Must be a node that can contain paragraphs (such as a Story).</param>
        private static void AppendBookmarkedText(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)
        {
            // This is the paragraph that contains the beginning of the bookmark.
            Paragraph startPara = (Paragraph)srcBookmark.BookmarkStart.ParentNode;
            // This is the paragraph that contains the end of the bookmark.
            Paragraph endPara = (Paragraph)srcBookmark.BookmarkEnd.ParentNode;
            if ((startPara == null) || (endPara == null))
                throw new InvalidOperationException("Parent of the bookmark start or end is not a paragraph, cannot handle this scenario yet.");
            // Limit ourselves to a reasonably simple scenario.
            if (startPara.ParentNode != endPara.ParentNode)
                throw new InvalidOperationException("Start and end paragraphs have different parents, cannot handle this scenario yet.");
            // We want to copy all paragraphs from the start paragraph up to (and including) the end paragraph,
            // therefore the node at which we stop is one after the end paragraph.
            Node endNode = endPara.NextSibling;
            // This is the loop to go through all paragraph-level nodes in the bookmark.
            for (Node curNode = startPara; curNode != endNode; curNode = curNode.NextSibling)
            {
                // This creates a copy of the current node and imports it (makes it valid) in the context
                // of the destination document. Importing means adjusting styles and list identifiers correctly.
                Node newNode = importer.ImportNode(curNode, true);
                // Now we simply append the new node to the destination.
                dstNode.AppendChild(newNode);
            }
        }
    }
}


上面的程序就完成了这个功能,这个东西实属不易啊,为什么这么说呢?关于Apose.word的资料都事英文的,对于小编这种英语盲,您说怎么办呢?没有办法啊,其实在API上面就是一个简单的功能,但是没有办法啊,在最为关键的时候还是大神点播了一下,在根据自己查询的一些资料完成了这个功能。所在代码中的注释也是一些英文的,算是提高一下小编的英文吧。希望能对读者提供一些帮助!!!

目录
相关文章
|
API 数据处理 数据安全/隐私保护
curl基础用法
curl基础用法
|
消息中间件 SQL JSON
FlinkSQL 实时采集Kafka内容到MySQL(实战记录)
最近在做实时采集Kafka发布的内容到MySQL,本文记录一下关键的点,细节不再描述,希望能帮助到大家。
1489 0
FlinkSQL 实时采集Kafka内容到MySQL(实战记录)
|
缓存 Linux Windows
Linux环境aspose插件word转pdf中文乱码解决方案
Linux环境aspose插件word转pdf中文乱码解决方案
3319 0
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课14 纯享单机版
PolarDB不仅支持基于“共享存储+多计算节点”的集群版,还提供类似开源PostgreSQL的单机版。单机版部署简单,适合大多数应用场景,并可直接使用PostgreSQL生态插件。通过Docker容器、Git克隆代码、编译软件等步骤,即可完成PolarDB单机版的安装与配置。具体操作包括启动容器、进入容器、克隆代码、编译软件、初始化实例、配置参数及启动数据库。此外,还有多个相关教程和视频链接供参考,帮助用户更好地理解和使用PolarDB单机版。
1479 1
|
前端开发 C#
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
|
9月前
|
人工智能 Linux API
OpenClaw Skill 标准开发模板:10个实用Skill源码+阿里云/本地部署与大模型API配置教程
OpenClaw作为轻量化AI智能体平台,其核心扩展能力来自自定义Skill开发,对于新手而言,掌握标准化Skill结构、可直接运行的实用源码、系统的调试避坑方法,就能快速开发出满足文件操作、办公自动化、开发辅助等场景的专属技能。本文提供统一通用的OpenClaw Skill标准模板,包含10个可直接复制使用的新手Skill源码,覆盖文件处理、日常办公、开发辅助三大高频场景,同时完整补充2026年阿里云服务器部署、MacOS/Linux/Windows11本地部署流程,以及阿里云千问大模型API与免费Coding Plan API的配置方法,搭配全场景常见问题解决方案,让零基础用户也能稳定开发
1202 1
|
11月前
|
Ubuntu Java Linux
ARM Linux 环境 国产 华为 欧拉 openEuler 20.03 操作系统安装 Polardb-X 数据库 单机版 rpm 包 教程
本文介绍在ARM64架构的openEuler 20.03系统上,通过RPM包安装部署PolarDB-X单节点数据库的完整过程,涵盖环境准备、RPM安装、用户配置、数据库初始化及启动连接等步骤,助力用户在国产化软硬件平台上快速搭建PolarDB-X运行环境。
1782 2
|
11月前
|
Java Linux 数据库连接
PolarDB-X 集中式三节点高可用集群部署 & Java 场景 CRUD 应用
本文介绍在CentOS 7.9、openEuler 20.03及银河麒麟V10上部署PolarDB-X三节点高可用集群的完整过程,涵盖环境准备、配置文件设置、集群初始化与启动,并通过Java应用实现CRUD操作验证。集群支持自动主备切换,确保服务高可用,适用于生产环境数据库架构搭建与学习参考。
1088 0
|
机器学习/深度学习 算法 调度
【column-and-constraint generation method[CCG]】两阶段鲁棒优化(Python代码实现)
【column-and-constraint generation method[CCG]】两阶段鲁棒优化(Python代码实现)
592 0
|
弹性计算 运维 关系型数据库
用 Patroni 搭建 PolarDB-PG 高可用集群
本文详细介绍了如何利用开源PolarDB-PG和Patroni搭建高可用集群。实验环境使用了三台ECS,内核版本为PolarDB-PG 15,Patroni版本为4.0.3,etcd版本为3.5.0。文章依次讲解了ETCD的安装与配置、PolarDB-PG 15的安装与初始化,以及Patroni的配置和启动过程。通过Patroni自动创建备库,实现高可用集群的搭建。最后总结指出,用户可根据需求调整配置,或选择线上PolarDB-PG产品以减少运维成本并提升稳定性。