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上面就是一个简单的功能,但是没有办法啊,在最为关键的时候还是大神点播了一下,在根据自己查询的一些资料完成了这个功能。所在代码中的注释也是一些英文的,算是提高一下小编的英文吧。希望能对读者提供一些帮助!!!

目录
相关文章
|
6月前
docx设置保存的word文档字体及大小
一般我是要将文件保存为这种形式我会使用
104 0
【Word】利用域代码快速实现基于书签的交叉引用
【Word】利用域代码快速实现基于书签的交叉引用
394 0
|
Web App开发 XML Java
通过freemarker生成一个word,解决生成的word用wps打开有问题的问题,解决出word时中文文件名乱码问题,解决打开出word时打开的word出现问题的问题,出图片,解决动态列表
 通过freemarker制作word比较简单 步骤:制作word模板。制作方式是:将模板word保存成为xml----在xml的word模板中添加相应的标记----将xml的word文件的后缀名改成ftl文件(要注意的是生成xml格式要是2003格式的xml,也就是说拿到的word模板得是2003格式的,否则用wps打开word将会出现问题)   详细步骤如下: 模板制作(将要动态显示的
4623 0
|
C#
C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签
概要 书签的设置可以帮助我们快速的定位某段文字,使用起来很方便,也很节省时间。在前一篇文章“C# 如何添加/删除Word书签”中介绍了插入、删除书签的方法,本篇文章将对C# 操作Word书签的功能做进一步介绍。
1920 0
|
C#
C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
序 在前面的文章C# 如何插入、修改、删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中继续介绍操作Word批注的方法。分以下三种情况来介绍: 1. 插入图片到Word批注 2. 读取Word批注 3.
1497 0