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

Retain annotations when embedding pages #849

Closed
juanludlf opened this issue Apr 6, 2021 · 6 comments
Closed

Retain annotations when embedding pages #849

juanludlf opened this issue Apr 6, 2021 · 6 comments

Comments

@juanludlf
Copy link

Annotations are retained by using copyPages but they are not retained with the embedPage method.

Is there any way to retain annotations when embedding pages?

@juanludlf juanludlf changed the title Retain annotations embedding pages Retain annotations when embedding pages Apr 6, 2021
@acoyfellow
Copy link

I ran into the same issue. I also needed a way to add more annotations on the fly. Here was my solution:

const existingAnnots = page.node.Annots().array;
const annots = [ ...existingAnnots ];
/* create new annots, push into array */
await page.node.set(PDFName.of('Annots'), pdfDoc.context.obj(annots));

@juanludlf
Copy link
Author

Hi @acoyfellow,
Thanks for the solution. The point is we need to scale the embedded page (and all included objects, like annotations) before call drawPage.
If we copy annotations after page has been scaled, how can we scale them according to the new dimensions of the page?

@Hopding
Copy link
Owner

Hopding commented Sep 23, 2021

https://pdf-lib.js.org/docs/api/classes/pdfpage#drawpage can't (and won't) preserve annotations. You should use https://pdf-lib.js.org/docs/api/classes/pdfdocument#addpage instead.

@juanludlf
Copy link
Author

juanludlf commented Sep 24, 2021

Hi @Hopding,
But addPage doest not accept a PDFEmbeddedPage. What if I need to scale the page before adding it to the document?
Thank you

@juanludlf
Copy link
Author

I've just seen a PR in progress that will solve this problem: #991

@wildhart
Copy link

I don't think PR #991 completely solves the original problem: How to embed PDF at given cooridinates and scale on a page and retain annotations. This requirement comes from needing to embed an external pdf into my document, but I need my own border around the embeded pages, and my own header, footer and page numbers as the embeded pdf is part of a larger document.

Here's the problems:

  • Using page.drawPage() only takes an embeddedPage, not another page instance, and as per this original bug report, embedded pages do not retain their annotations.
  • Using doc.addPage() retains annotations. Then it's possible to use page.scaleContent(); page.scaleAnnotations() and then page.translateContent() but there is no page.translateAnnotations() method, so it's not possible to scale and translate the embedded pdf annotations into the center of my own page.

@Hopding can you suggest a work-around for this?

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