Office OneNote文件处理API-Aspose.Note功能演示:在C#中以编程方式创建OneNote(.ONE)文档

Aspose.Note for .NET是功能丰富的OneNote文档处理API,可让您使用C#或VB.NET以编程方式创建,读取和转换OneNote文档。本文,将介绍如何使用C#从头开始创建OneNote文档。

Aspose.Note for .NET是功能丰富的OneNote文档处理API,可让您使用C#或VB.NET以编程方式创建,读取和转换OneNote文档。

MS OneNote为您提供了一种以数字便笺(.ONE)形式组织和管理信息的方法。OneNote文档中的页面用于包含用户定义的内容,该内容可能包含文本,表格,图像,列表等。在本文中,将介绍在OneNote文档中创建页面及其内容的所有基本方面。包括:

  • 创建一个空的OneNote文档
  • 将页面添加到OneNote文档
  • 将图像插入OneNote文档
  • 在OneNote文档中添加表
  • 在OneNote文档中添加标签
  • 在OneNote文档中插入超链接

Aspose.Note for .NET已升级至V20.5,如果你还没有用过Aspose.Tasks可以点击这里下载最新版测试。


使用C#创建OneNote(.ONE)文档

让我们从创建一个仅包含页面标题的空OneNote文档开始。以下是创建空白页的OneNote文档并将其另存为.one文件的步骤。

  • 创建Document类的实例。
  • 通过使用Document类的对象初始化Page对象来创建新页面。
  • 使用Page.Title属性设置页面标题。
  • 调用Document.AppendChild()方法并传递Page对象。
  • 使用Document.Save()方法保存OneNote文档。

下面的代码示例演示如何使用C#创建一个空的OneNote文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_LoadingAndSaving();// Create an object of the Document classDocument doc = new Aspose.Note.Document();// Initialize Page class objectAspose.Note.Page page = new Aspose.Note.Page(doc);// Default style for all text in the document.Aspose.Note.TextStyle textStyle = new TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };// Set page title propertiespage.Title = new Title(doc){    TitleText = new RichText(doc) { Text = "Title text.", DefaultStyle = textStyle },    TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), DefaultStyle = textStyle },    TitleTime = new RichText(doc) { Text = "12:34", DefaultStyle = textStyle }};// Append Page node in the documentdoc.AppendChildLast(page);dataDir = dataDir + "CreateDocWithPageTitle_out.one";// Save OneNote documentdoc.Save(dataDir);

使用C#将页面添加到OneNote文档

OneNote文档中的页面可以是主页,也可以是子页面。例如,您要创建一个年度报告,其中进一步包含月度报告的小节。在这种情况下,可以将报告的描述放在主页上,将月度报告放在子页面上。在OneNote文档中,可以使用页面级别进行处理。

以下是在OneNote文档中创建页面的步骤。

  • 使用Document类创建一个新的OneNote文档。
  • 为文档创建新页面,并使用Page类设置其级别。
  • 使用Outline和OutlineElement类创建注释。
  • 使用Page.AppendChildLast()方法向页面添加注释。
  • 使用Document.AppendChildLast()方法将页面添加到OneNote文档中。
  • 使用Document.Save()方法保存文档。

下面的代码示例演示如何使用C#创建页面并将页面添加到OneNote文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Pages();// Create an object of the Document classDocument doc = new Document();// Initialize Page class object and set its levelAspose.Note.Page page1 = new Aspose.Note.Page(doc) { Level = 1 };// Initialize Page class object and set its levelAspose.Note.Page page2 = new Aspose.Note.Page(doc) { Level = 2 };// Initialize Page class object and set its levelAspose.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);TextStyle textStyle = new TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };RichText text = new RichText(doc) { Text = "First page.", DefaultStyle = 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 TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };var text2 = new RichText(doc) { Text = "Second page.", DefaultStyle = 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 TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };var text3 = new RichText(doc) { Text = "Third page.", DefaultStyle = 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 documentdoc.Save(dataDir);

使用C#将图像插入OneNote文档

可以将图像插入OneNote页面。以下是创建具有图像的OneNote文档的步骤。

  • 为新的OneNote文档创建Document类的实例。
  • 使用Page类为文档创建一个新页面。
  • 创建Image类的新实例,并使用图像的路径对其进行初始化。
  • 使用OutlineElement.AppendChildLast()方法将图像添加到页面。
  • 使用Document.AppendChildLast()方法将页面添加到文档中。
  • 保存OneNote文档。

下面的代码示例演示如何使用C#将图像插入OneNote文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Images();// Create an object of the Document classDocument doc = new Document();// Initialize Page class objectAspose.Note.Page page = new Aspose.Note.Page(doc);// Initialize Outline class object and set offset propertiesOutline outline = new Outline(doc) { VerticalOffset = 0, HorizontalOffset = 0 };// Initialize OutlineElement class objectOutlineElement 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 alignmentimage.Alignment = HorizontalAlignment.Right;// Add imageoutlineElem.AppendChildLast(image);// Add outline elementsoutline.AppendChildLast(outlineElem);// Add Outline nodepage.AppendChildLast(outline);// Add Page nodedoc.AppendChildLast(page);dataDir = dataDir + "BuildDocAndInsertImage_out.one";// Save OneNote documentdoc.Save(dataDir);

使用C#将表添加到OneNote文档

表是一种以行和列的形式组织和汇总数据的好方法。通过有效的组织,表格使您可以快速找到所需的数据。OneNote文档还支持表格。以下是在OneNote文档中添加表的步骤。

  • 使用Document类创建一个新的OneNote文档。
  • 使用Page类创建一个新页面。
  • 分别使用TableRow和TableCell类添加新的表行和表单元格。
  • 使用TableCell.AppendChildLast方法将元素插入到表格单元格中。
  • 使用TableRow.AppendChildLast()方法将单元格添加到表行。
  • 创建Table类的实例,然后向其中添加表行。
  • 使用OutlineElement和Outline类将表添加到页面。
  • 将页面添加到文档,并使用Document.Save()方法保存文档。

下面的代码示例演示如何使用C#将表添加到OneNote文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Tables();// Create an object of the Document classDocument doc = new Document();// Initialize Page class objectAspose.Note.Page page = new Aspose.Note.Page(doc);// Initialize TableRow class objectTableRow row1 = new TableRow(doc);// Initialize TableCell class objectsTableCell cell11 = new TableCell(doc);TableCell cell12 = new TableCell(doc);TableCell cell13 = new TableCell(doc);// Append outline elements in the table cellcell11.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.1"));cell12.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.2"));cell13.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.3"));// Table cells to rowsrow1.AppendChildLast(cell11);row1.AppendChildLast(cell12);row1.AppendChildLast(cell13);// Initialize TableRow class objectTableRow row2 = new TableRow(doc);// initialize TableCell class objectsTableCell cell21 = new TableCell(doc);TableCell cell22 = new TableCell(doc);TableCell cell23 = new TableCell(doc);// Append outline elements in the table cellcell21.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.1"));cell22.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.2"));cell23.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.3"));// Append table cells to rowsrow2.AppendChildLast(cell21);row2.AppendChildLast(cell22);row2.AppendChildLast(cell23);// Initialize Table class object and set column widthsTable table = new Table(doc){    IsBordersVisible = true,    Columns = { new TableColumn { Width = 200 }, new TableColumn { Width = 200 }, new TableColumn { Width = 200 } }};// Append table rows to tabletable.AppendChildLast(row1);table.AppendChildLast(row2);// Initialize Outline objectOutline outline = new Outline(doc);// Initialize OutlineElement objectOutlineElement outlineElem = new OutlineElement(doc);// Add table to outline element nodeoutlineElem.AppendChildLast(table);// Add outline element to outlineoutline.AppendChildLast(outlineElem);// Add outline to page nodepage.AppendChildLast(outline);// Add page to document nodedoc.AppendChildLast(page);dataDir = dataDir + "InsertTable_out.one";doc.Save(dataDir);

使用C#在OneNote文档中插入标签

标记用于对OneNote文档中的笔记进行分类或优先级排序。您可以将标签应用于文本或段落,以快速查找或过滤相关注释。以下是在OneNote文档中创建和添加标签的步骤。

  • 创建一个Document类的实例。
  • 使用Page类创建一个新页面。
  • 初始化Outline和OutlineElement对象。
  • 创建一个新的RichText对象并设置其文本和其他属性。
  • 使用RichText.Tags.Add()方法向文本添加标签。
  • 在页面上添加文字。
  • 将页面添加到OneNote文档并保存。

以下代码示例显示了如何使用C#将标签添加到OneNote文档中的文本。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Tags();// Create an object of the Document classDocument doc = new Document();// Initialize Page class objectAspose.Note.Page page = new Aspose.Note.Page(doc);// Initialize Outline class objectOutline outline = new Outline(doc);// Initialize OutlineElement class objectOutlineElement outlineElem = new OutlineElement(doc);TextStyle textStyle = new TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };RichText text = new RichText(doc) { Text = "OneNote text.", DefaultStyle = textStyle };text.Tags.Add(new NoteTag{    Icon = TagIcon.YellowStar,});// Add text nodeoutlineElem.AppendChildLast(text);// Add outline element nodeoutline.AppendChildLast(outlineElem);// Add outline nodepage.AppendChildLast(outline);// Add page nodedoc.AppendChildLast(page);dataDir = dataDir + "AddTextNodeWithTag_out.one";// Save OneNote documentdoc.Save(dataDir);

使用C#在OneNote文档中插入超链接

让我们看看如何使用以下步骤将超链接插入到OneNote文档。

  • 创建Document类的实例。
  • 使用Page类为文档创建一个新页面。
  • 使用TextStyle类设置超链接的文本样式及其URL。
  • 创建RichText类的实例,添加其文本并使用先前为超链接创建的文本样式对其进行初始化。
  • 将文本添加到OneNote文档的页面。
  • 将文档另存为.one文件。

下面的代码示例演示如何使用C#将超链接插入到OneNote文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Tasks();// Create an object of the Document classDocument doc = new Document();// Initialize Page class objectAspose.Note.Page page = new Aspose.Note.Page(doc);// Initialize Title class objectTitle title = new Title(doc);TextStyle defaultTextStyle = new TextStyle{    FontName = "Arial",    FontSize = 10,    FontColor = SystemColors.WindowText};RichText titleText = new RichText(doc){    Text = "Title!",    DefaultStyle = defaultTextStyle};Outline outline = new Outline(doc){    MaxWidth = 200,    MaxHeight = 200,    VerticalOffset = 100,    HorizontalOffset = 100};OutlineElement outlineElem = new OutlineElement(doc);TextStyle textStyleRed = new TextStyle{    FontColor = Color.Red,    FontName = "Arial",    FontSize = 10,    RunIndex = 8//this style will be applied to 0-7 characters.};TextStyle textStyleHyperlink = new TextStyle{    RunIndex = 17,//this style will be applied to 8-16 characters.    IsHyperlink = true,    HyperlinkAddress = "www.google.com"};RichText text = new RichText(doc){    Text = "This is hyperlink. This text is not a hyperlink.",    DefaultStyle = defaultTextStyle,    Styles = { textStyleRed, textStyleHyperlink }};title.TitleText = titleText;page.Title = title;outlineElem.AppendChildLast(text);// Add outline elementsoutline.AppendChildLast(outlineElem);// Add Outline nodepage.AppendChildLast(outline);// Add Page nodedoc.AppendChildLast(page);dataDir = dataDir + "AddHyperlink_out.one";// Save OneNote documentdoc.Save(dataDir);


还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年4月6日
下一篇 2020年4月6日

相关推荐

发表回复

登录后才能评论