Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 25, 2022 09:56
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/7c46c61f771888e8969767564d380fe7 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7c46c61f771888e8969767564d380fe7 to your computer and use it in GitHub Desktop.
Extract Text or Images from OneNote Documents using Java
// This code example demonstrates how to Extract images from OneNode document.
// Load the document into Aspose.Note
Document doc = new Document("D:\\Files\\Note\\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);
// Show image properties
System.out.println("Width: " + image.getWidth());
System.out.println("Height: " + image.getHeight());
System.out.println("OriginalWidth: " + image.getOriginalWidth());
System.out.println("OriginalHeight: " + image.getOriginalHeight());
System.out.println("FileName: " + image.getFileName());
System.out.println("LastModifiedTime: " + image.getLastModifiedTime());
String outputFile = "ExtractImages_out" + i + "_" + image.getFileName();
// Save the image
byte[] buffer = image.getBytes();
Files.write(Paths.get("D:\\Files\\Note\\Images\\" + outputFile), buffer);
System.out.printf("File saved: %s\n", outputFile);
}
// This code example demonstrates how to Extract text from pages of a OneNode document.
// Load the document into Aspose.Note.
Document doc = new Document("D:\\Files\\Note\\Sample1.one");
// Get list of page nodes
List<Page> pages = doc.getChildNodes(Page.class);
for (Page p : pages) {
System.out.println("---- Page Started Here ----");
List<RichText> textNodes = (List<RichText>) p.getChildNodes(RichText.class);
for (RichText richText : textNodes) {
if(!richText.getText().isBlank())
System.out.println(richText.getText().toString());
}
System.out.println("---- Page Ended Here ----");
System.out.println();
}
// This code example demonstrates how to Extract text from a specific page of a OneNode document.
// Load the document into Aspose.Note
Document doc = new Document("D:\\Files\\Note\\Sample1.one");
// Get list of page nodes
List<Page> pages = doc.getChildNodes(Page.class);
// Get page by index
Page page = pages.get(0);
// Get text of the page
List<RichText> textNodes = (List<RichText>) page.getChildNodes(RichText.class);
// Show text
for (RichText richText : textNodes) {
if(!richText.getText().isBlank())
System.out.println(richText.getText().toString());
}
// This code example demonstrates how to Extract all the text from OneNode document.
// Load the document into Aspose.Note.
Document oneFile = new Document("D:\\Files\\Note\\Sample1.one");
// Retrieve text
List<RichText> textNodes = (List<RichText>) oneFile.getChildNodes(RichText.class);
for (RichText richText : textNodes) {
if(!richText.getText().isBlank())
System.out.println(richText.getText().toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment