Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/a203abbe23ea73343ae424e0c6ebe423 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/a203abbe23ea73343ae424e0c6ebe423 to your computer and use it in GitHub Desktop.
Insert or Extract Image from a OneNote file using Java
// Load the document into Aspose.Note
Document doc = new Document("Sample1.one");
// Get all images
List<Image> list = doc.getChildNodes(Image.class);
System.out.printf("Total Images: %s\n\n", list.size());
// Traverse the list
for (int i = 0; i < list.size(); i++) {
Image image = list.get(i);
String outputFile = "ExtractImages_out" + i + "_" + image.getFileName();
byte[] buffer = image.getBytes();
// Save output image file.
Files.write(Paths.get(dataDir + outputFile), buffer);
}
// Initialize LoadOptions class object.
LoadOptions options = new LoadOptions();
Document oneFile = new Document("Sample1.one", options);
// Get the first page of the document.
Page page = oneFile.getFirstChild();
// Load an image from the file.
Image image = new Image(oneFile, "Input.jpg");
// Change the image's size according to your needs (optional).
image.setWidth(100);
image.setHeight(100);
// Set the image's location in the page (optional).
image.setVerticalOffset(400);
image.setHorizontalOffset(100);
// Set image alignment
image.setAlignment(HorizontalAlignment.Right);
// Add the image to the page.
page.appendChildLast(image);
// Save the document in the .one format.
oneFile.save("InsertImage_out.one", SaveFormat.One);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment