Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 16, 2020 17:09
Show Gist options
  • Save aspose-com-gists/f1c4460425d3a75dd63cd514a9833946 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f1c4460425d3a75dd63cd514a9833946 to your computer and use it in GitHub Desktop.
Aspose.Note for .NET
Aspose.Note for .NET
// Instantiate the License class
Aspose.Note.License license = new Aspose.Note.License();
// Pass only the name of the license file embedded in the assembly
license.SetLicense("Aspose.Note.lic");
Aspose.Note.License license = new Aspose.Note.License();
license.SetLicense("Aspose.Note.lic");
Aspose.Note.License license = new Aspose.Note.License();
FileStream myStream = new FileStream("Aspose.Note.lic", FileMode.Open);
license.SetLicense(myStream);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Attachments();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Initialize Outline class object
Outline outline = new Outline(doc);
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
// Initialize AttachedFile class object and also pass its icon path
AttachedFile attachedFile = new AttachedFile(doc, dataDir + "attachment.txt", File.OpenRead(dataDir + "icon.jpg"), ImageFormat.Jpeg);
// 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);
dataDir = dataDir + "AttachFileAndSetIcon_out.one";
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Attachments();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Initialize Outline class object
Outline outline = new Outline(doc);
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
// Initialize AttachedFile class object
AttachedFile attachedFile = new AttachedFile(doc, dataDir + "attachment.txt");
// 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);
dataDir = dataDir + "AttachFileByPath_out.one";
doc.Save(dataDir);
private static void CopyStream(Stream input, Stream output)
{
try
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Attachments();
// Load the document into Aspose.Note.
Document oneFile = new Document( dataDir + "Sample1.one");
// Get a list of attached file nodes
IList<AttachedFile> nodes = oneFile.GetChildNodes<AttachedFile>();
// Iterate through all nodes
foreach (AttachedFile file in nodes)
{
// Load attached file to a stream object
using (Stream outputStream = new MemoryStream(file.Bytes))
{
// Create a local file
using (Stream fileStream = System.IO.File.OpenWrite(String.Format(dataDir + file.FileName)))
{
// Copy file stream
CopyStream(outputStream, fileStream);
}
}
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Initialize Outline class object and set offset properties
Outline outline = new Outline(doc) { VerticalOffset = 0, HorizontalOffset = 0 };
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
// Load an image by the file path.
Aspose.Note.Image image = new Aspose.Note.Image(doc, dataDir + "image.jpg");
// Set image alignment
image.Alignment = HorizontalAlignment.Right;
// Add image
outlineElem.AppendChildLast(image);
// Add outline elements
outline.AppendChildLast(outlineElem);
// Add Outline node
page.AppendChildLast(outline);
// Add Page node
doc.AppendChildLast(page);
dataDir = dataDir + "BuildDocAndInsertImage_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
Outline outline1 = new Outline(doc) { VerticalOffset = 600, HorizontalOffset = 0 };
OutlineElement outlineElem1 = new OutlineElement(doc);
FileStream fs = File.OpenRead(dataDir + "image.jpg");
// Load the second image using the image name, extension and stream.
Aspose.Note.Image image1 = new Aspose.Note.Image(doc, "Penguins.jpg", fs);
// Set image alignment
image1.Alignment = HorizontalAlignment.Right;
outlineElem1.AppendChildLast(image1);
outline1.AppendChildLast(outlineElem1);
page.AppendChildLast(outline1);
doc.AppendChildLast(page);
dataDir = dataDir + "BuildDocAndInsertImageUsingImageStream_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Image nodes
IList<Aspose.Note.Image> nodes = oneFile.GetChildNodes<Aspose.Note.Image>();
foreach (Aspose.Note.Image image in nodes)
{
using (MemoryStream stream = new MemoryStream(image.Bytes))
{
using (Bitmap bitMap = new Bitmap(stream))
{
// Save image bytes to a file
bitMap.Save(String.Format(dataDir + "{0}", Path.GetFileName(image.FileName)));
}
}
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Image nodes
IList<Aspose.Note.Image> images = oneFile.GetChildNodes<Aspose.Note.Image>();
foreach (Aspose.Note.Image image in images)
{
Console.WriteLine("Width: {0}", image.Width);
Console.WriteLine("Height: {0}", image.Height);
Console.WriteLine("OriginalWidth: {0}", image.OriginalWidth);
Console.WriteLine("OriginalHeight: {0}", image.OriginalHeight);
Console.WriteLine("FileName: {0}", image.FileName);
Console.WriteLine("LastModifiedTime: {0}", image.LastModifiedTime);
Console.WriteLine();
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
var document = new Document();
var page = new Page(document);
var image = new Image(document, dataDir + "image.jpg");
image.AlternativeTextTitle = "This is an image's title!";
image.AlternativeTextDescription = "And this is an image's description!";
page.AppendChildLast(image);
document.AppendChildLast(page);
dataDir = dataDir + "ImageAlternativeText_out.one";
document.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Load document from the stream.
Document doc = new Document(dataDir + "Aspose.one");
// Get the first page of the document.
Aspose.Note.Page page = doc.FirstChild;
// Load an image from the file.
Aspose.Note.Image image = new Aspose.Note.Image(doc, dataDir + "image.jpg");
// Change the image's size according to your needs (optional).
image.Width = 100;
image.Height = 100;
// Set the image's location in the page (optional).
image.VerticalOffset = 400;
image.HorizontalOffset = 100;
// Set image alignment
image.Alignment = HorizontalAlignment.Right;
// Add the image to the page.
page.AppendChildLast(image);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Initialize the new Document
Document doc = new Document() { AutomaticLayoutChangesDetectionEnabled = false };
// Initialize the new Page
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Default style for all text in the document.
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
page.Title = new Title(doc)
{
TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
};
// Append page node
doc.AppendChildLast(page);
// Save OneNote document in different formats, set text font size and detect layout changes manually.
doc.Save(dataDir + "ConsequentExportOperations_out.html");
doc.Save(dataDir + "ConsequentExportOperations_out.pdf");
doc.Save(dataDir + "ConsequentExportOperations_out.jpg");
textStyle.FontSize = 11;
doc.DetectLayoutChanges();
doc.Save(dataDir + "ConsequentExportOperations_out.bmp");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Initialize ImageSaveOptions object
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png);
// Set page index
opts.PageIndex = 1;
dataDir = dataDir + "ConvertSpecificPageToImage_out.png";
// Save the document as PNG.
oneFile.Save(dataDir, opts);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document doc = new Document(dataDir + "Aspose.one");
dataDir = dataDir + "SetOutputImageResolution_out.jpg";
// Save the document.
doc.Save(dataDir, new ImageSaveOptions(SaveFormat.Jpeg) { Quality = 100 });
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document doc = new Document(dataDir + "Aspose.one");
dataDir = dataDir + "SetOutputImageResolution_out.jpg";
// Save the document.
doc.Save(dataDir, new ImageSaveOptions(SaveFormat.Jpeg) { Resolution = 220 });
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Initialize Title class object
Title title = new Title(doc);
// Initialize TextStyle class object and set formatting properties
ParagraphStyle defaultTextStyle = new ParagraphStyle
{
FontColor = Color.Black,
FontName = "Arial",
FontSize = 10
};
RichText titleText = new RichText(doc)
{
Text = "Title!",
ParagraphStyle = defaultTextStyle
};
Outline outline = new Outline(doc)
{
VerticalOffset = 100,
HorizontalOffset = 100
};
OutlineElement outlineElem = new OutlineElement(doc);
// RunIndex = 5 means the style will be applied only to 0-4 characters. ("Hello")
TextStyle textStyleForHelloWord = new TextStyle
{
FontColor = Color.Red,
FontName = "Arial",
FontSize = 10,
RunIndex = 5,
};
// RunIndex = 13 means the style will be applied only to 5-12 characters. (" OneNote")
TextStyle textStyleForOneNoteWord = new TextStyle
{
FontColor = Color.Green,
FontName = "Calibri",
FontSize = 10,
IsItalic = true,
RunIndex = 13,
};
// RunIndex = 18 means the style will be applied only to 13-17 characters. (" text").
// Other characters ("!") will have the default style.
TextStyle textStyleForTextWord = new TextStyle
{
FontColor = Color.Blue,
FontName = "Arial",
FontSize = 15,
IsBold = true,
IsItalic = true,
RunIndex = 18,
};
RichText text = new RichText(doc)
{
Text = "Hello OneNote text!",
ParagraphStyle = defaultTextStyle,
Styles = { textStyleForHelloWord, textStyleForOneNoteWord, textStyleForTextWord }
};
title.TitleText = titleText;
// Set page title
page.Title = title;
// Add RichText node
outlineElem.AppendChildLast(text);
// Add OutlineElement node
outline.AppendChildLast(outlineElem);
// Add Outline node
page.AppendChildLast(outline);
// Add Page node
doc.AppendChildLast(page);
dataDir = dataDir + "CreateDocWithFormattedRichText_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Create an object of the Document class
Document doc = new Aspose.Note.Document();
// Initialize Page class object
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Default style for all text in the document.
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
// Set page title properties
page.Title = new Title(doc)
{
TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
};
// Append Page node in the document
doc.AppendChildLast(page);
dataDir = dataDir + "CreateDocWithPageTitle_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page(doc);
// Initialize Outline class object
Outline outline = new Outline(doc);
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
// Initialize TextStyle class object and set formatting properties
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
// Initialize RichText class object and apply text style
RichText text = new RichText(doc) { Text = "Hello OneNote text!", ParagraphStyle = textStyle };
// Add RichText node
outlineElem.AppendChildLast(text);
// Add OutlineElement node
outline.AppendChildLast(outlineElem);
// Add Outline node
page.AppendChildLast(outline);
// Add Page node
doc.AppendChildLast(page);
dataDir = dataDir + "CreateDocWithSimpleRichText_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Initialize OneNote document
Document doc = new Document();
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Default style for all text in the document.
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
page.Title = new Title(doc)
{
TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
};
doc.AppendChildLast(page);
dataDir = dataDir + "CreateAndSavePageRange_out.html";
// Save as HTML format
doc.Save(dataDir, new HtmlSaveOptions
{
PageCount = 1,
PageIndex = 0
});
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Initialize OneNote document
Document doc = new Document();
Aspose.Note.Page page = new Aspose.Note.Page(doc);
// Default style for all text in the document.
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
page.Title = new Title(doc)
{
TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
};
doc.AppendChildLast(page);
dataDir = dataDir + "CreateOneNoteDocAndSaveToHTML_out.html";
// Save as HTML format
doc.Save(dataDir);
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
var options = new HtmlSaveOptions()
{
ExportCss = ResourceExportType.ExportEmbedded,
ExportFonts = ResourceExportType.ExportEmbedded,
ExportImages = ResourceExportType.ExportEmbedded,
FontFaceTypes = FontFaceType.Ttf
};
var r = new MemoryStream();
document.Save(r, options);
// This code creates documentFolder containing document.html, css folder with style.css file, images folder with images and fonts folder with fonts.
// style.css file will contains at the end the following string "/* This line is appended to stream manually by user */"
var options = new HtmlSaveOptions()
{
FontFaceTypes = FontFaceType.Ttf
};
var savingCallbacks = new UserSavingCallbacks()
{
RootFolder = "documentFolder",
CssFolder = "css",
KeepCssStreamOpened = true,
ImagesFolder = "images",
FontsFolder = "fonts"
};
options.CssSavingCallback = savingCallbacks;
options.FontSavingCallback = savingCallbacks;
options.ImageSavingCallback = savingCallbacks;
if (!Directory.Exists(savingCallbacks.RootFolder))
{
Directory.CreateDirectory(savingCallbacks.RootFolder);
}
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
using (var stream = File.Create(Path.Combine(savingCallbacks.RootFolder, "document.html")))
{
document.Save(stream, options);
}
using (var writer = new StreamWriter(savingCallbacks.CssStream))
{
writer.WriteLine();
writer.WriteLine("/* This line is appended to stream manually by user */");
}
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
var options = new HtmlSaveOptions()
{
ExportCss = ResourceExportType.ExportEmbedded,
ExportFonts = ResourceExportType.ExportEmbedded,
ExportImages = ResourceExportType.ExportEmbedded,
FontFaceTypes = FontFaceType.Ttf
};
document.Save(dataDir + "document_out.html", options);
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Open the document we want to convert.
Document doc = new Document(dataDir + "Aspose.one");
// Create an object that inherits from the DocumentVisitor class.
MyOneNoteToTxtWriter myConverter = new MyOneNoteToTxtWriter();
// This is the well known Visitor pattern. Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
//
// Note that every node in the object model has the Accept method so the visiting
// can be executed not only for the whole document, but for any node in the document.
doc.Accept(myConverter);
// Once the visiting is complete, we can retrieve the result of the operation,
// that in this example, has accumulated in the visitor.
Console.WriteLine(myConverter.GetText());
Console.WriteLine(myConverter.NodeCount);
}
/// <summary>
/// Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
/// </summary>
public class MyOneNoteToTxtWriter : DocumentVisitor
{
public MyOneNoteToTxtWriter()
{
nodecount = 0;
mIsSkipText = false;
mBuilder = new StringBuilder();
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public string GetText()
{
return mBuilder.ToString();
}
/// <summary>
/// Adds text to the current output. Honors the enabled/disabled output flag.
/// </summary>
private void AppendText(string text)
{
if (!mIsSkipText)
mBuilder.Append(text);
}
/// <summary>
/// Called when a RichText node is encountered in the document.
/// </summary>
public override void VisitRichTextStart(RichText run)
{
++nodecount;
AppendText(run.Text);
}
/// <summary>
/// Called when a Document node is encountered in the document.
/// </summary>
public override void VisitDocumentStart(Document document)
{
++nodecount;
}
/// <summary>
/// Called when a Page node is encountered in the document.
/// </summary>
public override void VisitPageStart(Page page)
{
++nodecount;
}
/// <summary>
/// Called when a Title node is encountered in the document.
/// </summary>
public override void VisitTitleStart(Title title)
{
++nodecount;
}
/// <summary>
/// Called when a Image node is encountered in the document.
/// </summary>
public override void VisitImageStart(Image image)
{
++nodecount;
}
/// <summary>
/// Called when a OutlineGroup node is encountered in the document.
/// </summary>
public override void VisitOutlineGroupStart(OutlineGroup outlineGroup)
{
++nodecount;
}
/// <summary>
/// Called when a Outline node is encountered in the document.
/// </summary>
public override void VisitOutlineStart(Outline outline)
{
++nodecount;
}
/// <summary>
/// Called when a OutlineElement node is encountered in the document.
/// </summary>
public override void VisitOutlineElementStart(OutlineElement outlineElement)
{
++nodecount;
}
/// <summary>
/// Gets the total count of nodes by the Visitor
/// </summary>
public Int32 NodeCount
{
get { return this.nodecount; }
}
private readonly StringBuilder mBuilder;
private bool mIsSkipText;
private Int32 nodecount;
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string fileName = "Open Notebook.onetoc2";
if (fileName != "")
{
try
{
var notebook = new Notebook(dataDir + fileName);
foreach (var notebookChildNode in notebook)
{
Console.WriteLine(notebookChildNode.DisplayName);
if (notebookChildNode is Document)
{
// Do something with child document
}
else if (notebookChildNode is Notebook)
{
// Do something with child notebook
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
Console.WriteLine("\nPlease enter valid file name.");
}
// The path to the documents directory.
Document doc = new Document();
// Returns NodeType.Document
NodeType type = doc.NodeType;
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(100);
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(400);
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
var pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.PageSplittingAlgorithm = new AlwaysSplitObjectsAlgorithm();
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm();
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
float heightLimitOfClonedPart = 500;
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(heightLimitOfClonedPart);
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(heightLimitOfClonedPart);
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(100);
// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(400);
var pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.PageSplittingAlgorithm = new AlwaysSplitObjectsAlgorithm();
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm();
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();
float heightLimitOfClonedPart = 500;
pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(heightLimitOfClonedPart);
// Or
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(heightLimitOfClonedPart);
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(100);
pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(400);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
LoadOptions loadOptions = new LoadOptions
{
DocumentPassword = "password"
};
Document doc = new Document(dataDir + "Sample1.one", loadOptions);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
switch (document.FileFormat)
{
case FileFormat.OneNote2010:
// Process OneNote 2010
break;
case FileFormat.OneNoteOnline:
// Process OneNote Online
break;
}
string inputFile = "Sample1.one";
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string outputFile = "SaveDocToOneNoteFormat_out.one";
Document doc = new Document(dataDir + inputFile);
doc.Save(dataDir + outputFile);
string inputFile = "Sample1.one";
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string outputFile = "SaveDocToOneNoteFormatUsingOnesaveoptions_out.one";
Document document = new Document(dataDir + inputFile);
document.Save(dataDir + outputFile, new OneSaveOptions());
string inputFile = "Sample1.one";
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string outputFile = "SaveDocToOneNoteFormatUsingSaveFormat_out.one";
Document document = new Document(dataDir + inputFile);
document.Save(dataDir + outputFile, SaveFormat.One);
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Initialize PdfSaveOptions object
PdfSaveOptions opts = new PdfSaveOptions();
// Set page index
opts.PageIndex = 0;
// Set page count
opts.PageCount = 1;
dataDir = dataDir + "SaveRangeOfPagesAsPDF_out.pdf";
// Save the document as PDF
oneFile.Save(dataDir, opts);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
dataDir = dataDir + "SaveToImageDefaultOptions_out.gif";
// Save the document as gif.
oneFile.Save(dataDir, SaveFormat.Gif);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document doc = new Document(dataDir + "Aspose.one");
MemoryStream dstStream = new MemoryStream();
doc.Save(dstStream, SaveFormat.Pdf);
// Rewind the stream position back to zero so it is ready for next reader.
dstStream.Position = 0;
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
dataDir = dataDir + "SaveWithDefaultSettings_out.pdf";
// Save the document as PDF
oneFile.Save(dataDir, SaveFormat.Pdf);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document doc = new Document(dataDir + "Aspose.one");
// Initialize PdfSaveOptions object
PdfSaveOptions opts = new PdfSaveOptions();
// Set page index
opts.PageIndex = 2;
// Set page count
opts.PageCount = 3;
//specify compression if required
opts.ImageCompression = Saving.Pdf.PdfImageCompression.Jpeg;
opts.JpegQuality = 90;
dataDir = dataDir + "Document.SaveWithOptions_out.pdf";
doc.Save(dataDir, opts);
// For complete examples and data files, please go to https://github.com/aspose-note/Aspose.Note-for-.NET
class UserSavingCallbacks : ICssSavingCallback, IFontSavingCallback, IImageSavingCallback
{
public string RootFolder { get; set; }
public bool KeepCssStreamOpened { get; set; }
public string CssFolder { get; set; }
public Stream CssStream { get; private set; }
public string FontsFolder { get; set; }
public string ImagesFolder { get; set; }
public void FontSaving(FontSavingArgs args)
{
string uri;
Stream stream;
this.CreateResourceInFolder(this.FontsFolder, args.FileName, out uri, out stream);
args.Stream = stream;
args.Uri = Path.Combine("..", uri).Replace("\\", "\\\\");
}
public void CssSaving(CssSavingArgs args)
{
string uri;
Stream stream;
this.CreateResourceInFolder(this.CssFolder, args.FileName, out uri, out stream);
args.Stream = this.CssStream = stream;
args.KeepStreamOpen = this.KeepCssStreamOpened;
args.Uri = uri;
}
public void ImageSaving(ImageSavingArgs args)
{
string uri;
Stream stream;
this.CreateResourceInFolder(this.ImagesFolder, args.FileName, out uri, out stream);
args.Stream = stream;
args.Uri = uri;
}
private void CreateResourceInFolder(string folder, string filename, out string uri, out Stream stream)
{
var relativePath = folder;
var fullPath = Path.Combine(this.RootFolder, relativePath);
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
stream = File.Create(Path.Combine(fullPath, filename));
uri = Path.Combine(relativePath, filename);
}
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
// Append a new child to the Notebook
notebook.AppendChild(new Document(dataDir + "Neuer Abschnitt 1.one"));
dataDir = dataDir + "AddChildNode_out.onetoc2";
// Save the Notebook
notebook.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
dataDir = dataDir + "ConvertToImage_out.png";
// Save the Notebook
notebook.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch öffnen.onetoc2");
var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
documentSaveOptions.Resolution = 400;
notebookSaveOptions.Flatten = true;
dataDir = dataDir + "ConvertToImageAsFlattenedNotebook_out.png";
// Save the Notebook
notebook.Save(dataDir, notebookSaveOptions);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
documentSaveOptions.Resolution = 400;
dataDir = dataDir + "ConvertToImageWithOptions_out.png";
// Save the Notebook
notebook.Save(dataDir, notebookSaveOptions);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
dataDir = dataDir + "ConvertToPDF_out.pdf";
// Save the Notebook
notebook.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
dataDir = dataDir + "ConvertToPDFAsFlattened_out.pdf";
// Save the Notebook
notebook.Save(
dataDir,
new NotebookPdfSaveOptions
{
Flatten = true
});
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
var notebookSaveOptions = new NotebookPdfSaveOptions();
var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
documentSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();
dataDir = dataDir + "ConvertToPDF_out.pdf";
// Save the Notebook
notebook.Save(dataDir, notebookSaveOptions);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
var notebook = new Notebook();
dataDir = dataDir + "test_out.onetoc2";
// Save the Notebook
notebook.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
Document document = new Document();
document.Save(dataDir + "CreatingPasswordProtectedDoc_out.one", new OneSaveOptions() { DocumentPassword = "pass" });
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
FileStream stream = new FileStream(dataDir + "Notizbuch öffnen.onetoc2", FileMode.Open);
var notebook = new Notebook(stream);
FileStream childStream = new FileStream(dataDir + "Aspose.one", FileMode.Open);
notebook.LoadChildDocument(childStream);
notebook.LoadChildDocument(dataDir + "Sample1.one");
string inputFile = "Notizbuch öffnen.onetoc2";
string dataDir = RunExamples.GetDataDir_NoteBook();
// By default children loading is "lazy".
Notebook notebook = new Notebook(dataDir + inputFile);
foreach (INotebookChildNode notebookChildNode in notebook) {
// Actual loading of the child document happens only here.
if (notebookChildNode is Document) {
// Do something with child document
}
}
// By default children loading is "lazy".
// Therefore for instant loading has took place,
// it is necessary to set the NotebookLoadOptions.InstantLoading flag.
NotebookLoadOptions loadOptions = new NotebookLoadOptions();
loadOptions.InstantLoading = true;
String inputFile = "Notizbuch öffnen.onetoc2";
String dataDir = RunExamples.GetDataDir_NoteBook();
Notebook notebook = new Notebook(dataDir + inputFile, loadOptions);
// All child documents are already loaded.
foreach (INotebookChildNode notebookChildNode in notebook) {
if (notebookChildNode is Document) {
// Do something with child document
}
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
var notebook = new Notebook(dataDir + "test.onetoc2", new NotebookLoadOptions() { DeferredLoading = true });
notebook.LoadChildDocument(dataDir + "Aspose.one");
notebook.LoadChildDocument(dataDir + "Locked Pass1.one", new LoadOptions() { DocumentPassword = "pass" });
notebook.LoadChildDocument(dataDir + "Locked Pass2.one", new LoadOptions() { DocumentPassword = "pass2" });
string inputFile = "notebook.onetoc2";
string dataDir = RunExamples.GetDataDir_NoteBook();
Notebook rootNotebook = new Notebook(dataDir + inputFile);
IList<RichText> allRichTextNodes = rootNotebook.GetChildNodes<RichText>();
foreach (RichText richTextNode in allRichTextNodes) {
Console.WriteLine(richTextNode.Text);
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
// Load a OneNote Notebook
var notebook = new Notebook(dataDir + "test.onetoc2");
// Traverse through its child nodes for searching the desired child item
foreach (var child in new List<INotebookChildNode>(notebook))
{
if (child.DisplayName == "Remove Me")
{
// Remove the Child Item from the Notebook
notebook.RemoveChild(child);
}
}
dataDir = dataDir + "RemoveChildNode_out.onetoc2";
// Save the Notebook
notebook.Save(dataDir);
string inputFile = "notebook.onetoc2";
string dataDir = RunExamples.GetDataDir_NoteBook();
Notebook rootNotebook = new Notebook(dataDir + inputFile);
IList<Document> allDocuments = rootNotebook.GetChildNodes<Document>();
foreach (Document document in allDocuments) {
Console.WriteLine(document.DisplayName);
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
var notebook = new Notebook();
MemoryStream stream = new MemoryStream();
notebook.Save(stream);
if (notebook.Count > 0)
{
var childDocument0 = notebook[0] as Document;
MemoryStream childStream = new MemoryStream();
childDocument0.Save(childStream);
var childDocument1 = notebook[1] as Document;
childDocument0.Save(dataDir + "SaveNotebookToStream_out.one");
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_NoteBook();
var notebook = new Notebook(dataDir + "notebook2.onetoc2", new NotebookLoadOptions() { DeferredLoading = false });
notebook.Save(dataDir + "notebook_out.onetoc2", new NotebookOneSaveOptions() { DeferredSaving = true });
if (notebook.Count > 0)
{
var childDocument0 = notebook[0] as Document;
childDocument0.Save(dataDir + "Not Locked_out.one");
var childDocument1 = notebook[1] as Document;
childDocument1.Save(dataDir + "Locked Pass1_out.one", new OneSaveOptions() { DocumentPassword = "pass" });
var childDocument2 = notebook[2] as Document;
childDocument2.Save(dataDir + "Locked Pass2_out.one", new OneSaveOptions() { DocumentPassword = "pass2" });
}
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document
Document doc = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
var history = doc.GetPageHistory(doc.FirstChild);
for (int i = 0; i < history.Count; i++)
{
var historyPage = history[i];
Console.Write(" {0}. Author: {1}, {2:dd.MM.yyyy hh.mm.ss}",
i,
historyPage.PageContentRevisionSummary.AuthorMostRecent,
historyPage.PageContentRevisionSummary.LastModifiedTime);
Console.WriteLine(historyPage.IsConflictPage ? ", IsConflict: true" : string.Empty);
// By default conflict pages are just skipped on saving.
// If mark it as non-conflict then it will be saved as usual one in the history.
if (historyPage.IsConflictPage)
historyPage.IsConflictPage = false;
}
doc.Save(dataDir + "ConflictPageManipulation_out.one", SaveFormat.One);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object and set its level
Aspose.Note.Page page1 = new Aspose.Note.Page(doc) { Level = 1 };
// Initialize Page class object and set its level
Aspose.Note.Page page2 = new Aspose.Note.Page(doc) { Level = 2 };
// Initialize Page class object and set its level
Aspose.Note.Page page3 = new Aspose.Note.Page(doc) { Level = 1 };
/*---------- Adding nodes to first Page ----------*/
Outline outline = new Outline(doc);
OutlineElement outlineElem = new OutlineElement(doc);
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
RichText text = new RichText(doc) { Text = "First page.", ParagraphStyle = textStyle };
outlineElem.AppendChildLast(text);
outline.AppendChildLast(outlineElem);
page1.AppendChildLast(outline);
/*---------- Adding nodes to second Page ----------*/
var outline2 = new Outline(doc);
var outlineElem2 = new OutlineElement(doc);
var textStyle2 = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
var text2 = new RichText(doc) { Text = "Second page.", ParagraphStyle = textStyle2 };
outlineElem2.AppendChildLast(text2);
outline2.AppendChildLast(outlineElem2);
page2.AppendChildLast(outline2);
/*---------- Adding nodes to third Page ----------*/
var outline3 = new Outline(doc);
var outlineElem3 = new OutlineElement(doc);
var textStyle3 = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
var text3 = new RichText(doc) { Text = "Third page.", ParagraphStyle = textStyle3 };
outlineElem3.AppendChildLast(text3);
outline3.AppendChildLast(outlineElem3);
page3.AppendChildLast(outline3);
/*---------- Add pages to the OneNote Document ----------*/
doc.AppendChildLast(page1);
doc.AppendChildLast(page2);
doc.AppendChildLast(page3);
dataDir = dataDir + "CreateDocWithRootAndSubPages_out.one";
// Save OneNote document
doc.Save(dataDir);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get number of pages
int count = oneFile.GetChildNodes<Page>().Count;
// Print count on the output screen
Console.WriteLine(count);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Page nodes
IList<Page> pages = oneFile.GetChildNodes<Page>();
foreach (Page page in pages)
{
Console.WriteLine("LastModifiedTime: {0}", page.LastModifiedTime);
Console.WriteLine("CreationTime: {0}", page.CreationTime);
Console.WriteLine("Title: {0}", page.Title);
Console.WriteLine("Level: {0}", page.Level);
Console.WriteLine("Author: {0}", page.Author);
Console.WriteLine();
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document
Document document = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
// Get first page
Page firstPage = document.FirstChild;
foreach (Page pageRevision in document.GetPageHistory(firstPage))
{
/*Use pageRevision like a regular page.*/
Console.WriteLine("LastModifiedTime: {0}", pageRevision.LastModifiedTime);
Console.WriteLine("CreationTime: {0}", pageRevision.CreationTime);
Console.WriteLine("Title: {0}", pageRevision.Title);
Console.WriteLine("Level: {0}", pageRevision.Level);
Console.WriteLine("Author: {0}", pageRevision.Author);
Console.WriteLine();
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document and get first child
Document document = new Document(dataDir + "Aspose.one");
Page page = document.FirstChild;
var pageHistory = document.GetPageHistory(page);
pageHistory.RemoveRange(0, 1);
pageHistory[0] = new Page(document);
if (pageHistory.Count > 1)
{
pageHistory[1].Title.TitleText.Text = "New Title";
pageHistory.Add(new Page(document));
pageHistory.Insert(1, new Page(document));
document.Save(dataDir + "ModifyPageHistory_out.one");
}
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document
Document document = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
//Clone into new document without history
var cloned = new Document();
cloned.AppendChildLast(document.FirstChild.Clone());
//Clone into new document with history
cloned = new Document();
cloned.AppendChildLast(document.FirstChild.Clone(true));
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document and get first child
Document document = new Document(dataDir + "Aspose.one");
Page page = document.FirstChild;
var pageHistory = document.GetPageHistory(page);
pageHistory.Add(page.Clone());
document.Save(dataDir + "PushCurrentPageVersion_out.one");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document and get first child
Document document = new Document(dataDir + "Aspose.one");
Page page = document.FirstChild;
Page lastPage = null;
foreach (Page pageRevision in document.GetPageHistory(page))
{
lastPage = pageRevision;
}
document.RemoveChild(page);
document.AppendChildLast(lastPage);
document.Save(dataDir + "RollBackRevisions_out.one");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load OneNote document and get first child
Document document = new Document(dataDir + "Aspose.one");
Page page = document.FirstChild;
// Reading Content Revision Summary for this page
var pageRevisionInfo = page.PageContentRevisionSummary;
Console.WriteLine(string.Format(
"Author:\t{0}\nModified:\t{1}",
pageRevisionInfo.AuthorMostRecent,
pageRevisionInfo.LastModifiedTime.ToString("dd.MM.yyyy HH:mm:ss")));
// Update Page Revision Summary for this page
pageRevisionInfo.AuthorMostRecent = "New Author";
pageRevisionInfo.LastModifiedTime = DateTime.Now;
document.Save(dataDir + "WorkingWithPageRevisions_out.one");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
document.Print();
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
var document = new Aspose.Note.Document(dataDir + "Aspose.one");
var printerSettings = new PrinterSettings() { FromPage = 0, ToPage = 10 };
printerSettings.DefaultPageSettings.Landscape = true;
printerSettings.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(50, 50, 150, 50);
document.Print(new PrintOptions()
{
PrinterSettings = printerSettings,
Resolution = 1200,
PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(),
DocumentName = "Test.one"
});
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Tables();