|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as mupdf from 'mupdf'; |
| 3 | +import path from 'path'; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +const scriptdir = path.resolve(__dirname); |
| 7 | +const filename = path.join(scriptdir, "resources", "test.pdf"); |
| 8 | +const outputFilename = path.join(scriptdir, "resources", "output-annotations.pdf"); |
| 9 | + |
| 10 | +describe('mupdfjs annotations tests', () => { |
| 11 | + let document: mupdf.PDFDocument; |
| 12 | + let page: mupdf.PDFPage; |
| 13 | + |
| 14 | + beforeAll(async () => { |
| 15 | + const data = fs.readFileSync(filename); |
| 16 | + document = await mupdf.Document.openDocument(data, 'application/pdf') as mupdf.PDFDocument; |
| 17 | + page = document.loadPage(0); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(() => { |
| 21 | + fs.writeFileSync(outputFilename, document.saveToBuffer("incremental").asUint8Array()); |
| 22 | + document.destroy(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should add and verify Caret annotation', async () => { |
| 26 | + let caret = page.createAnnotation("Caret"); |
| 27 | + caret.setContents("I'm a caret!"); |
| 28 | + caret.setRect([100, 50, 0, 0]); |
| 29 | + caret.update(); |
| 30 | + |
| 31 | + const annotations = page.getAnnotations(); |
| 32 | + const caretAnnotation = annotations.find(ann => ann.getType() === 'Caret'); |
| 33 | + expect(caretAnnotation).toBeDefined(); |
| 34 | + if (caretAnnotation) { |
| 35 | + expect(caretAnnotation.getContents()).toBe("I'm a caret!"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('should add and verify FreeText annotation', async () => { |
| 40 | + let freeText = page.createAnnotation("FreeText") |
| 41 | + const text = "I'm free text!"; |
| 42 | + freeText.setContents(text); |
| 43 | + freeText.setDefaultAppearance("Helv", 16, [0, 1, 0]); |
| 44 | + freeText.setRect([0, 0, 200, 50]); |
| 45 | + freeText.update(); |
| 46 | + |
| 47 | + const annotations = page.getAnnotations(); |
| 48 | + const freeTextAnnotation = annotations.find(ann => ann.getType() === 'FreeText'); |
| 49 | + expect(freeTextAnnotation).toBeDefined(); |
| 50 | + if (freeTextAnnotation) { |
| 51 | + expect(freeTextAnnotation.getContents()).toBe(text); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('should add and verify Text annotation', async () => { |
| 56 | + let note = page.createAnnotation("Text"); |
| 57 | + const text = "I'm a note!"; |
| 58 | + note.setContents(text); |
| 59 | + note.setRect([50, 50, 0, 0]); |
| 60 | + note.update(); |
| 61 | + |
| 62 | + const annotations = page.getAnnotations(); |
| 63 | + const textAnnotation = annotations.find(ann => ann.getType() === 'Text'); |
| 64 | + expect(textAnnotation).toBeDefined(); |
| 65 | + if (textAnnotation) { |
| 66 | + expect(textAnnotation.getContents()).toBe(text); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should add and verify Highlight annotation', async () => { |
| 71 | + let highlight = page.createAnnotation("Highlight"); |
| 72 | + highlight.setColor([1, 1, 0]); |
| 73 | + highlight.setQuadPoints([ |
| 74 | + [20, 65, 230, 65, 20, 85, 230, 85], |
| 75 | + [20, 90, 230, 90, 20, 110, 230, 110], |
| 76 | + ]); |
| 77 | + highlight.update(); |
| 78 | + |
| 79 | + const annotations = page.getAnnotations(); |
| 80 | + const highlightAnnotation = annotations.find(ann => ann.getType() === 'Highlight'); |
| 81 | + expect(highlightAnnotation).toBeDefined(); |
| 82 | + if (highlightAnnotation) { |
| 83 | + expect(highlightAnnotation.getQuadPoints()).toEqual([ |
| 84 | + [20, 65, 230, 65, 20, 85, 230, 85], |
| 85 | + [20, 90, 230, 90, 20, 110, 230, 110], |
| 86 | + ]); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it('should add and verify Underline annotation', async () => { |
| 91 | + let underline = page.createAnnotation("Underline"); |
| 92 | + underline.setColor([0, 1, 0]); |
| 93 | + underline.setQuadPoints([[50, 50, 150, 50, 50, 100, 150, 100]]); |
| 94 | + underline.update(); |
| 95 | + |
| 96 | + const annotations = page.getAnnotations(); |
| 97 | + const underlineAnnotation = annotations.find(ann => ann.getType() === 'Underline'); |
| 98 | + expect(underlineAnnotation).toBeDefined(); |
| 99 | + if (underlineAnnotation) { |
| 100 | + expect(underlineAnnotation.getQuadPoints()).toEqual([[50, 50, 150, 50, 50, 100, 150, 100]]); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it('should add and verify Squiggly annotation', async () => { |
| 105 | + let squiggly = page.createAnnotation("Squiggly"); |
| 106 | + squiggly.setColor([0, 0, 1]); |
| 107 | + squiggly.setQuadPoints([[50, 150, 150, 150, 50, 200, 150, 200]]); |
| 108 | + squiggly.update(); |
| 109 | + |
| 110 | + const annotations = page.getAnnotations(); |
| 111 | + const squigglyAnnotation = annotations.find(ann => ann.getType() === 'Squiggly'); |
| 112 | + expect(squigglyAnnotation).toBeDefined(); |
| 113 | + if (squigglyAnnotation) { |
| 114 | + expect(squigglyAnnotation.getQuadPoints()).toEqual([[50, 150, 150, 150, 50, 200, 150, 200]]); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + it('should add and verify Line annotation', async () => { |
| 119 | + let line = page.createAnnotation("Line"); |
| 120 | + line.setColor([0, 0, 0]); |
| 121 | + line.setLine([50, 350], [150, 350]); |
| 122 | + line.update(); |
| 123 | + |
| 124 | + const annotations = page.getAnnotations(); |
| 125 | + const lineAnnotation = annotations.find(ann => ann.getType() === 'Line'); |
| 126 | + expect(lineAnnotation).toBeDefined(); |
| 127 | + if (lineAnnotation) { |
| 128 | + expect(lineAnnotation.getLine()).toEqual([[50, 350], [150, 350]]); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + // it('should add and verify Square annotation', async () => { |
| 133 | + // let square = page.createAnnotation("Square"); |
| 134 | + // square.setColor([0, 1, 1]); |
| 135 | + // square.setRect([50, 450, 150, 500]); |
| 136 | + // square.update(); |
| 137 | + |
| 138 | + // const annotations = page.getAnnotations(); |
| 139 | + // const squareAnnotation = annotations.find(ann => ann.getType() === 'Square'); |
| 140 | + // expect(squareAnnotation).toBeDefined(); |
| 141 | + // if (squareAnnotation) { |
| 142 | + // expect(squareAnnotation.getRect()).toEqual([50, 450, 150, 500]); |
| 143 | + // } |
| 144 | + // }); |
| 145 | + |
| 146 | + // it('should add and verify Circle annotation', async () => { |
| 147 | + // let circle = page.createAnnotation("Circle"); |
| 148 | + // circle.setColor([1, 0, 1]); |
| 149 | + // circle.setRect([200, 50, 300, 100]); |
| 150 | + // circle.update(); |
| 151 | + |
| 152 | + // const annotations = page.getAnnotations(); |
| 153 | + // const circleAnnotation = annotations.find(ann => ann.getType() === 'Circle'); |
| 154 | + // expect(circleAnnotation).toBeDefined(); |
| 155 | + // if (circleAnnotation) { |
| 156 | + // expect(circleAnnotation.getRect()).toEqual([200, 50, 300, 100]); |
| 157 | + // } |
| 158 | + // }); |
| 159 | + |
| 160 | + it('should add and verify Polygon annotation', async () => { |
| 161 | + let polygon = page.createAnnotation("Polygon"); |
| 162 | + polygon.setColor([1, 1, 0]); |
| 163 | + polygon.setVertices([[100, 150], [200, 150], [150, 200]]); |
| 164 | + polygon.update(); |
| 165 | + |
| 166 | + const annotations = page.getAnnotations(); |
| 167 | + const polygonAnnotation = annotations.find(ann => ann.getType() === 'Polygon'); |
| 168 | + expect(polygonAnnotation).toBeDefined(); |
| 169 | + if (polygonAnnotation) { |
| 170 | + expect(polygonAnnotation.getVertices()).toEqual([[100, 150], [200, 150], [150, 200]]); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + it('should add and verify PolyLine annotation', async () => { |
| 175 | + let polyline = page.createAnnotation("PolyLine"); |
| 176 | + polyline.setColor([0, 1, 1]); |
| 177 | + polyline.setVertices([[100, 250], [200, 250], [150, 300]]); |
| 178 | + polyline.update(); |
| 179 | + |
| 180 | + const annotations = page.getAnnotations(); |
| 181 | + const polylineAnnotation = annotations.find(ann => ann.getType() === 'PolyLine'); |
| 182 | + expect(polylineAnnotation).toBeDefined(); |
| 183 | + if (polylineAnnotation) { |
| 184 | + expect(polylineAnnotation.getVertices()).toEqual([[100, 250], [200, 250], [150, 300]]); |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + // it('should add and verify Stamp annotation', async () => { |
| 189 | + // let stamp = page.createAnnotation("Stamp"); |
| 190 | + // stamp.setRect([50, 650, 150, 700]); |
| 191 | + // stamp.setContents("Approved"); |
| 192 | + // stamp.update(); |
| 193 | + |
| 194 | + // const annotations = page.getAnnotations(); |
| 195 | + // const stampAnnotation = annotations.find(ann => ann.getType() === 'Stamp'); |
| 196 | + // expect(stampAnnotation).toBeDefined(); |
| 197 | + // if (stampAnnotation) { |
| 198 | + // expect(stampAnnotation.getRect()).toEqual([50, 650, 150, 700]); |
| 199 | + // expect(stampAnnotation.getContents()).toBe("Approved"); |
| 200 | + // } |
| 201 | + // }); |
| 202 | + |
| 203 | + it('should delete an annotation', async () => { |
| 204 | + let note = page.createAnnotation("Text"); |
| 205 | + const text = "This is a temporary note."; |
| 206 | + note.setContents(text); |
| 207 | + note.setRect([100, 100, 0, 0]); |
| 208 | + note.update(); |
| 209 | + |
| 210 | + let annotations = page.getAnnotations(); |
| 211 | + const textAnnotation = annotations.find(ann => ann.getType() === 'Text' && ann.getContents() === text); |
| 212 | + expect(textAnnotation).toBeDefined(); |
| 213 | + |
| 214 | + if (textAnnotation) { |
| 215 | + page.deleteAnnotation(textAnnotation); |
| 216 | + annotations = page.getAnnotations(); |
| 217 | + expect(annotations.find(ann => ann.getType() === 'Text' && ann.getContents() === text)).toBeUndefined(); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | +}); |
0 commit comments