Work or Extract Images from OneNote Document using C#
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:
- Use the Document.GetChildNodes method to select all Image nodes.
- Iterate through the resulting Image node collections.
- Extract image bytes array using the Image.Bytes property.
- Save image bytes to a file.
The code example given below demonstrates how to extract images from a OneNote document and save them as files.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Aspose.one");
6
7// Get all Image nodes
8IList<Aspose.Note.Image> nodes = oneFile.GetChildNodes<Aspose.Note.Image>();
9
10foreach (Aspose.Note.Image image in nodes)
11{
12 using (MemoryStream stream = new MemoryStream(image.Bytes))
13 {
14 using (Bitmap bitMap = new Bitmap(stream))
15 {
16 // Save image bytes to a file
17 bitMap.Save(String.Format(dataDir + "{0}", Path.GetFileName(image.FileName)));
18 }
19 }
20}
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
The code example given below demonstrates how to get information about each image from a OneNote document.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Aspose.one");
6
7// Get all Image nodes
8IList<Aspose.Note.Image> images = oneFile.GetChildNodes<Aspose.Note.Image>();
9
10foreach (Aspose.Note.Image image in images)
11{
12 Console.WriteLine("Width: {0}", image.Width);
13 Console.WriteLine("Height: {0}", image.Height);
14 Console.WriteLine("OriginalWidth: {0}", image.OriginalWidth);
15 Console.WriteLine("OriginalHeight: {0}", image.OriginalHeight);
16 Console.WriteLine("FileName: {0}", image.FileName);
17 Console.WriteLine("LastModifiedTime: {0}", image.LastModifiedTime);
18 Console.WriteLine();
19}
Insert an Image on a OneNote Document Page
Aspose.Diagram for .NET APIs now allows inserting an image anywhere on the OneNote document.
An image can be inserted in the following ways.
Insert an Image in an Existing OneNote Document
To insert an image on a OneNote document, follow these steps:
- Use the Document.FirstChild property to get the first page.
- Use the Image class constructor to load the image.
- Use the Image.Width and Image.Height properties to adjust the size of the image.
- Use the Image.VerticalOffset and Image.HorizontalOffset properties to set the location of the image.
- Use the Page.AppendChild property to insert the image.
- Save a OneNote document.
The code example given below demonstrates how to insert an image on a OneNote document and save them as files.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
3
4// Load document from the stream.
5Document doc = new Document(dataDir + "Aspose.one");
6// Get the first page of the document.
7Aspose.Note.Page page = doc.FirstChild;
8
9// Load an image from the file.
10Aspose.Note.Image image = new Aspose.Note.Image(doc, dataDir + "image.jpg");
11// Change the image's size according to your needs (optional).
12image.Width = 100;
13image.Height = 100;
14// Set the image's location in the page (optional).
15image.VerticalOffset = 400;
16image.HorizontalOffset = 100;
17// Set image alignment
18image.Alignment = HorizontalAlignment.Right;
19// Add the image to the page.
20page.AppendChildLast(image);
Build a OneNote Document from Scratch and Insert an Image
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
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 and set offset properties
9Outline outline = new Outline(doc) { VerticalOffset = 0, HorizontalOffset = 0 };
10// Initialize OutlineElement class object
11OutlineElement outlineElem = new OutlineElement(doc);
12// Load an image by the file path.
13Aspose.Note.Image image = new Aspose.Note.Image(doc, dataDir + "image.jpg");
14// Set image alignment
15image.Alignment = 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
25dataDir = dataDir + "BuildDocAndInsertImage_out.one";
26// Save OneNote document
27doc.Save(dataDir);
Build a OneNote Document from the Scratch and Insert an Image using an Image Stream
This code example shows how to build a new OneNote document and insert an image using the image stream.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
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
9Outline outline1 = new Outline(doc) { VerticalOffset = 600, HorizontalOffset = 0 };
10OutlineElement outlineElem1 = new OutlineElement(doc);
11FileStream fs = File.OpenRead(dataDir + "image.jpg");
12// Load the second image using the image name, extension and stream.
13Aspose.Note.Image image1 = new Aspose.Note.Image(doc, "Penguins.jpg", fs);
14// Set image alignment
15image1.Alignment = HorizontalAlignment.Right;
16
17outlineElem1.AppendChildLast(image1);
18outline1.AppendChildLast(outlineElem1);
19page.AppendChildLast(outline1);
20
21doc.AppendChildLast(page);
22
23dataDir = dataDir + "BuildDocAndInsertImageUsingImageStream_out.one";
24// Save OneNote document
25doc.Save(dataDir);
Support for Image Alternative Text
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
3
4var document = new Document();
5var page = new Page(document);
6var image = new Image(document, dataDir + "image.jpg");
7image.AlternativeTextTitle = "This is an image's title!";
8image.AlternativeTextDescription = "And this is an image's description!";
9page.AppendChildLast(image);
10document.AppendChildLast(page);
11
12dataDir = dataDir + "ImageAlternativeText_out.one";
13document.Save(dataDir);
Link an Image to Hyperlink
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Images();
3
4var document = new Document();
5
6var page = new Page(document);
7
8var image = new Image(document, dataDir + "image.jpg");
9
10image.HyperlinkUrl = "http://image.com";
11
12page.AppendChildLast(image);
13
14document.AppendChildLast(page);
15
16document.Save(dataDir + "Image with Hyperlink_out.one");
Remove an Image
1var document = new Document("file.one");
2
3var images = document.GetChildNodes<Image>();
4
5foreach (Aspose.Note.Image image in images)
6{
7 if (image.ParentNode == null)
8 {
9 continue;
10 }
11
12 (image.ParentNode as Page)?.RemoveChild(image);
13 // The parent element of the image can be not only the page
14 (image.ParentNode as OutlineElement)?.RemoveChild(image);
15}
16
17document.Save("after.pdf");