翻譯|使用教程|編輯:李顯亮|2020-05-09 10:49:47.583|閱讀 405 次
概述:Aspose.Note for .NET是功能豐富的OneNote文檔處理API,可讓您使用C#或VB.NET以編程方式創(chuàng)建,讀取和轉(zhuǎn)換OneNote文檔。本文,將介紹如何使用C#從頭開始創(chuàng)建OneNote文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Aspose.Note for .NET是功能豐富的OneNote文檔處理API,可讓您使用C#或VB.NET以編程方式創(chuàng)建,讀取和轉(zhuǎn)換OneNote文檔。
MS OneNote為您提供了一種以數(shù)字便箋(.ONE)形式組織和管理信息的方法。OneNote文檔中的頁面用于包含用戶定義的內(nèi)容,該內(nèi)容可能包含文本,表格,圖像,列表等。在本文中,將介紹在OneNote文檔中創(chuàng)建頁面及其內(nèi)容的所有基本方面。包括:
Aspose.Note for .NET已升級至V20.5,如果你還沒有用過Aspose.Tasks可以點擊這里下載最新版測試。
讓我們從創(chuàng)建一個僅包含頁面標(biāo)題的空OneNote文檔開始。以下是創(chuàng)建空白頁的OneNote文檔并將其另存為.one文件的步驟。
下面的代碼示例演示如何使用C#創(chuàng)建一個空的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文檔中的頁面可以是主頁,也可以是子頁面。例如,您要創(chuàng)建一個年度報告,其中進一步包含月度報告的小節(jié)。在這種情況下,可以將報告的描述放在主頁上,將月度報告放在子頁面上。在OneNote文檔中,可以使用頁面級別進行處理。
以下是在OneNote文檔中創(chuàng)建頁面的步驟。
下面的代碼示例演示如何使用C#創(chuàng)建頁面并將頁面添加到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頁面。以下是創(chuàng)建具有圖像的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);
表是一種以行和列的形式組織和匯總數(shù)據(jù)的好方法。通過有效的組織,表格使您可以快速找到所需的數(shù)據(jù)。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);
標(biāo)記用于對OneNote文檔中的筆記進行分類或優(yōu)先級排序。您可以將標(biāo)簽應(yīng)用于文本或段落,以快速查找或過濾相關(guān)注釋。以下是在OneNote文檔中創(chuàng)建和添加標(biāo)簽的步驟。
以下代碼示例顯示了如何使用C#將標(biāo)簽添加到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);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn