Imran Rafique March 4, 2014one Comment

Read, Convert, Extract and Manipulate Microsoft OneNote Files using Aspose.Note for .NET 1.0.0

Read, Convert, Extract and Manipulate Microsoft OneNote Files using Aspose.Note for .NET 1.0.0

March 4, 2014
Share on FacebookTweet about this on TwitterShare on LinkedIn

We are excited to announce our new API: Aspose.Note for .NET 1.0.0. It interacts with the Microsoft Office OneNote (.one) file format. Aspose.Note is a class library for working with Microsoft OneNote files. It allows developers to work with ONE (2010/ 2013) files in ASP.NET web applications, web services and Windows applications. The Aspose.Note API brings with it some comprehensive features.

In the first version, we have included features like converting .one files to different image formats, exporting to PDF format, reading and extracting text from .one file, getting page information, getting image information, extracting images, and searching and replacing text. The following samples give you an idea how to get started with Aspose.Note for .NET.

Convert OneNote Files

  1. Save a OneNote (.one) file as an image (.PNG, .BMP, .JPEG, .GIF) file – The Aspose.Note API allows developers to save one notebook pages to image files so making a thumbnail is easy. Below is the code for saving a notebook as an image:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Save the document as GIF.
    oneFile.Save("FullFileName.gif", SaveFormat.Gif);
    
    OR
    
    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Initialize ImageSaveOptions object 
    ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png);
    // Set page index
    opts.PageIndex = 1;
    // Save the document as PNG.
    oneFile.Save(@"FullFileName.png", opts);

    Please see the following topic for further help: Converting OneNote to Image.

  2. Export OneNote (.one) file pages to PDF – If you have a Microsoft OneNote file that you want to share with others, but do not want them to be able to change the format, you can save the file as a PDF. The code below shows how to save all or specific pages from a OneNote file as a PDF file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Save the document as PDF
    oneFile.Save(@"FullFileName.pdf", new PdfSaveOptions());
    
    OR
    
    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Initialize PdfSaveOptions object
    PdfSaveOptions opts = new PdfSaveOptions();
    // Set page index
    opts.PageIndex = 2;
    // Set page index
    opts.PageCount = 3;
    // Save the document as PDF
    oneFile.Save(@"FullFileName.pdf", opts);
    

    Please see the following topic for further help: Converting OneNote Document to PDF.

Read OneNote Files

  1. Show all text from a OneNote file – Sometimes developers need to grab some text from of a OneNote file.  The following code extracts  and shows all the textual content from a OneNote file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Get textual contents
    string text = oneFile.GetText();
    // Display on the output screen
    Console.WriteLine(text);

    Please see the following topic for further help: Extract Text from OneNote Document.

  2. Get the number of pages from a OneNote document – A OneNote document may have multiple pages: you can display the page count. The count includes pages from all levels. The following code gets the page count from a OneNote file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Get pages count
    int count = oneFile.GetChildNodes(NodeType.Page).Count;

    Please see the following topic for further help: Get Number of Pages from a OneNote Document.

  3. Get all pages info from a OneNote document – Aspose.Note API allows developers, access details about each page like page title, author, page level, page size and create time etc. Following is the code to get details about each page from the OneNote file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // get collection of page nodes
    IList pages = oneFile.GetChildNodes(NodeType.Page);
    foreach (Page page in pages)
    {
        Console.WriteLine("LastModifiedTime: {0}", page.LastModifiedTime);
        Console.WriteLine("CreationTime: {0}", page.CreationTime);
        Console.WriteLine("Title: {0}", page.Title);
        Console.WriteLine("Level: {0}", page.Level);
        Console.WriteLine("Author: {0}", page.Author);
        Console.WriteLine();
    }

    Please see the following topic for further help: Get Information of Each Page from a OneNote Document

  4. Get the text from a particular page of a OneNote document Aspose.Note also allows you to retrieve text based on a specific page number in an OneNote document. The code below shows how:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Get list of page nodes
    IList nodes = oneFile.GetChildNodes(NodeType.Page);
    
    if (nodes.Count > 0 && nodes[0].NodeType == NodeType.Page)
    {
        Page page = (Page)nodes[0];
        // Get text
        string text = page.GetText();
    }

    Please see the following topic for further help: Extracting Text from a Specified Page of OneNote Document.

  5. Get image info from a OneNote document – Aspose.Note allows developers to access details about each image in a document, for example file name, width, height, extension, and last modified time. The code that follows shows how to get details about each image from a OneNote file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Get list of Image nodes
    IList nodes = oneFile.GetChildNodes(NodeType.Image);
    
    foreach (Note.Image image in nodes)
    {
        Console.WriteLine("Width: {0}", image.Width);
        Console.WriteLine("Height: {0}", image.Height);
        Console.WriteLine("OriginalWidth: {0}", image.OriginalWidth);
        Console.WriteLine("OriginalHeight: {0}", image.OriginalHeight);
        Console.WriteLine("FileName: {0}", image.FileName);
        Console.WriteLine("Extension: {0}", image.Extension);
        Console.WriteLine("LastModifiedTime: {0}", image.LastModifiedTime);
        Console.WriteLine();
    }

    Please see the following topic for further help: Get Information of Each Image from a OneNote Document.

Extract Images from OneNote Files

  1. Extract all images from a OneNote document – Sometimes you want screenshots and clippings as actual image files. Aspose.Note has this feature and lets developers save these files as such. The following is code that saves all the images from a OneNote file:

    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"FullFileName.one");
    // Get list of Image nodes 
    IList nodes = oneFile.GetChildNodes(NodeType.Image);
    foreach (Note.Image image in nodes)
    {
       using (MemoryStream stream = new MemoryStream(image.Bytes))
       {
          using (Bitmap bitMap = new Bitmap(stream))
          {
             // Save the document as image
             bitMap.Save(String.Format(@"C:\Temp\{0}", Path.GetFileName(image.FileName)));
          }
       }
    }

    Please see the following topic for further help: Extract Images from a OneNote Document.

Search and Replace Text in OneNote File

When editing a OneNote book, finding and replacing text is helpful. For example, if the name of a project has changed, you can easily replace all instances of the old project name with the new one. In Aspose.Note, use the RichText class to replace specific text.

  1. Search and replace text on all pages of a OneNote document – The code below searches and replaces text on all pages of a OneNote file:

    Dictionary replacements = new Dictionary();
    replacements.Add("Old Text Here", "New Text Here");
    
    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"New Section 1.one");
    
    CompositeNode compositeNode = (CompositeNode)oneFile;
    // Get all RichText nodes
    IList textNodes = compositeNode.GetChildNodes(NodeType.RichText);
    
    foreach (Node node in textNodes)
    {
        foreach (KeyValuePair kvp in replacements)
        {
            RichText richText = (RichText)node;
            if (richText != null && richText.Text.Contains(kvp.Key))
            {
                // Replace text of a shape
                richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
            }
        }
    }
    
    // Save to any supported file format
    oneFile.Save(@"Output.pdf", SaveFormat.Pdf);

    Please see the following topic for further help: Replace Text on All Pages.

  2. Search and replace text on a particular page of a OneNote document – Below is code for searching and replacing text on a particular page of a OneNote file:

    Dictionary replacements = new Dictionary();
    replacements.Add("Old Text Here", "New Text Here");
    
    // Load the document into Aspose.Note.
    Document oneFile = new Document(@"New Section 1.one");
    
    IList pageNodes = oneFile.GetChildNodes(NodeType.Page);
    CompositeNode compositeNode = (CompositeNode)pageNodes[0];
    
    // Get all RichText nodes
    IList textNodes = compositeNode.GetChildNodes(NodeType.RichText);
    
    foreach (Node node in textNodes)
    {
        foreach (KeyValuePair kvp in replacements)
        {
            RichText richText = (RichText)node;
            if (richText != null && richText.Text.Contains(kvp.Key))
            {
                // Replace text of a shape
                richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
            }
        }
    }
    
    // Save to any supported file format
    oneFile.Save(@"Output.pdf", SaveFormat.Pdf);

    Please see the following topic for further help: Replace Text on a Particular Page.

We hope you’ll enjoy these API features that save time and efforts. The API is quite simple and developers can use it in application easily. Aspose.Note for .NET will allow you to convert, extract, read or update OneNote files without installing Microsoft OneNote on the server.

To view a complete list of API features and try the API at your end, please visit the following page and download the first version of Aspose.Note for .NET. If you need any help, please feel free to ask in the Aspose.Note forum. For more details, please visit the Aspose.Note for .NET documentation.

Join the Conversation

1 Comment

Leave a comment

Posted inAspose.Note Product Family, Imran.Rafique
 

Related Articles