Have you ever wanted to insert a PDF or other document into OneNote document programmatically? You’re in luck, because this article will show you how to insert PDF into a OneNote using C#.
OneNote is Microsoft’s note-taking and digital task management software. In addition to collecting, organizing, and collaborating on free-form information, it allows multi-user collaboration with notes, drawings, screen scraps, and audio comments. Sometimes, we may need to insert PDF files into OneNote documents as attachments. The goal of this article is to provide a solution for inserting a PDF document into OneNote using C#.
If you are looking for a way to import the content from PDF documents into notes, you can read my previous blog post providing a step-by-step guide on how to convert PDF to OneNote in C#.
The following topics will be covered in this article:
- C# API to Insert PDF into OneNote
- Insert PDF into New OneNote Document
- Insert PDF into Existing OneNote Document
C# API to Insert PDF into OneNote
For inserting a PDF document into a new or existing OneNote document, we will be using the Aspose.Note for .NET API. It is a feature-rich OneNote document manipulation API that lets you create, read, and convert OneNote documents. It also allows inserting images, pictures or other documents into new or existing OneNote documents. In addition, users can import content from PDF documents into OneNote documents using the API.
Please either download the DLL of the API or install it using NuGet.
PM> Install-Package Aspose.Note
Insert PDF into New OneNote Document using C#
We can easily create a new OneNote document and insert a PDF file as an attachment by following the steps given below:
- Firstly, create an instance of the Document class.
- Next, create objects of the Page, Outline, and the OutlineElement classes.
- Then, read PDF file and its icon file into the FileStream objects.
- Meanwhile, initialize the AttachedFile class object and pass the file name and stream objects as arguments.
- After that, add the AttachedFile to the OutlineElement using the AppendChildLast() method.
- Similarly, append Outline to the Page and Page to the Document.
- Finally, call the Save() method to save the OneNote document.
The following code sample shows how to insert a PDF file into a new OneNote document using C#.
Insert PDF into Existing OneNote Document in C#
We can also insert a PDF document into an existing OneNote document by following the steps mentioned above. However, we just need to load an existing OneNote using the Document class in the step # 1.
The following code sample shows how to insert a PDF to an existing OneNote document using C#.
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
Conclusion
In this article, we have learned how to:
- create a new OneNote document programmatically;
- load an existing OneNote document;
- load and insert a PDF as an attachment into OneNote.
Besides inserting a PDF into OneNote using C#, you can learn more about Aspose.Note for .NET API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.