forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 56
Add support for text markup annotation #121
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
Open
konbraphat51
wants to merge
46
commits into
cantoo-scribe:master
Choose a base branch
from
konbraphat51:annotation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
70c4479
annotation types
konbraphat51 0c52eaf
plural
konbraphat51 dbe3184
get subtype
konbraphat51 f18da38
refac: add table reference
konbraphat51 3d7b136
annotation entries
konbraphat51 befcfe7
PDF page converting annotations
konbraphat51 b4043c6
rename operations -> drawingOperations
konbraphat51 613c8c2
Revert "rename operations -> drawingOperations"
konbraphat51 52483a2
Merge branch 'master' into annotation
konbraphat51 39811d1
add annotation object into sample.pdf
konbraphat51 f655fc9
fix: export AnnotationTypes
konbraphat51 4e56456
refac:fixed by lint
konbraphat51 fbe291b
fix: seperate types of P
konbraphat51 4debc59
feat:test annotation reading
konbraphat51 8652794
feat: add creation method for annotation object
konbraphat51 ea97ece
feat: register created Annotation object to PDF
konbraphat51 4e3d4fb
Revert "add annotation object into sample.pdf"
konbraphat51 77e6ba8
fix: move edited pdf sample
konbraphat51 7dd5301
fix: move path
konbraphat51 78aa772
fix: correct method name from Create to create in PDFAnnotation class
konbraphat51 9e89bdc
fix: rename PDFWidgetAnnotation.create to PDFWidgetAnnotation.createE…
konbraphat51 d2e888d
fix: option typing
konbraphat51 228f59f
fix: able to set array number freely
konbraphat51 ac3eb23
fix: rename PDFAnnotation.create to PDFAnnotation.createBase and add …
konbraphat51 53675ca
fix: create PDFAnnotation subclasses from Factory class
konbraphat51 6d7da79
refac: easier API to add Text Markup Annotation
konbraphat51 3cb8171
fix: fixed error with testing
konbraphat51 ba1e168
fix: update addTextMarkupAnnotation to return PDFTextMarkupAnnotation…
konbraphat51 c32f16c
test: testing annotation creation
konbraphat51 2d8b3c6
sample PDF printing code
konbraphat51 50e4d51
test: test output of annotation creation
konbraphat51 be3a35c
fix: fix slash duplication
konbraphat51 cee0754
fix dependency
konbraphat51 a7f8ad5
fix linter
konbraphat51 e61dd0f
rid testing codes
konbraphat51 3e8b024
feat: make contents editable
konbraphat51 a5280cd
refac: reorder methods
konbraphat51 e0c1cd5
feat: add method to set page reference for annotations
konbraphat51 9ed2b0f
set methods
konbraphat51 d16f4bd
Merge branch 'master' into annotation
konbraphat51 e221ad9
refac: swap duplicated codes with set methods
konbraphat51 ebed776
refac: use Date object for modificationDate in PDFAnnotation
konbraphat51 2623aac
feat: add setQuadPoints method
konbraphat51 2f8db96
fix: order of qualpoints
konbraphat51 0ee1dd8
Revert "fix: order of qualpoints"
konbraphat51 da4f8c6
refac: add noting comment
konbraphat51 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //yarn node annotationsample-fixed.js | ||
| const fs = require('fs'); | ||
|
|
||
| async function createAnnotatedPDF() { | ||
| try { | ||
| console.log('Creating PDF with annotation...'); | ||
|
|
||
| // Import pdf-lib | ||
| const { PDFDocument, StandardFonts, rgb } = require('./cjs/index'); | ||
| const { | ||
| AnnotationTypes, | ||
| } = require('./cjs/core/annotation/AnnotationTypes'); | ||
|
|
||
| console.log('PDF-lib loaded successfully'); | ||
|
|
||
| // Create a new PDF document | ||
| const pdfDoc = await PDFDocument.create(); | ||
| const page = pdfDoc.addPage([600, 400]); | ||
| console.log('Page created'); | ||
|
|
||
| // Add some text first | ||
| const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica); | ||
| page.drawText('This text will be highlighted', { | ||
| x: 50, | ||
| y: 300, | ||
| size: 12, | ||
| font: helveticaFont, | ||
| color: rgb(0, 0, 0), | ||
| }); | ||
| console.log('Text added'); | ||
|
|
||
| // Try to add a highlight annotation | ||
| try { | ||
| page.addTextMarkupAnnotation({ | ||
| subtype: AnnotationTypes.Highlight, | ||
| color: [1, 1, 0], // Yellow | ||
| rect: { | ||
| x: 50, | ||
| y: 300, | ||
| width: 150, | ||
| height: 20, | ||
| }, | ||
| contents: 'This is a highlight annotation', | ||
| quadPoints: { | ||
| leftbottomX: 50, | ||
| leftbottomY: 300, | ||
| lefttopX: 50, | ||
| lefttopY: 320, | ||
| righttopX: 200, | ||
| righttopY: 320, | ||
| rightbottomX: 200, | ||
| rightbottomY: 300, | ||
| }, | ||
| }); | ||
| console.log('Annotation added successfully'); | ||
| } catch (annotationError) { | ||
| console.log('Could not add annotation:', annotationError.message); | ||
| console.log('Proceeding without annotation...'); | ||
| } | ||
|
|
||
| // Save the PDF | ||
| const pdfBytes = await pdfDoc.save(); | ||
| fs.writeFileSync('annotationsample.pdf', pdfBytes); | ||
| console.log('PDF created successfully: annotationsample.pdf'); | ||
| } catch (error) { | ||
| console.error('Error creating PDF:', error); | ||
| } | ||
| } | ||
|
|
||
| // Run the function | ||
| createAnnotatedPDF(); |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import PDFDict from '../objects/PDFDict'; | ||
| import PDFName from '../objects/PDFName'; | ||
| import PDFAnnotation from './PDFAnnotation'; | ||
| import PDFTextMarkupAnnotation from './PDFTextMarkupAnnotation'; | ||
| import { AnnotationTypes } from './AnnotationTypes'; | ||
|
|
||
| export default class AnnotationFactory { | ||
| static fromDict = (dict: PDFDict): PDFAnnotation => { | ||
| switch (this.getSubtype(dict)) { | ||
| case AnnotationTypes.Highlight: | ||
| case AnnotationTypes.Underline: | ||
| case AnnotationTypes.Squiggly: | ||
| case AnnotationTypes.StrikeOut: | ||
| return PDFTextMarkupAnnotation.fromDict(dict); | ||
| default: | ||
| return PDFAnnotation.fromDict(dict); | ||
| } | ||
| }; | ||
|
|
||
| private static getSubtype(dict: PDFDict): AnnotationTypes | undefined { | ||
| const subtypePdfName = dict.lookup(PDFName.of('Subtype'), PDFName); | ||
| if (subtypePdfName instanceof PDFName) { | ||
| return subtypePdfName.toString() as AnnotationTypes; | ||
| } else { | ||
| return undefined; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Subtypes of PDF annotations from table 169 in the PDF specification. | ||
| */ | ||
| export enum AnnotationTypes { | ||
| Text = '/Text', | ||
| Link = '/Link', | ||
| FreeText = '/FreeText', | ||
| Line = '/Line', | ||
| Square = '/Square', | ||
| Circle = '/Circle', | ||
| Polygon = '/Polygon', | ||
| PolyLine = '/PolyLine', | ||
| Highlight = '/Highlight', | ||
| Underline = '/Underline', | ||
| Squiggly = '/Squiggly', | ||
| StrikeOut = '/StrikeOut', | ||
| Stamp = '/Stamp', | ||
| Caret = '/Caret', | ||
| Ink = '/Ink', | ||
| Popup = '/Popup', | ||
| FileAttachment = '/FileAttachment', | ||
| Sound = '/Sound', | ||
| Movie = '/Movie', | ||
| Widget = '/Widget', | ||
| Screen = '/Screen', | ||
| PrinterMark = '/PrinterMark', | ||
| TrapNet = '/TrapNet', | ||
| Watermark = '/Watermark', | ||
| ThreeD = '/3D', | ||
| Redact = '/Redact', | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a very annoying data format. Please use Rect instead. Way easier to manipulate!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean
Rectassrc\utils\elements\Rectangle.ts?Actually,
qualPointsalso covers trapezoids and hourglass shapes, not only rectangles. So I think that is not a good ideamy experiments (sorry it's in Japanese): https://qiita.com/konbraphat51/items/a7ddb30e5277d5e10a5a#%E8%9D%B6%E3%81%A4%E3%81%8C%E3%81%84