@@ -10,6 +10,7 @@ import PDFFont from 'src/api/PDFFont';
10
10
import PDFImage from 'src/api/PDFImage' ;
11
11
import PDFPage from 'src/api/PDFPage' ;
12
12
import PDFForm from 'src/api/form/PDFForm' ;
13
+ import PDFSeparation from 'src/api/PDFSeparation' ;
13
14
import { PageSizes } from 'src/api/sizes' ;
14
15
import { StandardFonts } from 'src/api/StandardFonts' ;
15
16
import {
@@ -33,6 +34,7 @@ import {
33
34
PDFWriter ,
34
35
PngEmbedder ,
35
36
StandardFontEmbedder ,
37
+ SeparationEmbedder ,
36
38
UnexpectedObjectTypeError ,
37
39
} from 'src/core' ;
38
40
import {
@@ -61,11 +63,13 @@ import {
61
63
pluckIndices ,
62
64
range ,
63
65
toUint8Array ,
66
+ error ,
64
67
} from 'src/utils' ;
65
68
import FileEmbedder , { AFRelationship } from 'src/core/embedders/FileEmbedder' ;
66
69
import PDFEmbeddedFile from 'src/api/PDFEmbeddedFile' ;
67
70
import PDFJavaScript from 'src/api/PDFJavaScript' ;
68
71
import JavaScriptEmbedder from 'src/core/embedders/JavaScriptEmbedder' ;
72
+ import { Color , ColorTypes , colorToComponents } from './colors' ;
69
73
70
74
/**
71
75
* Represents a PDF document.
@@ -185,6 +189,7 @@ export default class PDFDocument {
185
189
private readonly formCache : Cache < PDFForm > ;
186
190
private readonly fonts : PDFFont [ ] ;
187
191
private readonly images : PDFImage [ ] ;
192
+ private readonly separationColorSpaces : PDFSeparation [ ] ;
188
193
private readonly embeddedPages : PDFEmbeddedPage [ ] ;
189
194
private readonly embeddedFiles : PDFEmbeddedFile [ ] ;
190
195
private readonly javaScripts : PDFJavaScript [ ] ;
@@ -206,6 +211,7 @@ export default class PDFDocument {
206
211
this . formCache = Cache . populatedBy ( this . getOrCreateForm ) ;
207
212
this . fonts = [ ] ;
208
213
this . images = [ ] ;
214
+ this . separationColorSpaces = [ ] ;
209
215
this . embeddedPages = [ ] ;
210
216
this . embeddedFiles = [ ] ;
211
217
this . javaScripts = [ ] ;
@@ -997,6 +1003,32 @@ export default class PDFDocument {
997
1003
return pdfFont ;
998
1004
}
999
1005
1006
+ /**
1007
+ * Embed a separation color space into this document.
1008
+ * For example:
1009
+ * ```js
1010
+ * import { rgb } from 'pdf-lib'
1011
+ * const separation = pdfDoc.embedSeparation('PANTONE 123 C', rgb(1, 0, 0))
1012
+ * ```
1013
+ *
1014
+ * @param name The name of the separation color space.
1015
+ * @param alternate An alternate color to be used to approximate the intended
1016
+ * color.
1017
+ */
1018
+ embedSeparation ( name : string , alternate : Color ) : PDFSeparation {
1019
+ const ref = this . context . nextRef ( ) ;
1020
+ const alternateColorSpace = getColorSpace ( alternate ) ;
1021
+ const alternateColorComponents = colorToComponents ( alternate ) ;
1022
+ const embedder = SeparationEmbedder . for (
1023
+ name ,
1024
+ alternateColorSpace ,
1025
+ alternateColorComponents ,
1026
+ ) ;
1027
+ const separation = PDFSeparation . of ( ref , this , embedder ) ;
1028
+ this . separationColorSpaces . push ( separation ) ;
1029
+ return separation ;
1030
+ }
1031
+
1000
1032
/**
1001
1033
* Embed a JPEG image into this document. The input data can be provided in
1002
1034
* multiple formats:
@@ -1243,6 +1275,7 @@ export default class PDFDocument {
1243
1275
async flush ( ) : Promise < void > {
1244
1276
await this . embedAll ( this . fonts ) ;
1245
1277
await this . embedAll ( this . images ) ;
1278
+ await this . embedAll ( this . separationColorSpaces ) ;
1246
1279
await this . embedAll ( this . embeddedPages ) ;
1247
1280
await this . embedAll ( this . embeddedFiles ) ;
1248
1281
await this . embedAll ( this . javaScripts ) ;
@@ -1393,3 +1426,10 @@ function assertIsLiteralOrHexString(
1393
1426
throw new UnexpectedObjectTypeError ( [ PDFHexString , PDFString ] , pdfObject ) ;
1394
1427
}
1395
1428
}
1429
+
1430
+ // prettier-ignore
1431
+ const getColorSpace = ( color : Color ) =>
1432
+ color . type === ColorTypes . Grayscale ? 'DeviceGray'
1433
+ : color . type === ColorTypes . RGB ? 'DeviceRGB'
1434
+ : color . type === ColorTypes . CMYK ? 'DeviceCMYK'
1435
+ : error ( `Invalid alternate color: ${ JSON . stringify ( color ) } ` ) ;
0 commit comments