From 9d7fd55e4c3362a5d190b0fdaaf8cdbd3aa4b0e7 Mon Sep 17 00:00:00 2001 From: Ivan Lopez Date: Sun, 6 May 2018 22:24:16 -0500 Subject: [PATCH] add hyperlink to paragraph and doc --- src/file/file.ts | 14 +++++++++++++- src/file/paragraph/index.ts | 1 + src/file/paragraph/paragraph.ts | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/file/file.ts b/src/file/file.ts index 67393a8c6f..39a9e3d210 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -7,7 +7,7 @@ import { FooterWrapper } from "./footer-wrapper"; import { HeaderWrapper } from "./header-wrapper"; import { Media } from "./media"; import { Numbering } from "./numbering"; -import { Paragraph, PictureRun } from "./paragraph"; +import { Hyperlink, Paragraph, PictureRun } from "./paragraph"; import { Relationships } from "./relationships"; import { Styles } from "./styles"; import { DefaultStylesFactory } from "./styles/factory"; @@ -111,6 +111,18 @@ export class File { return this.document.createDrawing(mediaData); } + public createHyperlink(link: string, text?: string): Hyperlink { + text = text === undefined ? link : text; + const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount); + this.docRelationships.createRelationship( + hyperlink.linkId, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", + link, + "External", + ); + return hyperlink; + } + public get Document(): Document { return this.document; } diff --git a/src/file/paragraph/index.ts b/src/file/paragraph/index.ts index 08d4e487cf..68a1b0b800 100644 --- a/src/file/paragraph/index.ts +++ b/src/file/paragraph/index.ts @@ -2,3 +2,4 @@ export * from "./formatting"; export * from "./paragraph"; export * from "./properties"; export * from "./run"; +export * from "./links"; diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index 2c5e007a9b..d8659470b0 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -13,6 +13,7 @@ import { ISpacingProperties, Spacing } from "./formatting/spacing"; import { Style } from "./formatting/style"; import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop"; import { NumberProperties } from "./formatting/unordered-list"; +import { Hyperlink } from "./links"; import { ParagraphProperties } from "./properties"; export class Paragraph extends XmlComponent { @@ -32,6 +33,11 @@ export class Paragraph extends XmlComponent { return this; } + public addHyperLink(hyperlink: Hyperlink): Paragraph { + this.root.push(hyperlink); + return this; + } + public createTextRun(text: string): TextRun { const run = new TextRun(text); this.addRun(run);