Working with Hyperlinks

A hyperlink is a text or an image user can click on, and jump to another document/URL. Aspose.Note API allows adding a hyperlink in the OneNote Document.

 1String dataDir = Utils.getSharedDataDir(AddHyperlink.class) + "hyperlinks/";
 2		
 3// create an object of the Document class
 4Document doc = new Document();
 5
 6// initialize Page class object
 7Page page = new Page(doc);
 8
 9// initialize Title class object
10Title title = new Title(doc);
11
12// initialize default text style
13ParagraphStyle defaultTextStyle = new ParagraphStyle();
14defaultTextStyle.setFontName("Arial");
15defaultTextStyle.setFontSize(10);
16defaultTextStyle.setFontColor(java.awt.Color.GRAY);
17
18RichText titleText = new RichText(doc);
19titleText.setText("Title");
20titleText.setParagraphStyle(defaultTextStyle);
21
22Outline outline = new Outline(doc);
23outline.setMaxWidth(200);
24outline.setMaxHeight(200);
25outline.setVerticalOffset(100);
26outline.setHorizontalOffset(100);
27
28OutlineElement outlineElem = new OutlineElement(doc);
29
30TextStyle textStyleRed = new TextStyle();
31textStyleRed.setFontName("Arial");
32textStyleRed.setFontSize(10);
33textStyleRed.setFontColor(java.awt.Color.red);
34textStyleRed.setRunIndex(8);
35
36TextStyle textStyleHyperlink = new TextStyle();
37textStyleHyperlink.setRunIndex(17);
38textStyleHyperlink.setHyperlink(true);
39textStyleHyperlink.setHyperlinkAddress("www.google.com");
40
41RichText text = new RichText(doc);
42text.setText("This is hyperlink. This text is not a hyperlink.");
43text.setParagraphStyle(defaultTextStyle);
44text.getStyles().addItem(textStyleRed);
45text.getStyles().addItem(textStyleHyperlink);
46
47title.setTitleText(titleText);
48page.setTitle(title);
49outlineElem.appendChildLast(text);
50
51// add outline elements
52outline.appendChildLast(outlineElem);
53
54// add Outline node
55page.appendChildLast(outline);
56
57// add Page node
58doc.appendChildLast(page);
59
60doc.save(dataDir + "AddHyperlink_out.pdf");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.