docx icon indicating copy to clipboard operation
docx copied to clipboard

How to create an external hyperlink

Open crespowang opened this issue 5 years ago • 12 comments

I can't find any documents around it, I could see an example from https://github.com/dolanmiu/docx/blob/master/demo/35-hyperlinks.ts but it seems out of date.

crespowang avatar Mar 03 '21 02:03 crespowang

hi @crespowang I was able to get the hyperlinks working by following:

const { Hyperlink }  = docx;
new Paragraph({
        children: [
            new Hyperlink(anchor_text, "", "http://google.com")
        ],
})

bmmule avatar Mar 03 '21 09:03 bmmule

demo 35 is up to date with master, but not in 5.5.0

demo 35 way will be the way going forward in 6.0.0

dolanmiu avatar Mar 03 '21 15:03 dolanmiu

I have a similar question. I can create the link:

image

but when I do Link -> Edit in word, the address doesn't show up:

image

here's the code:

    const { Document, Paragraph, Hyperlink } = require('docx');
    const doc = new Document();
    doc.addSection({
      children: [
        new Paragraph({
          children: [
            new Hyperlink('This Is A Link', '', 'https://google.com/')
          ],
        })
      ],
    });

Also, the links doesn't work, when I click open.

tripodsan avatar Mar 16 '21 10:03 tripodsan

The feature isn't released yet

dolanmiu avatar Mar 16 '21 17:03 dolanmiu

The feature isn't released yet

Could you please explain how to create an external hyperlink with release 5.5.0?

crespowang avatar Mar 17 '21 21:03 crespowang

there is a big discrepancy between what is released, what is available in the API and what is documented.... I have a hard time to figure out how stuff works.....

tripodsan avatar Mar 18 '21 01:03 tripodsan

Sorry guys, 6.0.0 will be released soon, if you guys want 5.5.0 related examples, then just go to the 5.5.0 tag:

https://github.com/dolanmiu/docx/tree/5.5.0

dolanmiu avatar Mar 18 '21 01:03 dolanmiu

Guys I've found out how to do this

import * as fs from "fs";
import { Document, HyperlinkRef, HyperlinkType, Packer, Paragraph, Media } from "../build";

const doc = new Document({
    hyperlinks: {
        myCoolLink: {
            link: "http://www.example.com",
            text: "Hyperlink",
            type: HyperlinkType.EXTERNAL,
        },
        myOtherLink: {
            link: "http://www.google.com",
            text: "Google Link",
            type: HyperlinkType.EXTERNAL,
        },
    },
});

const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));

doc.addSection({
    children: [
        new Paragraph({
            children: [new HyperlinkRef("myCoolLink")],
        }),
        new Paragraph({
            children: [image1, new HyperlinkRef("myOtherLink")],
        }),
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

The problem is that you can't add hyperlink on the fly. You need to know what links will be there when you create the document.

It would be nice to have something like this, so that you can create links with the Paragraph rather than the Document

const { Hyperlink }  = docx;
new Paragraph({
        children: [
            new Hyperlink(anchor_text, "", "http://google.com")
        ],
})

Hopefully it's in 6.0.0 release.

crespowang avatar Mar 18 '21 03:03 crespowang

I'm using version 6.0.3, and the following doesn't seem to work - I get empty text where the hyperlink should be. The TextRuns before and after the hyperlink appear fine. Any idea?

new Paragraph({
                    children: [ 
                        new TextRun({ text: 'A', size: 22, font: 'Montserrat Light' }),
                        new ExternalHyperlink({
                            children: [
                                new TextRun({
                                    text: 'link to google',
                                    style: "Hyperlink",
                                    size: 22, font: 'Montserrat Light'
                                }),
                            ],
                            link: 'http://www.google.com'
                        }),                       
                        new TextRun({ text: `B`, size: 22, font: 'Montserrat Light' })
                    ]
})

Metheny80 avatar Sep 01 '21 16:09 Metheny80

At the moment, ExternalHyperlink's take a single child in 6.0, in v7 it will allow multiple children

dolanmiu avatar Sep 02 '21 03:09 dolanmiu

My code includes only a single child , but it doesn't seem to work. Is this the correct syntax?

Metheny80 avatar Sep 02 '21 04:09 Metheny80

Try similar to this:

const hyperlink = new ExternalHyperlink({
    child: new TextRun({
        text: 'link to google',
        style: "Hyperlink",
        size: 22, font: 'Montserrat Light'
    }),
    link: 'http://www.google.com'
});

The example from this point in time should likely work: https://github.com/dolanmiu/docx/blob/3299c557a0ff77dd07d7f14506b717b045fd6bba/demo/35-hyperlinks.ts

rowanc1 avatar Sep 02 '21 05:09 rowanc1