@pdfme/pdf-lib 6.0.3 → 6.0.4-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (449) hide show
  1. package/{src/api/Embeddable.ts → dist/api/Embeddable.d.ts} +1 -1
  2. package/dist/api/PDFDocument.d.ts +778 -0
  3. package/dist/api/PDFDocumentOptions.d.ts +38 -0
  4. package/dist/api/PDFEmbeddedFile.d.ts +38 -0
  5. package/dist/api/PDFEmbeddedPage.d.ts +73 -0
  6. package/dist/api/PDFFont.d.ts +94 -0
  7. package/dist/api/PDFImage.d.ts +95 -0
  8. package/dist/api/PDFJavaScript.d.ts +38 -0
  9. package/dist/api/PDFPage.d.ts +910 -0
  10. package/dist/api/PDFPageOptions.d.ts +172 -0
  11. package/dist/api/StandardFonts.d.ts +16 -0
  12. package/dist/api/colors.d.ts +34 -0
  13. package/dist/api/errors.d.ts +42 -0
  14. package/dist/api/form/PDFButton.d.ts +137 -0
  15. package/dist/api/form/PDFCheckBox.d.ts +142 -0
  16. package/dist/api/form/PDFDropdown.d.ts +402 -0
  17. package/dist/api/form/PDFField.d.ts +203 -0
  18. package/dist/api/form/PDFForm.d.ts +411 -0
  19. package/dist/api/form/PDFOptionList.d.ts +335 -0
  20. package/dist/api/form/PDFRadioGroup.d.ts +252 -0
  21. package/dist/api/form/PDFSignature.d.ts +29 -0
  22. package/dist/api/form/PDFTextField.d.ts +537 -0
  23. package/dist/api/form/appearances.d.ts +41 -0
  24. package/dist/api/image/alignment.d.ts +5 -0
  25. package/dist/api/objects.d.ts +4 -0
  26. package/dist/api/operations.d.ts +226 -0
  27. package/dist/api/operators.d.ts +90 -0
  28. package/dist/api/rotations.d.ts +38 -0
  29. package/dist/api/sizes.d.ts +52 -0
  30. package/dist/api/svg.d.ts +70 -0
  31. package/dist/api/svgPath.d.ts +2 -0
  32. package/dist/api/text/alignment.d.ts +5 -0
  33. package/dist/api/text/layout.d.ts +54 -0
  34. package/dist/core/PDFContext.d.ts +84 -0
  35. package/dist/core/PDFObjectCopier.d.ts +37 -0
  36. package/dist/core/acroform/PDFAcroButton.d.ts +21 -0
  37. package/dist/core/acroform/PDFAcroCheckBox.d.ts +13 -0
  38. package/dist/core/acroform/PDFAcroChoice.d.ts +20 -0
  39. package/dist/core/acroform/PDFAcroComboBox.d.ts +9 -0
  40. package/dist/core/acroform/PDFAcroField.d.ts +36 -0
  41. package/dist/core/acroform/PDFAcroForm.d.ts +20 -0
  42. package/dist/core/acroform/PDFAcroListBox.d.ts +9 -0
  43. package/dist/core/acroform/PDFAcroNonTerminal.d.ts +13 -0
  44. package/dist/core/acroform/PDFAcroPushButton.d.ts +9 -0
  45. package/dist/core/acroform/PDFAcroRadioButton.d.ts +13 -0
  46. package/{src/core/acroform/PDFAcroSignature.ts → dist/core/acroform/PDFAcroSignature.d.ts} +2 -4
  47. package/dist/core/acroform/PDFAcroTerminal.d.ts +16 -0
  48. package/dist/core/acroform/PDFAcroText.d.ts +22 -0
  49. package/dist/core/acroform/flags.d.ts +141 -0
  50. package/dist/core/acroform/utils.d.ts +6 -0
  51. package/dist/core/annotation/AppearanceCharacteristics.d.ts +33 -0
  52. package/dist/core/annotation/BorderStyle.d.ts +11 -0
  53. package/dist/core/annotation/PDFAnnotation.d.ts +51 -0
  54. package/dist/core/annotation/PDFWidgetAnnotation.d.ts +26 -0
  55. package/dist/core/annotation/flags.d.ts +79 -0
  56. package/dist/core/crypto.d.ts +95 -0
  57. package/dist/core/document/PDFCrossRefSection.d.ts +27 -0
  58. package/dist/core/document/PDFHeader.d.ts +10 -0
  59. package/dist/core/document/PDFTrailer.d.ts +9 -0
  60. package/dist/core/document/PDFTrailerDict.d.ts +10 -0
  61. package/dist/core/embedders/CMap.d.ts +3 -0
  62. package/dist/core/embedders/CustomFontEmbedder.d.ts +44 -0
  63. package/dist/core/embedders/CustomFontSubsetEmbedder.d.ts +20 -0
  64. package/dist/core/embedders/FileEmbedder.d.ts +33 -0
  65. package/dist/core/embedders/FontFlags.d.ts +13 -0
  66. package/dist/core/embedders/JavaScriptEmbedder.d.ts +10 -0
  67. package/dist/core/embedders/JpegEmbedder.d.ts +23 -0
  68. package/dist/core/embedders/PDFPageEmbedder.d.ts +37 -0
  69. package/dist/core/embedders/PngEmbedder.d.ts +19 -0
  70. package/dist/core/embedders/StandardFontEmbedder.d.ts +36 -0
  71. package/dist/core/errors.d.ts +94 -0
  72. package/{src/core/index.ts → dist/core/index.d.ts} +1 -17
  73. package/dist/core/interactive/ViewerPreferences.d.ts +347 -0
  74. package/dist/core/objects/PDFArray.d.ts +63 -0
  75. package/dist/core/objects/PDFBool.d.ts +13 -0
  76. package/dist/core/objects/PDFDict.d.ts +62 -0
  77. package/dist/core/objects/PDFHexString.d.ts +16 -0
  78. package/dist/core/objects/PDFInvalidObject.d.ts +11 -0
  79. package/dist/core/objects/PDFName.d.ts +41 -0
  80. package/dist/core/objects/PDFNull.d.ts +10 -0
  81. package/dist/core/objects/PDFNumber.d.ts +15 -0
  82. package/dist/core/objects/PDFObject.d.ts +8 -0
  83. package/dist/core/objects/PDFRawStream.d.ts +16 -0
  84. package/dist/core/objects/PDFRef.d.ts +13 -0
  85. package/dist/core/objects/PDFStream.d.ts +16 -0
  86. package/dist/core/objects/PDFString.d.ts +16 -0
  87. package/dist/core/operators/PDFOperator.d.ts +19 -0
  88. package/dist/core/operators/PDFOperatorNames.d.ts +76 -0
  89. package/dist/core/parser/BaseParser.d.ts +14 -0
  90. package/dist/core/parser/ByteStream.d.ts +26 -0
  91. package/dist/core/parser/PDFObjectParser.d.ts +31 -0
  92. package/dist/core/parser/PDFObjectStreamParser.d.ts +13 -0
  93. package/dist/core/parser/PDFParser.d.ts +46 -0
  94. package/dist/core/parser/PDFXRefStreamParser.d.ts +21 -0
  95. package/dist/core/streams/Ascii85Stream.d.ts +9 -0
  96. package/dist/core/streams/AsciiHexStream.d.ts +9 -0
  97. package/dist/core/streams/DecodeStream.d.ts +26 -0
  98. package/dist/core/streams/DecryptStream.d.ts +12 -0
  99. package/dist/core/streams/FlateStream.d.ts +13 -0
  100. package/dist/core/streams/LZWStream.d.ts +12 -0
  101. package/dist/core/streams/RunLengthStream.d.ts +8 -0
  102. package/dist/core/streams/Stream.d.ts +34 -0
  103. package/dist/core/streams/decode.d.ts +3 -0
  104. package/dist/core/structures/PDFCatalog.d.ts +27 -0
  105. package/dist/core/structures/PDFContentStream.d.ts +15 -0
  106. package/dist/core/structures/PDFCrossRefStream.d.ts +53 -0
  107. package/dist/core/structures/PDFFlateStream.d.ts +13 -0
  108. package/dist/core/structures/PDFObjectStream.d.ts +20 -0
  109. package/dist/core/structures/PDFPageLeaf.d.ts +54 -0
  110. package/dist/core/structures/PDFPageTree.d.ts +41 -0
  111. package/dist/core/syntax/CharCodes.d.ts +61 -0
  112. package/dist/core/syntax/Delimiters.d.ts +1 -0
  113. package/dist/core/syntax/Irregular.d.ts +1 -0
  114. package/dist/core/syntax/Keywords.d.ts +22 -0
  115. package/dist/core/syntax/Numeric.d.ts +3 -0
  116. package/dist/core/syntax/Whitespace.d.ts +1 -0
  117. package/dist/core/writers/PDFStreamWriter.d.ts +19 -0
  118. package/dist/core/writers/PDFWriter.d.ts +29 -0
  119. package/dist/index.js +19124 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/types/fontkit.d.ts +597 -0
  122. package/{src/types/index.ts → dist/types/index.d.ts} +8 -13
  123. package/{src/types/matrix.ts → dist/types/matrix.d.ts} +1 -2
  124. package/dist/utils/Cache.d.ts +10 -0
  125. package/dist/utils/arrays.d.ts +16 -0
  126. package/dist/utils/async.d.ts +5 -0
  127. package/dist/utils/base64.d.ts +10 -0
  128. package/dist/utils/elements/Arc.d.ts +22 -0
  129. package/dist/utils/elements/Circle.d.ts +14 -0
  130. package/dist/utils/elements/Ellipse.d.ts +20 -0
  131. package/dist/utils/elements/GraphElement.d.ts +6 -0
  132. package/dist/utils/elements/Line.d.ts +23 -0
  133. package/dist/utils/elements/Plot.d.ts +11 -0
  134. package/dist/utils/elements/Point.d.ts +18 -0
  135. package/dist/utils/elements/Rectangle.d.ts +21 -0
  136. package/dist/utils/elements/Segment.d.ts +19 -0
  137. package/dist/utils/errors.d.ts +1 -0
  138. package/dist/utils/intersections.d.ts +9 -0
  139. package/dist/utils/maths.d.ts +45 -0
  140. package/dist/utils/numbers.d.ts +23 -0
  141. package/dist/utils/objects.d.ts +15 -0
  142. package/dist/utils/pdfDocEncoding.d.ts +7 -0
  143. package/dist/utils/png.d.ts +17 -0
  144. package/dist/utils/rng.d.ts +13 -0
  145. package/dist/utils/strings.d.ts +25 -0
  146. package/{src/utils/unicode.ts → dist/utils/unicode.d.ts} +8 -195
  147. package/dist/utils/validators.d.ts +26 -0
  148. package/package.json +6 -1
  149. package/__tests__/api/PDFDocument.spec.ts +0 -576
  150. package/__tests__/api/PDFImage.spec.ts +0 -60
  151. package/__tests__/api/PDFPage.spec.ts +0 -151
  152. package/__tests__/api/form/PDFCheckBox.spec.ts +0 -71
  153. package/__tests__/api/form/PDFDropdown.spec.ts +0 -118
  154. package/__tests__/api/form/PDFForm.spec.ts +0 -370
  155. package/__tests__/api/form/PDFOptionList.spec.ts +0 -104
  156. package/__tests__/api/form/PDFRadioGroup.spec.ts +0 -183
  157. package/__tests__/api/form/PDFTextField.spec.ts +0 -148
  158. package/__tests__/api/svgPath.spec.ts +0 -153
  159. package/__tests__/api/text/layout.spec.ts +0 -166
  160. package/__tests__/core/PDFContext.spec.ts +0 -194
  161. package/__tests__/core/PDFObjectCopier.spec.ts +0 -421
  162. package/__tests__/core/acroform/PDFAcroField.spec.ts +0 -98
  163. package/__tests__/core/acroform/PDFAcroTerminal.spec.ts +0 -39
  164. package/__tests__/core/annotation/PDFWidgetAnnotation.spec.ts +0 -40
  165. package/__tests__/core/document/PDFCrossRefSection.spec.ts +0 -88
  166. package/__tests__/core/document/PDFHeader.spec.ts +0 -22
  167. package/__tests__/core/document/PDFTrailer.spec.ts +0 -28
  168. package/__tests__/core/document/PDFTrailerDict.spec.ts +0 -24
  169. package/__tests__/core/embedders/CMap.spec.ts +0 -47
  170. package/__tests__/core/embedders/CustomFontEmbedder.spec.ts +0 -96
  171. package/__tests__/core/embedders/CustomFontSubsetEmbedder.spec.ts +0 -62
  172. package/__tests__/core/embedders/FileEmbedder.spec.ts +0 -54
  173. package/__tests__/core/embedders/JpegEmbedder.spec.ts +0 -67
  174. package/__tests__/core/embedders/PDFPageEmbedder.spec.ts +0 -65
  175. package/__tests__/core/embedders/PngEmbedder.spec.ts +0 -87
  176. package/__tests__/core/embedders/StandardFontEmbedder.spec.ts +0 -79
  177. package/__tests__/core/embedders/data/SourceHanSerifJP-Regular.otf.cmap +0 -16096
  178. package/__tests__/core/embedders/data/Ubuntu-R.ttf.cmap +0 -1211
  179. package/__tests__/core/objects/PDFArray.spec.ts +0 -134
  180. package/__tests__/core/objects/PDFBool.spec.ts +0 -42
  181. package/__tests__/core/objects/PDFDict.spec.ts +0 -195
  182. package/__tests__/core/objects/PDFHexString.spec.ts +0 -168
  183. package/__tests__/core/objects/PDFInvalidObject.spec.ts +0 -32
  184. package/__tests__/core/objects/PDFName.spec.ts +0 -122
  185. package/__tests__/core/objects/PDFNull.spec.ts +0 -30
  186. package/__tests__/core/objects/PDFNumber.spec.ts +0 -50
  187. package/__tests__/core/objects/PDFObject.spec.ts +0 -29
  188. package/__tests__/core/objects/PDFRawStream.spec.ts +0 -45
  189. package/__tests__/core/objects/PDFRef.spec.ts +0 -60
  190. package/__tests__/core/objects/PDFString.spec.ts +0 -237
  191. package/__tests__/core/operators/PDFOperator.spec.ts +0 -77
  192. package/__tests__/core/parser/PDFObjectParser.spec.ts +0 -730
  193. package/__tests__/core/parser/PDFObjectStreamParser.spec.ts +0 -158
  194. package/__tests__/core/parser/PDFParser.spec.ts +0 -398
  195. package/__tests__/core/parser/PDFXRefStreamParser.spec.ts +0 -187
  196. package/__tests__/core/parser/data/object-stream-invalid +0 -2
  197. package/__tests__/core/parser/data/object-stream1 +0 -16
  198. package/__tests__/core/parser/data/object-stream2 +0 -10
  199. package/__tests__/core/parser/data/object-stream3 +0 -0
  200. package/__tests__/core/parser/data/object-stream4 +0 -0
  201. package/__tests__/core/parser/data/xref-stream1 +0 -0
  202. package/__tests__/core/parser/data/xref-stream2 +0 -0
  203. package/__tests__/core/parser/data/xref-stream3 +0 -0
  204. package/__tests__/core/parser/data/xref-stream4 +0 -0
  205. package/__tests__/core/streams/Ascii85Stream.spec.ts +0 -20
  206. package/__tests__/core/streams/AsciiHexStream.spec.ts +0 -20
  207. package/__tests__/core/streams/FlateStream.spec.ts +0 -20
  208. package/__tests__/core/streams/LZWStream.spec.ts +0 -20
  209. package/__tests__/core/streams/RunLengthStream.spec.ts +0 -20
  210. package/__tests__/core/streams/Stream.spec.ts +0 -128
  211. package/__tests__/core/streams/data/ascii85/1.decoded +0 -0
  212. package/__tests__/core/streams/data/ascii85/1.encoded +0 -43
  213. package/__tests__/core/streams/data/asciihex/1.decoded +0 -1
  214. package/__tests__/core/streams/data/asciihex/1.encoded +0 -1
  215. package/__tests__/core/streams/data/asciihex/2.decoded +0 -7
  216. package/__tests__/core/streams/data/asciihex/2.encoded +0 -1
  217. package/__tests__/core/streams/data/flate/1.corrupt.encoded +0 -1
  218. package/__tests__/core/streams/data/flate/1.decoded +0 -0
  219. package/__tests__/core/streams/data/flate/1.encoded +0 -2
  220. package/__tests__/core/streams/data/flate/2.decoded +0 -33
  221. package/__tests__/core/streams/data/flate/2.encoded +0 -0
  222. package/__tests__/core/streams/data/flate/3.decoded +0 -15
  223. package/__tests__/core/streams/data/flate/3.encoded +0 -0
  224. package/__tests__/core/streams/data/flate/4.decoded +0 -16
  225. package/__tests__/core/streams/data/flate/4.encoded +0 -0
  226. package/__tests__/core/streams/data/flate/5.decoded +0 -0
  227. package/__tests__/core/streams/data/flate/5.encoded +0 -0
  228. package/__tests__/core/streams/data/flate/6.decoded +0 -17
  229. package/__tests__/core/streams/data/flate/6.encoded +0 -0
  230. package/__tests__/core/streams/data/flate/7.decoded +0 -20
  231. package/__tests__/core/streams/data/flate/7.encoded +0 -1
  232. package/__tests__/core/streams/data/lzw/1.decoded +0 -0
  233. package/__tests__/core/streams/data/lzw/1.encoded +0 -0
  234. package/__tests__/core/streams/data/lzw/2.decoded +0 -0
  235. package/__tests__/core/streams/data/lzw/2.encoded +0 -0
  236. package/__tests__/core/streams/data/lzw/3.decoded +0 -14
  237. package/__tests__/core/streams/data/lzw/3.encoded +0 -2
  238. package/__tests__/core/streams/data/lzw/4.decoded +0 -14
  239. package/__tests__/core/streams/data/lzw/4.encoded +0 -2
  240. package/__tests__/core/streams/data/runlength/1.decoded +0 -1
  241. package/__tests__/core/streams/data/runlength/1.encoded +0 -1
  242. package/__tests__/core/streams/data/runlength/2.decoded +0 -1
  243. package/__tests__/core/streams/data/runlength/2.encoded +0 -1
  244. package/__tests__/core/streams/data/runlength/3.decoded +0 -0
  245. package/__tests__/core/streams/data/runlength/3.encoded +0 -0
  246. package/__tests__/core/streams/data/runlength/4.decoded +0 -1
  247. package/__tests__/core/streams/data/runlength/4.encoded +0 -1
  248. package/__tests__/core/streams/data/runlength/5.decoded +0 -1
  249. package/__tests__/core/streams/data/runlength/5.encoded +0 -1
  250. package/__tests__/core/streams/data/runlength/empty.encoded +0 -1
  251. package/__tests__/core/structures/PDFCatalog.spec.ts +0 -108
  252. package/__tests__/core/structures/PDFContentStream.spec.ts +0 -117
  253. package/__tests__/core/structures/PDFCrossRefStream.spec.ts +0 -145
  254. package/__tests__/core/structures/PDFObjectStream.spec.ts +0 -138
  255. package/__tests__/core/structures/PDFPageLeaf.spec.ts +0 -355
  256. package/__tests__/core/structures/PDFPageTree.spec.ts +0 -592
  257. package/__tests__/core/writers/PDFStreamWriter.spec.ts +0 -71
  258. package/__tests__/core/writers/PDFWriter.spec.ts +0 -144
  259. package/__tests__/core/writers/data/stream-writer-1.pdf +0 -0
  260. package/__tests__/index.test.ts +0 -12
  261. package/__tests__/utils/base64.spec.ts +0 -61
  262. package/__tests__/utils/data/simple.pdf +0 -201
  263. package/__tests__/utils/data/simple.pdf.base64 +0 -1
  264. package/__tests__/utils/pdfDocEncoding.spec.ts +0 -82
  265. package/__tests__/utils/png.spec.ts +0 -53
  266. package/__tests__/utils/rng.spec.ts +0 -14
  267. package/__tests__/utils/strings.spec.ts +0 -98
  268. package/__tests__/utils/unicode.spec.ts +0 -337
  269. package/assets/fonts/source_hans_jp/SourceHanSerifJP-Regular.otf +0 -0
  270. package/assets/fonts/ubuntu/Ubuntu-B.ttf +0 -0
  271. package/assets/fonts/ubuntu/Ubuntu-R.ttf +0 -0
  272. package/assets/images/cat_riding_unicorn.jpg +0 -0
  273. package/assets/images/cmyk_colorspace.jpg +0 -0
  274. package/assets/images/greyscale_bird.png +0 -0
  275. package/assets/images/minions_banana_alpha.png +0 -0
  276. package/assets/images/minions_banana_no_alpha.png +0 -0
  277. package/assets/images/minions_laughing.jpg +0 -0
  278. package/assets/pdfs/bixby_guide.pdf +0 -0
  279. package/assets/pdfs/encrypted_new.pdf +0 -0
  280. package/assets/pdfs/encrypted_old.pdf +0 -0
  281. package/assets/pdfs/fancy_fields.pdf +0 -0
  282. package/assets/pdfs/giraffe.pdf +0 -0
  283. package/assets/pdfs/invalid_root_ref.pdf +0 -0
  284. package/assets/pdfs/just_metadata.pdf +0 -49
  285. package/assets/pdfs/missing_endobj_keyword.pdf +0 -70
  286. package/assets/pdfs/missing_xref_trailer_dict.pdf +0 -141
  287. package/assets/pdfs/normal.pdf +0 -0
  288. package/assets/pdfs/pdf20examples/PDF 2.0 with offset start.pdf +0 -179
  289. package/assets/pdfs/stuff_following_header.pdf +0 -0
  290. package/assets/pdfs/us_constitution.pdf +0 -0
  291. package/assets/pdfs/with_comments.pdf +0 -0
  292. package/assets/pdfs/with_invalid_objects.pdf +0 -53
  293. package/assets/pdfs/with_invalid_stream_EOL.pdf +0 -0
  294. package/assets/pdfs/with_null_parent_entry.pdf +0 -0
  295. package/assets/pdfs/with_signature.pdf +0 -0
  296. package/assets/pdfs/with_update_sections.pdf +0 -0
  297. package/assets/pdfs/with_viewer_prefs.pdf +0 -0
  298. package/assets/pdfs/with_xfa_fields.pdf +0 -0
  299. package/src/api/PDFDocument.ts +0 -1389
  300. package/src/api/PDFDocumentOptions.ts +0 -45
  301. package/src/api/PDFEmbeddedFile.ts +0 -87
  302. package/src/api/PDFEmbeddedPage.ts +0 -100
  303. package/src/api/PDFFont.ts +0 -149
  304. package/src/api/PDFImage.ts +0 -143
  305. package/src/api/PDFJavaScript.ts +0 -75
  306. package/src/api/PDFPage.ts +0 -1604
  307. package/src/api/PDFPageOptions.ts +0 -175
  308. package/src/api/StandardFonts.ts +0 -16
  309. package/src/api/colors.ts +0 -109
  310. package/src/api/errors.ts +0 -110
  311. package/src/api/form/PDFButton.ts +0 -242
  312. package/src/api/form/PDFCheckBox.ts +0 -247
  313. package/src/api/form/PDFDropdown.ts +0 -637
  314. package/src/api/form/PDFField.ts +0 -491
  315. package/src/api/form/PDFForm.ts +0 -842
  316. package/src/api/form/PDFOptionList.ts +0 -555
  317. package/src/api/form/PDFRadioGroup.ts +0 -455
  318. package/src/api/form/PDFSignature.ts +0 -44
  319. package/src/api/form/PDFTextField.ts +0 -811
  320. package/src/api/form/appearances.ts +0 -655
  321. package/src/api/image/alignment.ts +0 -5
  322. package/src/api/objects.ts +0 -10
  323. package/src/api/operations.ts +0 -873
  324. package/src/api/operators.ts +0 -335
  325. package/src/api/rotations.ts +0 -84
  326. package/src/api/sizes.ts +0 -52
  327. package/src/api/svg.ts +0 -888
  328. package/src/api/svgPath.ts +0 -460
  329. package/src/api/text/alignment.ts +0 -5
  330. package/src/api/text/layout.ts +0 -328
  331. package/src/core/PDFContext.ts +0 -274
  332. package/src/core/PDFObjectCopier.ts +0 -142
  333. package/src/core/acroform/PDFAcroButton.ts +0 -104
  334. package/src/core/acroform/PDFAcroCheckBox.ts +0 -48
  335. package/src/core/acroform/PDFAcroChoice.ts +0 -143
  336. package/src/core/acroform/PDFAcroComboBox.ts +0 -21
  337. package/src/core/acroform/PDFAcroField.ts +0 -168
  338. package/src/core/acroform/PDFAcroForm.ts +0 -96
  339. package/src/core/acroform/PDFAcroListBox.ts +0 -19
  340. package/src/core/acroform/PDFAcroNonTerminal.ts +0 -33
  341. package/src/core/acroform/PDFAcroPushButton.ts +0 -21
  342. package/src/core/acroform/PDFAcroRadioButton.ts +0 -57
  343. package/src/core/acroform/PDFAcroTerminal.ts +0 -70
  344. package/src/core/acroform/PDFAcroText.ts +0 -76
  345. package/src/core/acroform/flags.ts +0 -162
  346. package/src/core/acroform/utils.ts +0 -129
  347. package/src/core/annotation/AppearanceCharacteristics.ts +0 -133
  348. package/src/core/annotation/BorderStyle.ts +0 -31
  349. package/src/core/annotation/PDFAnnotation.ts +0 -148
  350. package/src/core/annotation/PDFWidgetAnnotation.ts +0 -111
  351. package/src/core/annotation/flags.ts +0 -90
  352. package/src/core/crypto.ts +0 -1844
  353. package/src/core/document/PDFCrossRefSection.ts +0 -161
  354. package/src/core/document/PDFHeader.ts +0 -48
  355. package/src/core/document/PDFTrailer.ts +0 -48
  356. package/src/core/document/PDFTrailerDict.ts +0 -39
  357. package/src/core/embedders/CMap.ts +0 -65
  358. package/src/core/embedders/CustomFontEmbedder.ts +0 -237
  359. package/src/core/embedders/CustomFontSubsetEmbedder.ts +0 -89
  360. package/src/core/embedders/FileEmbedder.ts +0 -77
  361. package/src/core/embedders/FontFlags.ts +0 -45
  362. package/src/core/embedders/JavaScriptEmbedder.ts +0 -34
  363. package/src/core/embedders/JpegEmbedder.ts +0 -118
  364. package/src/core/embedders/PDFPageEmbedder.ts +0 -139
  365. package/src/core/embedders/PngEmbedder.ts +0 -69
  366. package/src/core/embedders/StandardFontEmbedder.ts +0 -121
  367. package/src/core/errors.ts +0 -226
  368. package/src/core/interactive/ViewerPreferences.ts +0 -565
  369. package/src/core/objects/PDFArray.ts +0 -179
  370. package/src/core/objects/PDFBool.ts +0 -53
  371. package/src/core/objects/PDFDict.ts +0 -220
  372. package/src/core/objects/PDFHexString.ts +0 -94
  373. package/src/core/objects/PDFInvalidObject.ts +0 -34
  374. package/src/core/objects/PDFName.ts +0 -152
  375. package/src/core/objects/PDFNull.ts +0 -30
  376. package/src/core/objects/PDFNumber.ts +0 -44
  377. package/src/core/objects/PDFObject.ts +0 -22
  378. package/src/core/objects/PDFRawStream.ts +0 -41
  379. package/src/core/objects/PDFRef.ts +0 -51
  380. package/src/core/objects/PDFStream.ts +0 -87
  381. package/src/core/objects/PDFString.ts +0 -118
  382. package/src/core/operators/PDFOperator.ts +0 -72
  383. package/src/core/operators/PDFOperatorNames.ts +0 -92
  384. package/src/core/parser/BaseParser.ts +0 -119
  385. package/src/core/parser/ByteStream.ts +0 -76
  386. package/src/core/parser/PDFObjectParser.ts +0 -302
  387. package/src/core/parser/PDFObjectStreamParser.ts +0 -65
  388. package/src/core/parser/PDFParser.ts +0 -363
  389. package/src/core/parser/PDFXRefStreamParser.ts +0 -129
  390. package/src/core/streams/Ascii85Stream.ts +0 -97
  391. package/src/core/streams/AsciiHexStream.ts +0 -77
  392. package/src/core/streams/DecodeStream.ts +0 -188
  393. package/src/core/streams/DecryptStream.ts +0 -49
  394. package/src/core/streams/FlateStream.ts +0 -397
  395. package/src/core/streams/LZWStream.ts +0 -157
  396. package/src/core/streams/RunLengthStream.ts +0 -55
  397. package/src/core/streams/Stream.ts +0 -126
  398. package/src/core/streams/decode.ts +0 -70
  399. package/src/core/structures/PDFCatalog.ts +0 -81
  400. package/src/core/structures/PDFContentStream.ts +0 -58
  401. package/src/core/structures/PDFCrossRefStream.ts +0 -238
  402. package/src/core/structures/PDFFlateStream.ts +0 -40
  403. package/src/core/structures/PDFObjectStream.ts +0 -91
  404. package/src/core/structures/PDFPageLeaf.ts +0 -244
  405. package/src/core/structures/PDFPageTree.ts +0 -192
  406. package/src/core/syntax/CharCodes.ts +0 -62
  407. package/src/core/syntax/Delimiters.ts +0 -14
  408. package/src/core/syntax/Irregular.ts +0 -10
  409. package/src/core/syntax/Keywords.ts +0 -57
  410. package/src/core/syntax/Numeric.ts +0 -26
  411. package/src/core/syntax/Whitespace.ts +0 -10
  412. package/src/core/writers/PDFStreamWriter.ts +0 -114
  413. package/src/core/writers/PDFWriter.ts +0 -146
  414. package/src/types/fontkit.ts +0 -643
  415. package/src/utils/Cache.ts +0 -26
  416. package/src/utils/arrays.ts +0 -153
  417. package/src/utils/async.ts +0 -8
  418. package/src/utils/base64.ts +0 -98
  419. package/src/utils/elements/Arc.ts +0 -97
  420. package/src/utils/elements/Circle.ts +0 -47
  421. package/src/utils/elements/Ellipse.ts +0 -117
  422. package/src/utils/elements/GraphElement.ts +0 -14
  423. package/src/utils/elements/Line.ts +0 -83
  424. package/src/utils/elements/Plot.ts +0 -50
  425. package/src/utils/elements/Point.ts +0 -37
  426. package/src/utils/elements/Rectangle.ts +0 -65
  427. package/src/utils/elements/Segment.ts +0 -81
  428. package/src/utils/errors.ts +0 -3
  429. package/src/utils/intersections.ts +0 -237
  430. package/src/utils/maths.ts +0 -96
  431. package/src/utils/numbers.ts +0 -55
  432. package/src/utils/objects.ts +0 -13
  433. package/src/utils/pdfDocEncoding.ts +0 -69
  434. package/src/utils/png.ts +0 -100
  435. package/src/utils/rng.ts +0 -21
  436. package/src/utils/strings.ts +0 -191
  437. package/src/utils/validators.ts +0 -192
  438. package/tsconfig.build.json +0 -14
  439. package/tsconfig.json +0 -15
  440. package/vite.config.mts +0 -39
  441. /package/{src/api/form/index.ts → dist/api/form/index.d.ts} +0 -0
  442. /package/{src/api/image/index.ts → dist/api/image/index.d.ts} +0 -0
  443. /package/{src/api/index.ts → dist/api/index.d.ts} +0 -0
  444. /package/{src/api/text/index.ts → dist/api/text/index.d.ts} +0 -0
  445. /package/{src/core/acroform/index.ts → dist/core/acroform/index.d.ts} +0 -0
  446. /package/{src/core/annotation/index.ts → dist/core/annotation/index.d.ts} +0 -0
  447. /package/{src/index.ts → dist/index.d.ts} +0 -0
  448. /package/{src/utils/elements/index.ts → dist/utils/elements/index.d.ts} +0 -0
  449. /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
@@ -1,842 +0,0 @@
1
- import PDFDocument from '../PDFDocument';
2
- import PDFPage from '../PDFPage';
3
- import PDFField from './PDFField';
4
- import PDFButton from './PDFButton';
5
- import PDFCheckBox from './PDFCheckBox';
6
- import PDFDropdown from './PDFDropdown';
7
- import PDFOptionList from './PDFOptionList';
8
- import PDFRadioGroup from './PDFRadioGroup';
9
- import PDFSignature from './PDFSignature';
10
- import PDFTextField from './PDFTextField';
11
- import {
12
- NoSuchFieldError,
13
- UnexpectedFieldTypeError,
14
- FieldAlreadyExistsError,
15
- InvalidFieldNamePartError,
16
- } from '../errors';
17
- import PDFFont from '../PDFFont';
18
- import { StandardFonts } from '../StandardFonts';
19
- import { rotateInPlace } from '../operations';
20
- import { drawObject, popGraphicsState, pushGraphicsState, translate } from '../operators';
21
- import {
22
- PDFAcroForm,
23
- PDFAcroField,
24
- PDFAcroCheckBox,
25
- PDFAcroComboBox,
26
- PDFAcroListBox,
27
- PDFAcroRadioButton,
28
- PDFAcroSignature,
29
- PDFAcroText,
30
- PDFAcroPushButton,
31
- PDFAcroNonTerminal,
32
- PDFDict,
33
- PDFOperator,
34
- PDFRef,
35
- createPDFAcroFields,
36
- PDFName,
37
- PDFWidgetAnnotation,
38
- } from '../../core';
39
- import { assertIs, Cache, assertOrUndefined } from '../../utils';
40
-
41
- export interface FlattenOptions {
42
- updateFieldAppearances: boolean;
43
- }
44
-
45
- /**
46
- * Represents the interactive form of a [[PDFDocument]].
47
- *
48
- * Interactive forms (sometimes called _AcroForms_) are collections of fields
49
- * designed to gather information from a user. A PDF document may contains any
50
- * number of fields that appear on various pages, all of which make up a single,
51
- * global interactive form spanning the entire document. This means that
52
- * instances of [[PDFDocument]] shall contain at most one [[PDFForm]].
53
- *
54
- * The fields of an interactive form are represented by [[PDFField]] instances.
55
- */
56
- export default class PDFForm {
57
- /**
58
- * > **NOTE:** You probably don't want to call this method directly. Instead,
59
- * > consider using the [[PDFDocument.getForm]] method, which will create an
60
- * > instance of [[PDFForm]] for you.
61
- *
62
- * Create an instance of [[PDFForm]] from an existing acroForm and embedder
63
- *
64
- * @param acroForm The underlying `PDFAcroForm` for this form.
65
- * @param doc The document to which the form will belong.
66
- */
67
- static of = (acroForm: PDFAcroForm, doc: PDFDocument) => new PDFForm(acroForm, doc);
68
-
69
- /** The low-level PDFAcroForm wrapped by this form. */
70
- readonly acroForm: PDFAcroForm;
71
-
72
- /** The document to which this form belongs. */
73
- readonly doc: PDFDocument;
74
-
75
- private readonly dirtyFields: Set<PDFRef>;
76
- private readonly defaultFontCache: Cache<PDFFont>;
77
-
78
- private constructor(acroForm: PDFAcroForm, doc: PDFDocument) {
79
- assertIs(acroForm, 'acroForm', [[PDFAcroForm, 'PDFAcroForm']]);
80
- assertIs(doc, 'doc', [[PDFDocument, 'PDFDocument']]);
81
-
82
- this.acroForm = acroForm;
83
- this.doc = doc;
84
-
85
- this.dirtyFields = new Set();
86
- this.defaultFontCache = Cache.populatedBy(this.embedDefaultFont);
87
- }
88
-
89
- /**
90
- * Returns `true` if this [[PDFForm]] has XFA data. Most PDFs with form
91
- * fields do not use XFA as it is not widely supported by PDF readers.
92
- *
93
- * > `pdf-lib` does not support creation, modification, or reading of XFA
94
- * > fields.
95
- *
96
- * For example:
97
- * ```js
98
- * const form = pdfDoc.getForm()
99
- * if (form.hasXFA()) console.log('PDF has XFA data')
100
- * ```
101
- * @returns Whether or not this form has XFA data.
102
- */
103
- hasXFA(): boolean {
104
- return this.acroForm.dict.has(PDFName.of('XFA'));
105
- }
106
-
107
- /**
108
- * Disconnect the XFA data from this [[PDFForm]] (if any exists). This will
109
- * force readers to fallback to standard fields if the [[PDFDocument]]
110
- * contains any. For example:
111
- *
112
- * For example:
113
- * ```js
114
- * const form = pdfDoc.getForm()
115
- * form.deleteXFA()
116
- * ```
117
- */
118
- deleteXFA(): void {
119
- this.acroForm.dict.delete(PDFName.of('XFA'));
120
- }
121
-
122
- /**
123
- * Get all fields contained in this [[PDFForm]]. For example:
124
- * ```js
125
- * const form = pdfDoc.getForm()
126
- * const fields = form.getFields()
127
- * fields.forEach(field => {
128
- * const type = field.constructor.name
129
- * const name = field.getName()
130
- * console.log(`${type}: ${name}`)
131
- * })
132
- * ```
133
- * @returns An array of all fields in this form.
134
- */
135
- getFields(): PDFField[] {
136
- const allFields = this.acroForm.getAllFields();
137
-
138
- const fields: PDFField[] = [];
139
- for (let idx = 0, len = allFields.length; idx < len; idx++) {
140
- const [acroField, ref] = allFields[idx];
141
- const field = convertToPDFField(acroField, ref, this.doc);
142
- if (field) fields.push(field);
143
- }
144
-
145
- return fields;
146
- }
147
-
148
- /**
149
- * Get the field in this [[PDFForm]] with the given name. For example:
150
- * ```js
151
- * const form = pdfDoc.getForm()
152
- * const field = form.getFieldMaybe('Page1.Foo.Bar[0]')
153
- * if (field) console.log('Field exists!')
154
- * ```
155
- * @param name A fully qualified field name.
156
- * @returns The field with the specified name, if one exists.
157
- */
158
- getFieldMaybe(name: string): PDFField | undefined {
159
- assertIs(name, 'name', ['string']);
160
- const fields = this.getFields();
161
- for (let idx = 0, len = fields.length; idx < len; idx++) {
162
- const field = fields[idx];
163
- if (field.getName() === name) return field;
164
- }
165
- return undefined;
166
- }
167
-
168
- /**
169
- * Get the field in this [[PDFForm]] with the given name. For example:
170
- * ```js
171
- * const form = pdfDoc.getForm()
172
- * const field = form.getField('Page1.Foo.Bar[0]')
173
- * ```
174
- * If no field exists with the provided name, an error will be thrown.
175
- * @param name A fully qualified field name.
176
- * @returns The field with the specified name.
177
- */
178
- getField(name: string): PDFField {
179
- assertIs(name, 'name', ['string']);
180
- const field = this.getFieldMaybe(name);
181
- if (field) return field;
182
- throw new NoSuchFieldError(name);
183
- }
184
-
185
- /**
186
- * Get the button field in this [[PDFForm]] with the given name. For example:
187
- * ```js
188
- * const form = pdfDoc.getForm()
189
- * const button = form.getButton('Page1.Foo.Button[0]')
190
- * ```
191
- * An error will be thrown if no field exists with the provided name, or if
192
- * the field exists but is not a button.
193
- * @param name A fully qualified button name.
194
- * @returns The button with the specified name.
195
- */
196
- getButton(name: string): PDFButton {
197
- assertIs(name, 'name', ['string']);
198
- const field = this.getField(name);
199
- if (field instanceof PDFButton) return field;
200
- throw new UnexpectedFieldTypeError(name, PDFButton, field);
201
- }
202
-
203
- /**
204
- * Get the check box field in this [[PDFForm]] with the given name.
205
- * For example:
206
- * ```js
207
- * const form = pdfDoc.getForm()
208
- * const checkBox = form.getCheckBox('Page1.Foo.CheckBox[0]')
209
- * checkBox.check()
210
- * ```
211
- * An error will be thrown if no field exists with the provided name, or if
212
- * the field exists but is not a check box.
213
- * @param name A fully qualified check box name.
214
- * @returns The check box with the specified name.
215
- */
216
- getCheckBox(name: string): PDFCheckBox {
217
- assertIs(name, 'name', ['string']);
218
- const field = this.getField(name);
219
- if (field instanceof PDFCheckBox) return field;
220
- throw new UnexpectedFieldTypeError(name, PDFCheckBox, field);
221
- }
222
-
223
- /**
224
- * Get the dropdown field in this [[PDFForm]] with the given name.
225
- * For example:
226
- * ```js
227
- * const form = pdfDoc.getForm()
228
- * const dropdown = form.getDropdown('Page1.Foo.Dropdown[0]')
229
- * const options = dropdown.getOptions()
230
- * dropdown.select(options[0])
231
- * ```
232
- * An error will be thrown if no field exists with the provided name, or if
233
- * the field exists but is not a dropdown.
234
- * @param name A fully qualified dropdown name.
235
- * @returns The dropdown with the specified name.
236
- */
237
- getDropdown(name: string): PDFDropdown {
238
- assertIs(name, 'name', ['string']);
239
- const field = this.getField(name);
240
- if (field instanceof PDFDropdown) return field;
241
- throw new UnexpectedFieldTypeError(name, PDFDropdown, field);
242
- }
243
-
244
- /**
245
- * Get the option list field in this [[PDFForm]] with the given name.
246
- * For example:
247
- * ```js
248
- * const form = pdfDoc.getForm()
249
- * const optionList = form.getOptionList('Page1.Foo.OptionList[0]')
250
- * const options = optionList.getOptions()
251
- * optionList.select(options[0])
252
- * ```
253
- * An error will be thrown if no field exists with the provided name, or if
254
- * the field exists but is not an option list.
255
- * @param name A fully qualified option list name.
256
- * @returns The option list with the specified name.
257
- */
258
- getOptionList(name: string): PDFOptionList {
259
- assertIs(name, 'name', ['string']);
260
- const field = this.getField(name);
261
- if (field instanceof PDFOptionList) return field;
262
- throw new UnexpectedFieldTypeError(name, PDFOptionList, field);
263
- }
264
-
265
- /**
266
- * Get the radio group field in this [[PDFForm]] with the given name.
267
- * For example:
268
- * ```js
269
- * const form = pdfDoc.getForm()
270
- * const radioGroup = form.getRadioGroup('Page1.Foo.RadioGroup[0]')
271
- * const options = radioGroup.getOptions()
272
- * radioGroup.select(options[0])
273
- * ```
274
- * An error will be thrown if no field exists with the provided name, or if
275
- * the field exists but is not a radio group.
276
- * @param name A fully qualified radio group name.
277
- * @returns The radio group with the specified name.
278
- */
279
- getRadioGroup(name: string): PDFRadioGroup {
280
- assertIs(name, 'name', ['string']);
281
- const field = this.getField(name);
282
- if (field instanceof PDFRadioGroup) return field;
283
- throw new UnexpectedFieldTypeError(name, PDFRadioGroup, field);
284
- }
285
-
286
- /**
287
- * Get the signature field in this [[PDFForm]] with the given name.
288
- * For example:
289
- * ```js
290
- * const form = pdfDoc.getForm()
291
- * const signature = form.getSignature('Page1.Foo.Signature[0]')
292
- * ```
293
- * An error will be thrown if no field exists with the provided name, or if
294
- * the field exists but is not a signature.
295
- * @param name A fully qualified signature name.
296
- * @returns The signature with the specified name.
297
- */
298
- getSignature(name: string): PDFSignature {
299
- assertIs(name, 'name', ['string']);
300
- const field = this.getField(name);
301
- if (field instanceof PDFSignature) return field;
302
- throw new UnexpectedFieldTypeError(name, PDFSignature, field);
303
- }
304
-
305
- /**
306
- * Get the text field in this [[PDFForm]] with the given name.
307
- * For example:
308
- * ```js
309
- * const form = pdfDoc.getForm()
310
- * const textField = form.getTextField('Page1.Foo.TextField[0]')
311
- * textField.setText('Are you designed to act or to be acted upon?')
312
- * ```
313
- * An error will be thrown if no field exists with the provided name, or if
314
- * the field exists but is not a text field.
315
- * @param name A fully qualified text field name.
316
- * @returns The text field with the specified name.
317
- */
318
- getTextField(name: string): PDFTextField {
319
- assertIs(name, 'name', ['string']);
320
- const field = this.getField(name);
321
- if (field instanceof PDFTextField) return field;
322
- throw new UnexpectedFieldTypeError(name, PDFTextField, field);
323
- }
324
-
325
- /**
326
- * Create a new button field in this [[PDFForm]] with the given name.
327
- * For example:
328
- * ```js
329
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
330
- * const page = pdfDoc.addPage()
331
- *
332
- * const form = pdfDoc.getForm()
333
- * const button = form.createButton('cool.new.button')
334
- *
335
- * button.addToPage('Do Stuff', font, page)
336
- * ```
337
- * An error will be thrown if a field already exists with the provided name.
338
- * @param name The fully qualified name for the new button.
339
- * @returns The new button field.
340
- */
341
- createButton(name: string): PDFButton {
342
- assertIs(name, 'name', ['string']);
343
-
344
- const nameParts = splitFieldName(name);
345
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
346
-
347
- const button = PDFAcroPushButton.create(this.doc.context);
348
- button.setPartialName(nameParts.terminal);
349
-
350
- addFieldToParent(parent, [button, button.ref], nameParts.terminal);
351
-
352
- return PDFButton.of(button, button.ref, this.doc);
353
- }
354
-
355
- /**
356
- * Create a new check box field in this [[PDFForm]] with the given name.
357
- * For example:
358
- * ```js
359
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
360
- * const page = pdfDoc.addPage()
361
- *
362
- * const form = pdfDoc.getForm()
363
- * const checkBox = form.createCheckBox('cool.new.checkBox')
364
- *
365
- * checkBox.addToPage(page)
366
- * ```
367
- * An error will be thrown if a field already exists with the provided name.
368
- * @param name The fully qualified name for the new check box.
369
- * @returns The new check box field.
370
- */
371
- createCheckBox(name: string): PDFCheckBox {
372
- assertIs(name, 'name', ['string']);
373
-
374
- const nameParts = splitFieldName(name);
375
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
376
-
377
- const checkBox = PDFAcroCheckBox.create(this.doc.context);
378
- checkBox.setPartialName(nameParts.terminal);
379
-
380
- addFieldToParent(parent, [checkBox, checkBox.ref], nameParts.terminal);
381
-
382
- return PDFCheckBox.of(checkBox, checkBox.ref, this.doc);
383
- }
384
-
385
- /**
386
- * Create a new dropdown field in this [[PDFForm]] with the given name.
387
- * For example:
388
- * ```js
389
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
390
- * const page = pdfDoc.addPage()
391
- *
392
- * const form = pdfDoc.getForm()
393
- * const dropdown = form.createDropdown('cool.new.dropdown')
394
- *
395
- * dropdown.addToPage(font, page)
396
- * ```
397
- * An error will be thrown if a field already exists with the provided name.
398
- * @param name The fully qualified name for the new dropdown.
399
- * @returns The new dropdown field.
400
- */
401
- createDropdown(name: string): PDFDropdown {
402
- assertIs(name, 'name', ['string']);
403
-
404
- const nameParts = splitFieldName(name);
405
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
406
-
407
- const comboBox = PDFAcroComboBox.create(this.doc.context);
408
- comboBox.setPartialName(nameParts.terminal);
409
-
410
- addFieldToParent(parent, [comboBox, comboBox.ref], nameParts.terminal);
411
-
412
- return PDFDropdown.of(comboBox, comboBox.ref, this.doc);
413
- }
414
-
415
- /**
416
- * Create a new option list field in this [[PDFForm]] with the given name.
417
- * For example:
418
- * ```js
419
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
420
- * const page = pdfDoc.addPage()
421
- *
422
- * const form = pdfDoc.getForm()
423
- * const optionList = form.createOptionList('cool.new.optionList')
424
- *
425
- * optionList.addToPage(font, page)
426
- * ```
427
- * An error will be thrown if a field already exists with the provided name.
428
- * @param name The fully qualified name for the new option list.
429
- * @returns The new option list field.
430
- */
431
- createOptionList(name: string): PDFOptionList {
432
- assertIs(name, 'name', ['string']);
433
-
434
- const nameParts = splitFieldName(name);
435
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
436
-
437
- const listBox = PDFAcroListBox.create(this.doc.context);
438
- listBox.setPartialName(nameParts.terminal);
439
-
440
- addFieldToParent(parent, [listBox, listBox.ref], nameParts.terminal);
441
-
442
- return PDFOptionList.of(listBox, listBox.ref, this.doc);
443
- }
444
-
445
- /**
446
- * Create a new radio group field in this [[PDFForm]] with the given name.
447
- * For example:
448
- * ```js
449
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
450
- * const page = pdfDoc.addPage()
451
- *
452
- * const form = pdfDoc.getForm()
453
- * const radioGroup = form.createRadioGroup('cool.new.radioGroup')
454
- *
455
- * radioGroup.addOptionToPage('is-dog', page, { y: 0 })
456
- * radioGroup.addOptionToPage('is-cat', page, { y: 75 })
457
- * ```
458
- * An error will be thrown if a field already exists with the provided name.
459
- * @param name The fully qualified name for the new radio group.
460
- * @returns The new radio group field.
461
- */
462
- createRadioGroup(name: string): PDFRadioGroup {
463
- assertIs(name, 'name', ['string']);
464
- const nameParts = splitFieldName(name);
465
-
466
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
467
-
468
- const radioButton = PDFAcroRadioButton.create(this.doc.context);
469
- radioButton.setPartialName(nameParts.terminal);
470
-
471
- addFieldToParent(parent, [radioButton, radioButton.ref], nameParts.terminal);
472
-
473
- return PDFRadioGroup.of(radioButton, radioButton.ref, this.doc);
474
- }
475
-
476
- /**
477
- * Create a new text field in this [[PDFForm]] with the given name.
478
- * For example:
479
- * ```js
480
- * const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
481
- * const page = pdfDoc.addPage()
482
- *
483
- * const form = pdfDoc.getForm()
484
- * const textField = form.createTextField('cool.new.textField')
485
- *
486
- * textField.addToPage(font, page)
487
- * ```
488
- * An error will be thrown if a field already exists with the provided name.
489
- * @param name The fully qualified name for the new radio group.
490
- * @returns The new radio group field.
491
- */
492
- createTextField(name: string): PDFTextField {
493
- assertIs(name, 'name', ['string']);
494
- const nameParts = splitFieldName(name);
495
-
496
- const parent = this.findOrCreateNonTerminals(nameParts.nonTerminal);
497
-
498
- const text = PDFAcroText.create(this.doc.context);
499
- text.setPartialName(nameParts.terminal);
500
-
501
- addFieldToParent(parent, [text, text.ref], nameParts.terminal);
502
-
503
- return PDFTextField.of(text, text.ref, this.doc);
504
- }
505
-
506
- /**
507
- * Flatten all fields in this [[PDFForm]].
508
- *
509
- * Flattening a form field will take the current appearance for each of that
510
- * field's widgets and make them part of their page's content stream. All form
511
- * fields and annotations associated are then removed. Note that once a form
512
- * has been flattened its fields can no longer be accessed or edited.
513
- *
514
- * This operation is often used after filling form fields to ensure a
515
- * consistent appearance across different PDF readers and/or printers.
516
- * Another common use case is to copy a template document with form fields
517
- * into another document. In this scenario you would load the template
518
- * document, fill its fields, flatten it, and then copy its pages into the
519
- * recipient document - the filled fields will be copied over.
520
- *
521
- * For example:
522
- * ```js
523
- * const form = pdfDoc.getForm();
524
- * form.flatten();
525
- * ```
526
- */
527
- flatten(options: FlattenOptions = { updateFieldAppearances: true }) {
528
- if (options.updateFieldAppearances) {
529
- this.updateFieldAppearances();
530
- }
531
-
532
- const fields = this.getFields();
533
-
534
- for (let i = 0, lenFields = fields.length; i < lenFields; i++) {
535
- const field = fields[i];
536
- const widgets = field.acroField.getWidgets();
537
-
538
- for (let j = 0, lenWidgets = widgets.length; j < lenWidgets; j++) {
539
- try {
540
- const widget = widgets[j];
541
- const page = this.findWidgetPage(widget);
542
- const widgetRef = this.findWidgetAppearanceRef(field, widget);
543
-
544
- const xObjectKey = page.node.newXObject('FlatWidget', widgetRef);
545
-
546
- const rectangle = widget.getRectangle();
547
- const operators = [
548
- pushGraphicsState(),
549
- translate(rectangle.x, rectangle.y),
550
- ...rotateInPlace({ ...rectangle, rotation: 0 }),
551
- drawObject(xObjectKey),
552
- popGraphicsState(),
553
- ].filter(Boolean) as PDFOperator[];
554
-
555
- page.pushOperators(...operators);
556
- } catch (err) {
557
- console.error(err);
558
- }
559
- }
560
-
561
- this.removeField(field);
562
- }
563
- }
564
-
565
- /**
566
- * Remove a field from this [[PDFForm]].
567
- *
568
- * For example:
569
- * ```js
570
- * const form = pdfDoc.getForm();
571
- * const ageField = form.getFields().find(x => x.getName() === 'Age');
572
- * form.removeField(ageField);
573
- * ```
574
- */
575
- removeField(field: PDFField) {
576
- const widgets = field.acroField.getWidgets();
577
- const pages: Set<PDFPage> = new Set();
578
-
579
- for (let i = 0, len = widgets.length; i < len; i++) {
580
- try {
581
- const widget = widgets[i];
582
- const widgetRef = this.findWidgetAppearanceRef(field, widget);
583
-
584
- const page = this.findWidgetPage(widget);
585
- pages.add(page);
586
-
587
- page.node.removeAnnot(widgetRef);
588
- } catch (err) {
589
- console.error(err);
590
- }
591
- }
592
-
593
- pages.forEach((page) => page.node.removeAnnot(field.ref));
594
- this.acroForm.removeField(field.acroField);
595
- const fieldKids = field.acroField.normalizedEntries().Kids;
596
- const kidsCount = fieldKids.size();
597
- for (let childIndex = 0; childIndex < kidsCount; childIndex++) {
598
- const child = fieldKids.get(childIndex);
599
- if (child instanceof PDFRef) {
600
- this.doc.context.delete(child);
601
- }
602
- }
603
- this.doc.context.delete(field.ref);
604
- }
605
-
606
- /**
607
- * Update the appearance streams for all widgets of all fields in this
608
- * [[PDFForm]]. Appearance streams will only be created for a widget if it
609
- * does not have any existing appearance streams, or the field's value has
610
- * changed (e.g. by calling [[PDFTextField.setText]] or
611
- * [[PDFDropdown.select]]).
612
- *
613
- * For example:
614
- * ```js
615
- * const courier = await pdfDoc.embedFont(StandardFonts.Courier)
616
- * const form = pdfDoc.getForm()
617
- * form.updateFieldAppearances(courier)
618
- * ```
619
- *
620
- * **IMPORTANT:** The default value for the `font` parameter is
621
- * [[StandardFonts.Helvetica]]. Note that this is a WinAnsi font. This means
622
- * that encoding errors will be thrown if any fields contain text with
623
- * characters outside the WinAnsi character set (the latin alphabet).
624
- *
625
- * Embedding a custom font and passing that as the `font`
626
- * parameter allows you to generate appearance streams with non WinAnsi
627
- * characters (assuming your custom font supports them).
628
- *
629
- * > **NOTE:** The [[PDFDocument.save]] method will call this method to
630
- * > update appearances automatically if a form was accessed via the
631
- * > [[PDFDocument.getForm]] method prior to saving.
632
- *
633
- * @param font Optionally, the font to use when creating new appearances.
634
- */
635
- updateFieldAppearances(font?: PDFFont) {
636
- assertOrUndefined(font, 'font', [[PDFFont, 'PDFFont']]);
637
-
638
- font = font ?? this.getDefaultFont();
639
-
640
- const fields = this.getFields();
641
-
642
- for (let idx = 0, len = fields.length; idx < len; idx++) {
643
- const field = fields[idx];
644
- if (field.needsAppearancesUpdate()) {
645
- field.defaultUpdateAppearances(font);
646
- }
647
- }
648
- }
649
-
650
- /**
651
- * Mark a field as dirty. This will cause its appearance streams to be
652
- * updated by [[PDFForm.updateFieldAppearances]].
653
- * ```js
654
- * const form = pdfDoc.getForm()
655
- * const field = form.getField('foo.bar')
656
- * form.markFieldAsDirty(field.ref)
657
- * ```
658
- * @param fieldRef The reference to the field that should be marked.
659
- */
660
- markFieldAsDirty(fieldRef: PDFRef) {
661
- assertOrUndefined(fieldRef, 'fieldRef', [[PDFRef, 'PDFRef']]);
662
- this.dirtyFields.add(fieldRef);
663
- }
664
-
665
- /**
666
- * Mark a field as dirty. This will cause its appearance streams to not be
667
- * updated by [[PDFForm.updateFieldAppearances]].
668
- * ```js
669
- * const form = pdfDoc.getForm()
670
- * const field = form.getField('foo.bar')
671
- * form.markFieldAsClean(field.ref)
672
- * ```
673
- * @param fieldRef The reference to the field that should be marked.
674
- */
675
- markFieldAsClean(fieldRef: PDFRef) {
676
- assertOrUndefined(fieldRef, 'fieldRef', [[PDFRef, 'PDFRef']]);
677
- this.dirtyFields.delete(fieldRef);
678
- }
679
-
680
- /**
681
- * Returns `true` is the specified field has been marked as dirty.
682
- * ```js
683
- * const form = pdfDoc.getForm()
684
- * const field = form.getField('foo.bar')
685
- * if (form.fieldIsDirty(field.ref)) console.log('Field is dirty')
686
- * ```
687
- * @param fieldRef The reference to the field that should be checked.
688
- * @returns Whether or not the specified field is dirty.
689
- */
690
- fieldIsDirty(fieldRef: PDFRef): boolean {
691
- assertOrUndefined(fieldRef, 'fieldRef', [[PDFRef, 'PDFRef']]);
692
- return this.dirtyFields.has(fieldRef);
693
- }
694
-
695
- getDefaultFont() {
696
- return this.defaultFontCache.access();
697
- }
698
-
699
- private findWidgetPage(widget: PDFWidgetAnnotation): PDFPage {
700
- const pageRef = widget.P();
701
- let page = this.doc.getPages().find((x) => x.ref === pageRef);
702
- if (page === undefined) {
703
- const widgetRef = this.doc.context.getObjectRef(widget.dict);
704
- if (widgetRef === undefined) {
705
- throw new Error('Could not find PDFRef for PDFObject');
706
- }
707
-
708
- page = this.doc.findPageForAnnotationRef(widgetRef);
709
-
710
- if (page === undefined) {
711
- throw new Error(`Could not find page for PDFRef ${widgetRef}`);
712
- }
713
- }
714
-
715
- return page;
716
- }
717
-
718
- private findWidgetAppearanceRef(field: PDFField, widget: PDFWidgetAnnotation): PDFRef {
719
- let refOrDict = widget.getNormalAppearance();
720
-
721
- if (
722
- refOrDict instanceof PDFDict &&
723
- (field instanceof PDFCheckBox || field instanceof PDFRadioGroup)
724
- ) {
725
- const value = field.acroField.getValue();
726
- const ref = refOrDict.get(value) ?? refOrDict.get(PDFName.of('Off'));
727
-
728
- if (ref instanceof PDFRef) {
729
- refOrDict = ref;
730
- }
731
- }
732
-
733
- if (!(refOrDict instanceof PDFRef)) {
734
- const name = field.getName();
735
- throw new Error(`Failed to extract appearance ref for: ${name}`);
736
- }
737
-
738
- return refOrDict;
739
- }
740
-
741
- private findOrCreateNonTerminals(partialNames: string[]) {
742
- let nonTerminal: [PDFAcroForm] | [PDFAcroNonTerminal, PDFRef] = [this.acroForm];
743
- for (let idx = 0, len = partialNames.length; idx < len; idx++) {
744
- const namePart = partialNames[idx];
745
- if (!namePart) throw new InvalidFieldNamePartError(namePart);
746
- const [parent, parentRef] = nonTerminal;
747
- const res = this.findNonTerminal(namePart, parent);
748
-
749
- if (res) {
750
- nonTerminal = res;
751
- } else {
752
- const node = PDFAcroNonTerminal.create(this.doc.context);
753
- node.setPartialName(namePart);
754
- node.setParent(parentRef);
755
- const nodeRef = this.doc.context.register(node.dict);
756
- parent.addField(nodeRef);
757
- nonTerminal = [node, nodeRef];
758
- }
759
- }
760
- return nonTerminal;
761
- }
762
-
763
- private findNonTerminal(
764
- partialName: string,
765
- parent: PDFAcroForm | PDFAcroNonTerminal,
766
- ): [PDFAcroNonTerminal, PDFRef] | undefined {
767
- const fields =
768
- parent instanceof PDFAcroForm
769
- ? this.acroForm.getFields()
770
- : createPDFAcroFields(parent.Kids());
771
-
772
- for (let idx = 0, len = fields.length; idx < len; idx++) {
773
- const [field, ref] = fields[idx];
774
- if (field.getPartialName() === partialName) {
775
- if (field instanceof PDFAcroNonTerminal) return [field, ref];
776
- throw new FieldAlreadyExistsError(partialName);
777
- }
778
- }
779
-
780
- return undefined;
781
- }
782
-
783
- private embedDefaultFont = (): PDFFont => this.doc.embedStandardFont(StandardFonts.Helvetica);
784
- }
785
-
786
- const convertToPDFField = (
787
- field: PDFAcroField,
788
- ref: PDFRef,
789
- doc: PDFDocument,
790
- ): PDFField | undefined => {
791
- if (field instanceof PDFAcroPushButton) return PDFButton.of(field, ref, doc);
792
- if (field instanceof PDFAcroCheckBox) return PDFCheckBox.of(field, ref, doc);
793
- if (field instanceof PDFAcroComboBox) return PDFDropdown.of(field, ref, doc);
794
- if (field instanceof PDFAcroListBox) return PDFOptionList.of(field, ref, doc);
795
- if (field instanceof PDFAcroText) return PDFTextField.of(field, ref, doc);
796
- if (field instanceof PDFAcroRadioButton) {
797
- return PDFRadioGroup.of(field, ref, doc);
798
- }
799
- if (field instanceof PDFAcroSignature) {
800
- return PDFSignature.of(field, ref, doc);
801
- }
802
- return undefined;
803
- };
804
-
805
- const splitFieldName = (fullyQualifiedName: string) => {
806
- if (fullyQualifiedName.length === 0) {
807
- throw new Error('PDF field names must not be empty strings');
808
- }
809
-
810
- const parts = fullyQualifiedName.split('.');
811
-
812
- for (let idx = 0, len = parts.length; idx < len; idx++) {
813
- if (parts[idx] === '') {
814
- throw new Error(
815
- `Periods in PDF field names must be separated by at least one character: "${fullyQualifiedName}"`,
816
- );
817
- }
818
- }
819
-
820
- if (parts.length === 1) return { nonTerminal: [], terminal: parts[0] };
821
-
822
- return {
823
- nonTerminal: parts.slice(0, parts.length - 1),
824
- terminal: parts[parts.length - 1],
825
- };
826
- };
827
-
828
- const addFieldToParent = (
829
- [parent, parentRef]: [PDFAcroForm] | [PDFAcroNonTerminal, PDFRef],
830
- [field, fieldRef]: [PDFAcroField, PDFRef],
831
- partialName: string,
832
- ) => {
833
- const entries = parent.normalizedEntries();
834
- const fields = createPDFAcroFields('Kids' in entries ? entries.Kids : entries.Fields);
835
- for (let idx = 0, len = fields.length; idx < len; idx++) {
836
- if (fields[idx][0].getPartialName() === partialName) {
837
- throw new FieldAlreadyExistsError(partialName);
838
- }
839
- }
840
- parent.addField(fieldRef);
841
- field.setParent(parentRef);
842
- };