您的位置:首页 > 其它

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

2016-01-08 23:43 615 查看
  

       引言

   最近项目中遇到了这么一个需求就是需要我获得上传上来的word文件中的内容,但是在开始的时候自己一点思

路都没有,在之前的项目中遇到过对word操作的需求,所以初步的想法就是给word文件打上书签,然后利用书签获得

里面的内容,这样就可以获得我们想要的内容。下面就给大家分享如何获得上传上来的word文件中的书签中的内容。

并且将内容复制到另外一个word文档中。

   首先看看目录结构

                   


   下面看一下代码

   我们首先需要引入最新版的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上面就是一个简单的功能,但是没有办法啊,在最为

关键的时候还是大神点播了一下,在根据自己查询的一些资料完成了这个功能。所在代码中的注释也是一些英文的,

算是提高一下小编的英文吧。希望能对读者提供一些帮助!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: