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:
- Java API to Add Images to OneNote
- Add Image to a New OneNote Document
- Insert Image into an Existing OneNote Document
- 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:
- Firstly, create an instance of the Document class.
- Next, initialize the Page class object.
- Then, load an image using the Image class.
- Optionally, set image height, width, alignment, offset, etc.
- After that, add the Image to the Page using the appendChildLast() method.
- Similarly, add the Page to the Document.
- 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 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:
- Firstly, load an existing OneNote using the Document class.
- Next, initialize the Page class object.
- Then, load an image using the Image class.
- Optionally, set image height, width, alignment, offset, etc.
- After that, add the Image to the Page using the appendChildLast() method.
- Similarly, add the Page to the Document.
- 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:
- Firstly, create an instance of the Document class.
- Next, initialize the Page class object.
- Then, load an image using the Image class.
- Next, specify the image alternative text using the setAlternativeTextTitle() and setAlternativeTextDescription() property methods.
- After that, add the Image to the Page using the appendChildLast() method.
- Similarly, add the Page to the Document.
- 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.
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.