Add Image to OneNote in Java

OneNote is a digital notebook that provides a single place for all of your notes and information. It 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 easily add images, insert pictures or photos to OneNote documents programmatically. In this article, we will learn how to add an image to a OneNote in Java.

The following topics shall be covered in this article:

  1. Java 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

Java 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 Java API. The API allows creating, reading, and converting OneNote documents programmatically without using Microsoft OneNote. It also allows inserting images and pictures into new or existing OneNote documents.

The API provides the Document class that represents an Aspose.Note document. It provides the save(String fileName) method for saving the OneNote document to a file. The Page class of the API represents a page in a OneNote document. The Image of the API represents an Image and provides various methods and properties to work with images in OneNote document.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-note</artifactId>
    <version>22.9</version>
</dependency>

Add Image to New OneNote Document in Java

We can create a new OneNote document and insert a picture or any 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 Java.

Add Image to New OneNote Document in Java

Add an Image to New OneNote Document in Java.

Add Image to Existing OneNote Document in Java

We can also insert a picture or any 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 Java.

Insert Image with Alternative Text in OneNote using Java

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 image alternative text using the setAlternativeTextTitle() and setAlternativeTextDescription() property methods.
  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 Java.

Insert Image with Alternative Text in OneNote using Java

Add an Image to New OneNote Document in Java.

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 and add an image using Java. We have also seen how to load an existing OneNote document and insert image into it programmatically. Besides, you can learn more about Aspose.Note for Java API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also