翻译|使用教程|编辑:李显亮|2020-05-09 10:49:47.583|阅读 320 次
概述:Aspose.Note for .NET是功能丰富的OneNote文档处理API,可让您使用C#或VB.NET以编程方式创建,读取和转换OneNote文档。本文,将介绍如何使用C#从头开始创建OneNote文档。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
Aspose.Note for .NET是功能丰富的OneNote文档处理API,可让您使用C#或VB.NET以编程方式创建,读取和转换OneNote文档。
MS OneNote为您提供了一种以数字便笺(.ONE)形式组织和管理信息的方法。OneNote文档中的页面用于包含用户定义的内容,该内容可能包含文本,表格,图像,列表等。在本文中,将介绍在OneNote文档中创建页面及其内容的所有基本方面。包括:
Aspose.Note for .NET已升级至V20.5,如果你还没有用过Aspose.Tasks可以点击这里下载最新版测试。
让我们从创建一个仅包含页面标题的空OneNote文档开始。以下是创建空白页的OneNote文档并将其另存为.one文件的步骤。
下面的代码示例演示如何使用C#创建一个空的OneNote文档。
// 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. Aspose.Note.TextStyle textStyle = new TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 }; // Set page title properties page.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 document doc.AppendChildLast(page); dataDir = dataDir + "CreateDocWithPageTitle_out.one"; // Save OneNote document doc.Save(dataDir);
OneNote文档中的页面可以是主页,也可以是子页面。例如,您要创建一个年度报告,其中进一步包含月度报告的小节。在这种情况下,可以将报告的描述放在主页上,将月度报告放在子页面上。在OneNote文档中,可以使用页面级别进行处理。
以下是在OneNote文档中创建页面的步骤。
下面的代码示例演示如何使用C#创建页面并将页面添加到OneNote文档。
// 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); 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 document doc.Save(dataDir);
可以将图像插入OneNote页面。以下是创建具有图像的OneNote文档的步骤。
下面的代码示例演示如何使用C#将图像插入OneNote文档。
// 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);
表是一种以行和列的形式组织和汇总数据的好方法。通过有效的组织,表格使您可以快速找到所需的数据。OneNote文档还支持表格。以下是在OneNote文档中添加表的步骤。
下面的代码示例演示如何使用C#将表添加到OneNote文档。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Tables(); // 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 TableRow class object TableRow row1 = new TableRow(doc); // Initialize TableCell class objects TableCell cell11 = new TableCell(doc); TableCell cell12 = new TableCell(doc); TableCell cell13 = new TableCell(doc); // Append outline elements in the table cell cell11.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.1")); cell12.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.2")); cell13.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.3")); // Table cells to rows row1.AppendChildLast(cell11); row1.AppendChildLast(cell12); row1.AppendChildLast(cell13); // Initialize TableRow class object TableRow row2 = new TableRow(doc); // initialize TableCell class objects TableCell cell21 = new TableCell(doc); TableCell cell22 = new TableCell(doc); TableCell cell23 = new TableCell(doc); // Append outline elements in the table cell cell21.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.1")); cell22.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.2")); cell23.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.3")); // Append table cells to rows row2.AppendChildLast(cell21); row2.AppendChildLast(cell22); row2.AppendChildLast(cell23); // Initialize Table class object and set column widths Table table = new Table(doc) { IsBordersVisible = true, Columns = { new TableColumn { Width = 200 }, new TableColumn { Width = 200 }, new TableColumn { Width = 200 } } }; // Append table rows to table table.AppendChildLast(row1); table.AppendChildLast(row2); // Initialize Outline object Outline outline = new Outline(doc); // Initialize OutlineElement object OutlineElement outlineElem = new OutlineElement(doc); // Add table to outline element node outlineElem.AppendChildLast(table); // Add outline element to outline outline.AppendChildLast(outlineElem); // Add outline to page node page.AppendChildLast(outline); // Add page to document node doc.AppendChildLast(page); dataDir = dataDir + "InsertTable_out.one"; doc.Save(dataDir);
标记用于对OneNote文档中的笔记进行分类或优先级排序。您可以将标签应用于文本或段落,以快速查找或过滤相关注释。以下是在OneNote文档中创建和添加标签的步骤。
以下代码示例显示了如何使用C#将标签添加到OneNote文档中的文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Tags(); // 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); 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 node outlineElem.AppendChildLast(text); // Add outline element node outline.AppendChildLast(outlineElem); // Add outline node page.AppendChildLast(outline); // Add page node doc.AppendChildLast(page); dataDir = dataDir + "AddTextNodeWithTag_out.one"; // Save OneNote document doc.Save(dataDir);
让我们看看如何使用以下步骤将超链接插入到OneNote文档。
下面的代码示例演示如何使用C#将超链接插入到OneNote文档。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Tasks(); // 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); 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 elements outline.AppendChildLast(outlineElem); // Add Outline node page.AppendChildLast(outline); // Add Page node doc.AppendChildLast(page); dataDir = dataDir + "AddHyperlink_out.one"; // Save OneNote document doc.Save(dataDir);
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn