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.
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.
The following code example demonstrates how to extract attached files from a OneNote document and save them to local space.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Attachments();
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document( dataDir + "Sample1.one");
6
7// Get a list of attached file nodes
8IList<AttachedFile> nodes = oneFile.GetChildNodes<AttachedFile>();
9
10// Iterate through all nodes
11foreach (AttachedFile file in nodes)
12{
13 // Load attached file to a stream object
14 using (Stream outputStream = new MemoryStream(file.Bytes))
15 {
16 // Create a local file
17 using (Stream fileStream = System.IO.File.OpenWrite(String.Format(dataDir + file.FileName)))
18 {
19 // Copy file stream
20 CopyStream(outputStream, fileStream);
21 }
22 }
23}
1private static void CopyStream(Stream input, Stream output)
2{
3 try
4 {
5 byte[] buffer = new byte[8 * 1024];
6 int len;
7 while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
8 {
9 output.Write(buffer, 0, len);
10 }
11 }
12 catch (Exception ex)
13 {
14 throw new Exception(ex.Message);
15 }
16}
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 attached file node under the OutlineElement node.
- Save a OneNote document.
Attach File by Passing its Path
The following code example demonstrates how to attach a file to a OneNote document.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Attachments();
3
4// Create an object of the Document class
5Document doc = new Document();
6// Initialize Page class object
7Aspose.Note.Page page = new Aspose.Note.Page(doc);
8// Initialize Outline class object
9Outline outline = new Outline(doc);
10// Initialize OutlineElement class object
11OutlineElement outlineElem = new OutlineElement(doc);
12// Initialize AttachedFile class object
13AttachedFile attachedFile = new AttachedFile(doc, dataDir + "attachment.txt");
14// Add attached file
15outlineElem.AppendChildLast(attachedFile);
16// Add outline element node
17outline.AppendChildLast(outlineElem);
18// Add outline node
19page.AppendChildLast(outline);
20// Add page node
21doc.AppendChildLast(page);
22dataDir = dataDir + "AttachFileByPath_out.one";
23doc.Save(dataDir);
Attach File and Set its Icon by Passing Files Path
The following code example demonstrates how to attach a file and also set attach file icon in the OneNote document.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Attachments();
3
4// Create an object of the Document class
5Document doc = new Document();
6// Initialize Page class object
7Aspose.Note.Page page = new Aspose.Note.Page(doc);
8// Initialize Outline class object
9Outline outline = new Outline(doc);
10// Initialize OutlineElement class object
11OutlineElement outlineElem = new OutlineElement(doc);
12// Initialize AttachedFile class object and also pass its icon path
13AttachedFile attachedFile = new AttachedFile(doc, dataDir + "attachment.txt", File.OpenRead(dataDir + "icon.jpg"), ImageFormat.Jpeg);
14// Add attached file
15outlineElem.AppendChildLast(attachedFile);
16// Add outline element node
17outline.AppendChildLast(outlineElem);
18// Add outline node
19page.AppendChildLast(outline);
20// Add page node
21doc.AppendChildLast(page);
22
23dataDir = dataDir + "AttachFileAndSetIcon_out.one";
24doc.Save(dataDir);