Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add text with a link in V1.x.x #161

Closed
Ashish-Tomar opened this issue Aug 12, 2019 · 4 comments
Closed

How to add text with a link in V1.x.x #161

Ashish-Tomar opened this issue Aug 12, 2019 · 4 comments

Comments

@Ashish-Tomar
Copy link

Hey @Hopding, Fantastic job with the library!

The code example to create text weblink under #45 doesn't work anymore due to changes in the object names and functions.

Would it be possible to provide an updated example?

Thanks in advance.

@Hopding
Copy link
Owner

Hopding commented Aug 20, 2019

Hello @Ashish-Tomar!

Here's the example from #45 updated for v1.x.x:

import { PDFDocument, PDFName, PDFString, rgb, StandardFonts } from 'pdf-lib';

/* (1) Create a new PDF document */
const pdfDoc = await PDFDocument.create();

/* (2) Embed a standard font within the document */
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);

/* (3) Define constants for the page's width and height */
const PAGE_WIDTH = 500;
const PAGE_HEIGHT = 500;

/* (4) Create the link annotation object and ref */
const linkAnnotation = pdfDoc.context.obj({
  Type: 'Annot',
  Subtype: 'Link',
  Rect: [145, PAGE_HEIGHT / 2 - 5, 358, PAGE_HEIGHT / 2 + 15],
  Border: [0, 0, 2],
  C: [0, 0, 1],
  A: {
    Type: 'Action',
    S: 'URI',
    URI: PDFString.of('https://github.com/Hopding/pdf-lib'),
  },
});
const linkAnnotationRef = pdfDoc.context.register(linkAnnotation);

/* (5) Create a page and it to the PDF document */
const page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);

/* (6) Draw some text for the link to be placed over */
page.drawText('Link to the pdf-lib GitHub Repo', {
  x: 150,
  y: PAGE_HEIGHT / 2,
  font: helveticaFont,
  size: 15,
  color: rgb(0, 0, 1),
});

/* (7) Add the link to the page */
page.node.set(PDFName.of('Annots'), pdfDoc.context.obj([linkAnnotationRef]));

/* (8) Serialize the PDF document */
const pdfBytes = await pdfDoc.save();

I hope this helps. Please let me know if you have any additional questions!

@Ashish-Tomar
Copy link
Author

Thanks, @Hopding! I owe you big time.

If you are ever in Vancouver, Canada, ping me.

@msvargas
Copy link

msvargas commented May 6, 2020

its very useful, but if you want add in existing PDF ,need push must be replace step 7, its very short however i share here to help anybody

...
  const annots = page.node.lookup(PDFName.of('Annots'), PDFArray);
  annots.push(linkAnnotationRef);

or if you need replace link annotation use this #206

for create invisible border use this:

{
...
Border: [0, 0, 0],
...
}

@ryangriggs
Copy link

ryangriggs commented Feb 28, 2023

Thanks for providing this example. Could you possibly elaborate on how to make a popup annotation (note) vs hyperlink? The goal would be to attach a note to a specific section of text, allowing the user to click the note and read the note text in a popup box. In Adobe Acrobat Pro you can do this by highlighting some text, then choosing Tools -> Comment & Markup -> Text Edits -> Add Note To Selected Text (as shown below). Then the text becomes highlighted, and clicking the highlight displays the note. I assume when creating the link annotation, a different Type and Subtype are used, but I'm not sure what. Any guidance appreciated!

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants