Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created December 2, 2022 17:34
Insert PDF into OneNote using C# | PDF in OneNote C#
// Load an existing OneNote document
Document doc = new Document("C:\\Files\\Sample1.one");
// Initialize Page class object
Page page = new Page();
// Initialize Outline class object
Outline outline = new Outline();
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement();
// Input PDF file to insert
var stream = File.OpenRead("C:\\Files\\companies.pdf");
// PDF file icon to insert
var iconStream = File.OpenRead("C:\\Files\\PDF_Icon.png");
// Initialize AttachedFile class object and also pass its icon path
//AttachedFile attachedFile = new AttachedFile("companies.pdf", stream);
AttachedFile attachedFile = new AttachedFile("companies.pdf", stream, iconStream, ImageFormat.Png);
// Add attached file
outlineElem.AppendChildLast(attachedFile);
// Add outline element node
outline.AppendChildLast(outlineElem);
// Add outline node
page.AppendChildLast(outline);
// Add page node
doc.AppendChildLast(page);
// Save the Document
doc.Save("C:\\Files\\Sample1_out.one");
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page();
// Initialize Outline class object
Outline outline = new Outline();
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement();
// Input PDF file to insert
var stream = File.OpenRead("C:\\Files\\companies.pdf");
// PDF file icon to insert
var iconStream = File.OpenRead("C:\\Files\\PDF_Icon.png");
// Initialize AttachedFile class object and also pass its icon path
//AttachedFile attachedFile = new AttachedFile("companies.pdf", stream);
AttachedFile attachedFile = new AttachedFile("companies.pdf", stream, iconStream, ImageFormat.Png);
// Add attached file
outlineElem.AppendChildLast(attachedFile);
// Add outline element node
outline.AppendChildLast(outlineElem);
// Add outline node
page.AppendChildLast(outline);
// Add page node
doc.AppendChildLast(page);
// Save the Document
doc.Save("C:\\Files\\AttachFileAndSetIcon_out.one");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment