Skip to content

feat: add page translate function#1379

Open
mo-bai wants to merge 2 commits intoHopding:masterfrom
mo-bai:test
Open

feat: add page translate function#1379
mo-bai wants to merge 2 commits intoHopding:masterfrom
mo-bai:test

Conversation

@mo-bai
Copy link
Copy Markdown

@mo-bai mo-bai commented Jan 14, 2023

What?

I added one [translate] function to the PDFPage class . the method can translate annotations and content of the PDF's page . not only then content .

let source = await PDFDocument.load(readFile(path))
const pdfDoc = await PDFDocument.create()
let pages = await pdfDoc.copyPages(source, source.getPageIndices())
for (let page of pages) {
page.translate(50,50)
pdfDoc.addPage(page)
const pdfBytes = await pdfDoc.save()

Why?

In many pdf files, annotations and text content is followed together, the library currently only provides the translateContent method, if you use the method will cause misalignment of annotations and text . also , there are no methods in the library for moving annotations

How?

In my method, I first find all the annotations and then trnaslate the annotations by adding or subtracting numbers from the location information in the annotations

Testing?

I created a pdf file, marked the annotation on top of a word, and then use my method [ translate(20,20) ] , and then check if the annotation deviates from the word . Result: No offset . then repeat doing this test by using different annotation type

New Dependencies?

no

Screenshots

1673705834409
the yellow line is annotation
1673705895780

Suggested Reading?

Yes , but the url(https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf) is 404

Anything Else?

Checklist

  • I read CONTRIBUTING.md.
  • I read MAINTAINERSHIP.md#pull-requests.
  • I added/updated unit tests for my changes.
  • I added/updated integration tests for my changes.
  • I ran the integration tests.
  • I tested my changes in Node, Deno, and the browser.
  • I viewed documents produced with my changes in Adobe Acrobat, Foxit Reader, Firefox, and Chrome.
  • I added/updated doc comments for any new/modified public APIs.
  • My changes work for both new and existing PDF files.
  • I ran the linter on my changes.

@anodynos
Copy link
Copy Markdown

@mo-bai please check my comment here

Copy link
Copy Markdown

@Sharcoux Sharcoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing:

  • tests
  • documentation

folknor added a commit to folknor/pdf-lib that referenced this pull request Jan 29, 2026
Add PDFPage.translate(x, y) that moves both page content AND annotations
together, keeping them aligned. Previously, translateContent() only moved
content, leaving annotations (comments, highlights, links, etc.) misaligned.

New methods:
- translate(x, y) - moves content and annotations together
- translateAnnotations(x, y) - moves only annotations

Also adds PDFArray.translatePDFNumbers() helper method.

Fixes Hopding/pdf-lib#1379

Coded by an LLM.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants