Aspose.Note使用DocumentVisitor提取OneNote文档内容

作者:控件中国网   出处:控件中国网   2016-03-17 13:43:49   阅读:5

Aspose.Note是一款专门用于处理和操作Microsoft Office OneNote文档的无图形界面类库,可以读取、写入、转换OneNote文档,也可以用于解析Microsoft Office OneNote文档,快速把文档的元素,如页数、图片、多文本、下划线、标题、表格等分割开来,控件提供的DocumentVisitor类可以用于快速提取OneNote文档内的各种内容,具体可以参考下面的Java代码:
 
public void ExtractOneNoteContent()
{
    // Open the document we want to convert.
    Document doc = new Document("MyOneNote.one", new LoadOptions());
 
    // Create an object that inherits from the DocumentVisitor class.
    MyOneNoteToTxtWriter myConverter = new MyOneNoteToTxtWriter();
 
    // This is the well known Visitor pattern. Get the model to accept a visitor.
    // The model will iterate through itself by calling the corresponding methods
    // on the visitor object (this is called visiting).
    //
    // Note that every node in the object model has the Accept method so the visiting
    // can be executed not only for the whole document, but for any node in the document.
    doc.accept(myConverter);
 
    // Once the visiting is complete, we can retrieve the result of the operation,
    // that in this example, has accumulated in the visitor.
    System.out.println(myConverter.GetText());
    System.out.println(myConverter.NodeCount());
}
 
//Additional Class
public class MyOneNoteToTxtWriter extends DocumentVisitor {
    public MyOneNoteToTxtWriter()    {
        nodecount = 0;
        mIsSkipText = false;
        mBuilder = new StringBuilder();
    }
 
    /// <summary>
    /// Gets the plain text of the document that was accumulated by the visitor.
    /// </summary>
    public String GetText()
    {
        return mBuilder.toString();
    }
 
    /// <summary>
    /// Adds text to the current output. Honors the enabled/disabled output flag.
    /// </summary>
    private void AppendText(String text)
    {
        if (!mIsSkipText)
            mBuilder.append(text);
    }
 
    /// <summary>
    /// Called when a RichText node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitRichTextStart(RichText run)
    {
        ++nodecount;
        AppendText(run.getText());
    }
 
    /// <summary>
    /// Called when a Document node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitDocumentStart(Document document)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a Page node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitPageStart(Page page)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a Title node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitTitleStart(Title title)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a Image node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitImageStart(Image image)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a OutlineGroup node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitOutlineGroupStart(OutlineGroup outlineGroup)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a Outline node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitOutlineStart(Outline outline)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Called when a OutlineElement node is encountered in the document.
    /// </summary>
    public /*override*/ void VisitOutlineElementStart(OutlineElement outlineElement)
    {
        ++nodecount;
    }
 
    /// <summary>
    /// Gets the total count of nodes by the Visitor
    /// </summary>
    public int NodeCount()
    {
        //get { return this.nodecount; }
    return this.nodecount;
    }
 
    private  StringBuilder mBuilder;
    private boolean mIsSkipText;
    private int nodecount;
 
}
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat