Working with Images

Extract Images from a OneNote Document

All images are stored inside image nodes in a OneNote document.

Extract Images

To extract all images or from a OneNote document, follow these steps:

  1. Use the Document.getChildNodes method to select all Image nodes.
  2. Iterate through the resulting Image node collections.
  3. Extract image bytes array using the Image.Bytes property.
  4. Save image bytes to a file.

This example shows how to extract images from a OneNote document and save them as files.

 1String dataDir = Utils.getSharedDataDir(ExtractImages.class) + "images/";
 2
 3// Load the document into Aspose.Note
 4Document doc = new Document(dataDir + "Sample1.one");
 5
 6// Get all images
 7List<Image> list = doc.getChildNodes(Image.class);
 8System.out.printf("Total Images: %s\n\n", list.size());
 9
10// Traverse the list
11for (int i = 0; i < list.size(); i++) {
12	Image image = list.get(i);
13
14	String outputFile = "ExtractImages_out" + i + "_" + image.getFileName();
15
16	byte[] buffer = image.getBytes();
17	Files.write(Paths.get(dataDir + outputFile), buffer);
18	System.out.printf("File saved: %s\n", dataDir);
19}

Get Information of Each Image from the OneNote Document

The Image class provides all the image properties for images in OneNote documents. All the images of the OneNote file are contained by image nodes in the Document object.

Get Information

 1String dataDir = Utils.getSharedDataDir(GetImageInfo.class) + "images/";
 2
 3// Load the document into Aspose.Note
 4Document doc = new Document(dataDir + "Sample1.one");
 5
 6// Get all images
 7List<Image> list = doc.getChildNodes(Image.class);
 8System.out.printf("Total Images: %s\n\n", list.size());
 9
10// Traverse the list
11for (Image image : list) {
12	System.out.println("Width: " + image.getWidth());
13	System.out.println("Height: " + image.getHeight());
14	System.out.println("OriginalWidth: " + image.getOriginalWidth());
15	System.out.println("OriginalHeight: " + image.getOriginalHeight());
16	System.out.println("FileName: " + image.getFileName());
17	System.out.println("LastModifiedTime: " + image.getLastModifiedTime());
18	System.out.println();
19}

Adding Image to a OneNote Document Page and Saving as PDF

Aspose.Diagram for JAVA APIs now allows inserting an image anywhere on the OneNote document.

These examples show how to:

Insert an Image in an Existing OneNote Document

To insert an image on a OneNote document, follow these steps:

  1. Use the Document.getFirstChild() property to get the first page.
  2. Use the Image class constructor to load the image.
  3. Use the Image.getWidth() and Image.getHeight() properties to adjust size of the image.
  4. Use the Image.getVerticalOffset() and Image.getHorizontalOffset() properties to set location of the image.
  5. Use the Page.appendChild() property to insert the image.
  6. Save a OneNote document.

This example shows how to insert an image on a OneNote document and save them as files.

 1// load document from the stream.
 2LoadOptions options = new LoadOptions();
 3
 4String dataDir = Utils.getSharedDataDir(InsertanImage.class) + "images/";
 5		
 6Document oneFile = new Document(dataDir + "Sample1.one", options);
 7
 8// get the first page of the document.
 9Page page = oneFile.getFirstChild();
10
11// load an image from the file.
12Image image = new Image(oneFile, dataDir + "Input.jpg");
13// change the image's size according to your needs (optional).
14image.setWidth(100);
15image.setHeight(100);
16// set the image's location in the page (optional).
17image.setVerticalOffset(400);
18image.setHorizontalOffset(100);
19// set image alignment
20image.setAlignment(HorizontalAlignment.Right);
21// add the image to the page.
22page.appendChildLast(image);
23
24// save the document in the *.one format.
25try {
26	// oneFile.save("D://Aspose_JavaProjects//OneNote//out.one");//NOT
27	// working
28	oneFile.save(dataDir + "InsertanImage_out.pdf", SaveFormat.Pdf);// WORKING
29} catch (IOException e) {
30	e.printStackTrace();
31}

Build a OneNote Document from the Scratch and Insert an Image

This example shows how to build a new OneNote document and insert an image by file path.

 1String dataDir = Utils.getSharedDataDir(BuildDocAndInsertImage.class) + "images/";
 2// create an object of the Document class
 3Document doc = new Document();
 4// initialize Page class object
 5Page page = new Page(doc);
 6// initialize Outline class object and set offset properties
 7Outline outline = new Outline(doc);
 8outline.setVerticalOffset(0);
 9outline.setHorizontalOffset(0);
10// initialize OutlineElement class object
11OutlineElement outlineElem = new OutlineElement(doc);
12// load an image by the file path.
13Image image = new Image(doc, dataDir + "Input.jpg");
14// set image alignment
15image.setAlignment(HorizontalAlignment.Right);
16// add image
17outlineElem.appendChildLast(image);
18// add outline elements
19outline.appendChildLast(outlineElem);
20// add Outline node
21page.appendChildLast(outline);
22// add Page node
23doc.appendChildLast(page);
24// save OneNote document
25// save the document in the *.one format.
26try {
27	// oneFile.save("D://Aspose_JavaProjects//OneNote//out.one");//NOT
28	// working
29	doc.save(dataDir + "NewOneNotedocument_out", SaveFormat.Pdf);// WORKING
30} catch (IOException e) {
31	e.printStackTrace();
32}

Build a OneNote Document from the Scratch and Insert an Image using an Image Stream

This example shows how to build a new OneNote document and insert an image using the image stream.

 1String dataDir = Utils.getSharedDataDir(BuildDocAndInsertImageUsingImageStream.class) + "images/";
 2
 3// create an object of the Document class
 4Document doc = new Document();
 5// initialize Page class object
 6Page page = new Page(doc);
 7
 8Outline outline1 = new Outline(doc);
 9outline1.setVerticalOffset(600);
10outline1.setHorizontalOffset(0);
11OutlineElement outlineElem1 = new OutlineElement(doc);
12InputStream fs = null;
13try {
14	fs = new FileInputStream(dataDir + "image.jpg");
15} catch (FileNotFoundException e) {
16	e.printStackTrace();
17}
18
19// Load the second image using the image name, extension and stream.
20Image image = new Image(doc, dataDir + "image1.jpg");
21// set image alignment
22image.setAlignment(HorizontalAlignment.Right);
23
24outlineElem1.appendChildLast(image);
25outline1.appendChildLast(outlineElem1);
26page.appendChildLast(outline1);
27
28doc.appendChildLast(page);
29// save OneNote document
30try {
31	doc.save("D://Aspose_JavaProjects//OneNote//out3.pdf",SaveFormat.Pdf);
32} catch (IOException e) {
33	e.printStackTrace();
34}

Support for Image Alternative Text

 1String dataDir = Utils.getSharedDataDir(AlternativeText.class) + "images/";
 2
 3Document document = new Document();
 4
 5Page page = new Page(document);
 6
 7Image image = new Image(document, dataDir + "image.jpg");
 8
 9image.setAlternativeTextTitle("ImageAlternativeText Title");
10		
11image.setAlternativeTextDescription("ImageAlternativeText Description");
12
13page.appendChildLast(image);
14
15document.appendChildLast(page);
16
17document.save(dataDir + "AlternativeText_out.one");
1Document document = new Document();
2Page page = new Page(document);
3Image image = new Image(document, dataDir + "image1.jpg");
4image.setHyperlinkUrl( "http://www.aspose.com");
5page.appendChildLast(image);
6document.appendChildLast(page);
7document.save(dataDir + "HyperlinkToImage_out.one");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.