Replacing images in onenote

Hi, I’m trying to replace images in a onenote file using Aspose.Note .NET CORE sdk.

I’ve done it easily for word and other office formats, but in Aspose.Notes, the Aspose.Notes.Image class does not have a setter for the Bytes property.

I’ve also tried to remove the image node and add a new one instead, but I got this exception:
System.ArgumentException: The node belongs to the other node.
at Aspose.Note.CompositeNode`1.RemoveChild(T oldChild)

I’d really appreciate some help here.
Thanks a lot!

@Nir_malam,

See the document with example code on working with images for your reference.

If you still find any issue, kindly share your sample code/project with sample document and image(s), we will check it soon.

PS. please zip the files prior attaching here.

@Nir_malam
Hi Nir,
Did you ever get this issue resolved? I am running in to the same issue…

Thanks :slight_smile:
Uriel

@Buffer2018,

Please refer to the document with example codes on working with images.

If you still find any issue, kindly share your sample code/project with sample document(s) and image(s), we will check it soon.

PS. please zip the files prior attaching here.

@amjad.sahi
Thank you for your prompt reply. However, the document referenced does not include any information about how to replace or remove images - only how to extract or add images.
I am looking to replace attachments and images.

@Buffer2018,

Thanks for the sample OneNote document.

Yes, the document does not have any example on how to remove images. However, one can try to remove image from the image collection. Anyways, we did test and we reproduced the issue as you mentioned. We have already a new ticket in our internal issue tracking system for your issue. Please follow up your other thread to get latest updates (once available) on your issue.

@amjad.sahi
Thank you for the fast reply. Can you guys also add to the documentation/examples how to remove and
also how to replace images and attachments?

thanks!

@Buffer2018,

Yes, we already discussed about enhancing the relevant document(s) in the Docs for the users. Hopefully, we will update the docs soon.

Hi!

Can you clarify what kind of replace operation do you mean?
Do you want to replace every existing Image node with new Image node containing new bitmap?
Or do you want to place node another kind(i.e. RichText) instead of every Image node?

Thanks!

I want the node to stay exactly where it is and display the same type and size of image - but I will change the image data a bit. Just as an example, think about taking a PNG and changing every second pixel to a white pixel, then replacing the original PNG in the document with the new one.

@Buffer2018,

Could you please zip and attach sample OneNote documents (input document and expected output document) and image files (to be replaced)? We will look into it soon.

Here is a sample one note document
embed_onenote.one.zip (21.5 KB)

Currently, because there is no way to set the Aspose.Note.Image.Bytes property, my current code adds a new image node and removes the previous node. I would much rather do something like

byte[] new_bytes = ...;
page.getChildNodes<Image>()[0].Bytes = new_bytes;

than to do this:

/// Helper method to remove child nodes
public static void RemoveChild(this ICompositeNode parent, Node child)
{
if (parent is Page)
(parent as Page).RemoveChild((IPageChildNode)child);
else if (parent is Outline)
(parent as Outline).RemoveChild((IOutlineChildNode)child);
else if (parent is OutlineElement)
(parent as OutlineElement).RemoveChild((IOutlineElementChildNode)child);
else if (parent is Notebook)
(parent as Notebook).RemoveChild((INotebookChildNode)child);
else
throw new FileFormatException();
}

/// Similar helper function to add child nodes
public static void AppendChildLast(this ICompositeNode parent, Node child)
{
/// …Code removed for brevity…
}

/// … code shortened for brevity …
/// In this code ‘image’ is the original image node in the document and ‘newImage’
/// is the new image being added.
List imagesToRemove = new List();
IList images = page.GetChildNodes();
for (int i = 0; i < images.Count; i++)
{
imagesToRemove.Add(image);
var newImage = new Image(“some_other_file_with_the_same_size.png”);
newImage.Alignment = image.Alignment;
newImage.AlternativeTextDescription = image.AlternativeTextDescription;
newImage.AlternativeTextTitle = image.AlternativeTextTitle;
newImage.Height = image.Height;
newImage.HorizontalOffset = image.HorizontalOffset;
newImage.HyperlinkUrl = image.HyperlinkUrl;
newImage.IsBackground = image.IsBackground;
newImage.LastModifiedTime = image.LastModifiedTime;
newImage.VerticalOffset = image.VerticalOffset;
newImage.Width = image.Width;
// TODO: Fix this when Aspose fixes their bug removing elements
image.ParentNode.AppendChildLast(newImage);
// …
}
// Remove all images that were replaced
imagesToRemove.ForEach(imageNode =>
{
imageNode.ParentNode.RemoveChild(imageNode);
});

@Buffer2018,

Thanks for the sample OneNote document and further details.

We will evaluate the feature for replacing images in OneNote file in one go. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): NOTENET-5828

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Buffer2018,

We have evaluated the ticket (logged earlier as “NOTENET-5828”) in detail. We may add a new method for image replacement to the APIs to meet your needs. Hopefully, in the upcoming release, we could include the supported API.

Once we have any updates on it or we publish the supported version, we will let you know here.

The issues you have found earlier (filed as NOTENET-5828) have been fixed in this update. This message was posted using Bugs notification tool by senua.remizova