Add Image to OneNote in C#

OneNote allows collecting, organizing, and collaborating free-form information and multi-user collaboration in the form of notes, drawings, screen clippings, and audio commentaries. We can insert images into OneNote documents programmatically. In this article, we will learn how to add an image to a OneNote in C#.

The following topics shall be covered in this article:

  1. C# API to Add Images to OneNote
  2. Add Image to a New OneNote Document
  3. Insert Image into an Existing OneNote Document
  4. Add Image with Alternative Text

C# API to Add Images to OneNote

To add an image to a new or existing OneNote document, we will be using the Aspose.Note for .NET API. The API allows creating, reading, and converting OneNote documents programmatically without using Microsoft OneNote. It also allows importing content from PDF documents into OneNote documents.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Note

Add Image to New OneNote Document in C#

We can create a new OneNote document and insert an image by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, initialize the Page class object.
  3. Then, load an image using the Image class.
  4. Optionally, set image height, width, alignment, offset, etc.
  5. After that, add the Image to the Page using the AppendChildLast() method.
  6. Similarly, add the Page to the Document.
  7. Finally, call the Save() method to save the OneNote document.

The following code sample shows how to add an image to a new OneNote document using C#.

// This code sample demonstrates how to add an image to a new OneNote document.
// The path to the documents directory.
string dataDir = "C:\\Files\\Note\\";
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page();
// Load an image by the file path.
Image image = new Image(dataDir + "sample.jpg");
// Set image alignment
image.Alignment = HorizontalAlignment.Right;
// Add image
page.AppendChildLast(image);
// Add Page node
doc.AppendChildLast(page);
// Save OneNote document
dataDir = dataDir + "CreateOneNoteWithImage.one";
doc.Save(dataDir);
Add-Image-to-New-OneNote-Document-in-CSharp

Add an Image to New OneNote Document in C#.

Add Image to Existing OneNote Document in C#

We can also insert an image into an existing OneNote document by following the steps given below:

  1. Firstly, load an existing OneNote using the Document class.
  2. Next, initialize the Page class object.
  3. Then, load an image using the Image class.
  4. Optionally, set image height, width, alignment, offset, etc.
  5. After that, add the Image to the Page using the AppendChildLast() method.
  6. Similarly, add the Page to the Document.
  7. Finally, call the Save() method to save the OneNote document.

The following code sample shows how to add an image to an existing OneNote document using C#.

// This code sample demonstrates how to add an image to an existing OneNote document.
// The path to the documents directory.
string dataDir = "C:\\Files\\Note\\";
// Load document from the stream.
Document doc = new Document(dataDir + "Sample1.one");
// Add a new page.
Page page = new Page();
// Load an image from the file.
Image image = new Image(dataDir + "sample.jpg");
// Change the image's size according to your needs (optional).
image.Width = 500;
image.Height = 750;
// Set the image's location in the page (optional).
image.VerticalOffset = 400;
image.HorizontalOffset = 100;
// Set image alignment
image.Alignment = HorizontalAlignment.Right;
// Add the image to the page.
page.AppendChildLast(image);
doc.AppendChildLast(page);
// Save OneNote document
dataDir = dataDir + "InsertImageIntoExisting.one";
doc.Save(dataDir);

Insert Image with Alternative Text in OneNote using C#

We can add image alternative text while inserting it into a OneNote document by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, initialize the Page class object.
  3. Then, load an image using the Image class.
  4. Next, specify the AlternativeTextTitle and AlternativeTextDescription properties.
  5. After that, add the Image to the Page using the AppendChildLast() method.
  6. Similarly, add the Page to the Document.
  7. Finally, call the Save() method to save the OneNote document.

The following code sample shows how to insert an image with alternative text in OneNote using C#.

// This code sample demonstrates how to add an image with Alternative text in OneNote document.
// The path to the documents directory.
string dataDir = "C:\\Files\\Note\\";
// Create a new document
var document = new Document();
// Add a new Page
var page = new Page();
// Load an Image
var image = new Image(dataDir + "sample.jpg");
// Specify image alternative text and description
image.AlternativeTextTitle = "This is an image's title!";
image.AlternativeTextDescription = "And this is an image's description!";
// Append an image
page.AppendChildLast(image);
// Append page
document.AppendChildLast(page);
// Save the document
dataDir = dataDir + "ImageAlternativeText.one";
document.Save(dataDir);

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new OneNote document using C#;
  • load an existing OneNote document;
  • add a page to the OneNote document;
  • add an image to OneNote document Page programmatically;

Besides adding an image to a OneNote in C#, you can learn more about Aspose.Note for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also