Working with Attachments
Retrieve Attached Files from a OneNote Document
All files attached to a Microsoft OneNote document can be retrieved. These files are stored in AttachedFile nodes. The AttachedFile class represents an attached file.
A OneNote file displaying attached files
To extract all attached files from a OneNote document, follow these steps:
- Use the Document.getChildNodes method to select all AttachedFile nodes.
- Iterate through the resulting node collections.
- Extract image bytes array using the AttachedFile.Bytes property.
- Save the attached file bytes to the local space.
This example shows how to extract attached files from a OneNote document and save them to local space.
1String dataDir = Utils.getSharedDataDir(RetrieveAttachment.class) + "text/";
2
3// Load the document into Aspose.Note
4Document doc = new Document(dataDir + "Sample1.one");
5
6// Get list of attachments
7List<AttachedFile> attachments = doc.getChildNodes(AttachedFile.class);
8System.out.println("Total attachments: " + attachments.size());
9
10for (AttachedFile a : attachments) {
11 // Load attachment into memory
12 byte[] buffer = a.getBytes();
13 ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
14
15 // Save it to output location
16 String outputFile = "Output_" + a.getFileName();
17 Path outputPath = Utils.getPath(RetrieveAttachment.class, outputFile);
18 Files.copy(stream, outputPath, StandardCopyOption.REPLACE_EXISTING);
19
20 System.out.println("File saved: " + outputPath);
21}
Attach a File to the OneNote Document
To keep a copy of any document or file as part of the OneNote document, developers can attach it to the OneNote page. The AttachedFile class represents an attachment file.
To attach a file to a OneNote document, follow these steps:
- Use the Document class to generate the OneNote file.
- Initialize Page, OutlineElement and Outline classes by passing document object.
- Initialize AttachedFile object by passing the document object and file path.
- Add an attached file node under the OutlineElement node.
- Save a OneNote document.
Attach File by Passing its Path
This example shows how to attach a file to a OneNote document.
1// The path to the documents directory.
2String dataDir = Utils.getSharedDataDir(AttachFileByPath.class) + "attachments\\";
3
4// Create an object of the Document class
5Document doc = new Document();
6
7// Initialize Page class object
8Page page = new Page(doc);
9
10// Initialize Outline class object
11Outline outline = new Outline(doc);
12
13// Initialize OutlineElement class object
14OutlineElement outlineElem = new OutlineElement(doc);
15
16// Initialize AttachedFile class object
17AttachedFile attachedFile = new AttachedFile(doc, dataDir + "attachment.txt");
18
19// Add attached file
20outlineElem.appendChildLast(attachedFile);
21
22// Add outline element node
23outline.appendChildLast(outlineElem);
24
25// Add outline node
26page.appendChildLast(outline);
27
28// Add page node
29doc.appendChildLast(page);
30dataDir = dataDir + "AttachFileByPath_out.one";
31doc.save(dataDir);
Attach File and Set its Icon by Passing Files Path
This example shows how to attach a file and also set an attach file icon in the OneNote document.
1// The path to the documents directory.
2String dataDir = Utils.getSharedDataDir(AttachFileByPath.class) + "attachments\\";
3
4// Create an object of the Document class
5Document doc = new Document();
6
7// Initialize Page class object
8Page page = new Page(doc);
9
10// Initialize Outline class object
11Outline outline = new Outline(doc);
12
13// Initialize OutlineElement class object
14OutlineElement outlineElem = new OutlineElement(doc);
15
16// Initialize AttachedFile class object and also pass its icon path
17AttachedFile attachedFile = null;
18try {
19 attachedFile = new AttachedFile(doc, dataDir + "attachment.txt", new FileInputStream(dataDir + "icon.jpg"), ImageFormat.getJpeg());
20} catch (FileNotFoundException e) {
21 e.printStackTrace();
22}
23
24// Add attached file
25outlineElem.appendChildLast(attachedFile);
26
27// Add outline element node
28outline.appendChildLast(outlineElem);
29
30// Add outline node
31page.appendChildLast(outline);
32
33// Add page node
34doc.appendChildLast(page);
35
36dataDir = dataDir + "AttachFileAndSetIcon_out.one";
37doc.save(dataDir);