Persits Software, Inc. Web Site
 Navigator:  Home |  Manual |  Chapter 15: Color Spaces
Chapter 16: IE-based HTML-to-PDF Conversion Chapter 14: PDF to Image Conversion
  Chapter 15: Color Spaces
15.1 PdfColorSpace Object
15.2 PdfFunction Object
15.3 Code Sample
15.4 Using Color Spaces with PdfTable and Other Objects

15.1 PdfColorSpace Object

15.1.1 Color Space Overview

The PDF format offers various methods for specifying the colors of graphics objects to be painted on the current page. Colors can be described in any of a variety of color systems, or color spaces. Some color spaces are related to device color representation (grayscale, RGB, CMYK), others to human visual perception (CIE-based.)

In PDF, a color space is an important component of the current graphics state along with other drawing parameters such as stroking and filling colors, line width, and others. Versions of AspPDF prior to 2.5 only supported RGB and CMYK color spaces via the PdfCanvas method SetColor/SetFillColor and SetColorCMYK/SetFillColorCMYK. These methods implicitly set the current color space to RGB and CMYK, respectively, and then specify the current color within this color space.

As of Version 2.5, AspPDF provides separate methods for specifying the current color space, and current color: SetColorSpace/SetFillColorSpace and SetColorEx/SetFillColorEx. This version also offers support for many more color spaces in addition to RGB and CMYK: Indexed, Lab, ICC-based, Separation, and others.

NOTE: A detailed discussion of PDF color spaces is outside the scope of this user manual. Please refer to the Adobe PDF Reference for detailed information on this vast subject.

15.1.2 PdfColorSpace Object Usage

To create an instance of the PdfColorSpace object, the PdfDocument method CreateColorSpace should be called. This method expects two arguments: the color space name, and an optional list of parameters. The valid color space names are: "DeviceRGB", "DeviceCMYK", "DeviceGray", "CalRGB", "CalGray", "Lab", "ICCBased", "Indexed", "Separation" and "DeviceN".

When the newly created PdfColorSpace object represents a basic color space such as "DeviceRGB", no additional steps are required and the object is ready to be used. However, if the color space is an advanced one, some additional properties and/or methods need to be called on the PdfColorSpace object for it to be valid and complete, as shown below. For debugging purposes, the PdfColorSpace object exposes the property IsValid which returns True if this object is valid and complete, and False otherwise. The exact verbal reason for the object not being valid should be retrieved via the property ValidationError.

Once a valid instance of the PdfColorSpace object is created, it can be used on the canvas of a page or graphics via the PdfCanvas methods SetColorSpace and SetFillColorSpace to set the new active color space for stroking and fill operations, respectively.

Once a color space is made active, the current color within this color space should be specified via the methods SetColorEx and SetFillColorEx for stroking and fill operations, respectively. These methods expect a single argument, a PdfParam object or parameter string through which all color components c1, c2, ..., cN are specified.

For example, the following sequence creates an RGB color space, activates it for stroking operations and sets the current stroking color to (0.5, 0.7, 1):

Set csRGB = Doc.CreateColorSpace("DeviceRGB")
Page.Canvas.SetColorSpace csRGB
Page.Canvas.SetColorEx "c1=0.5; c2=0.7; c3=1"

Note that the sequence above could be replaced by a single line of code:

Page.Canvas.SetColor 0.5, 0.7, 1

The following sequence creates an CMYK color space, activates it for fill operations and sets the current fill color to (0.3, 0.5, 0.8, 0.2):

Set csCMYK = Doc.CreateColorSpace("DeviceCMYK")
Page.Canvas.SetFillColorSpace csCMYK
Page.Canvas.SetFillColorEx "c1=0.3; c2=0.5; c3=0.8; c4=0.2"

The sequence above has the same effect as:

Page.Canvas.SetFillColorCMYK 0.3, 0.5, 0.8, 0.2

Note that it is the responsibility of the developer to ensure the correct number of color components are passed to the SetColorEx and SetFillColorEx methods in accordance with the currently active color space (e.g. 3 for RGB, 4 for CMYK, 1 for grayscale, etc.)

In addition to the use with the SetColorSpace and SetFillColorSpace methods of the PdfCanvas object, a color space object can also be assigned to an image via the method PdfImage.SetColorSpace.

15.1.3 Supported Color Spaces

This sub-section describes all color spaces currently supported by AspPDF, and the parameters and steps required to create a PdfColorSpace object representing each one.

DeviceRGB, DeviceCMYK, DeviceGray

These device color spaces enable a page description to specify color values that are directly related to their representation on an output device.

To create an RGB, CMYK or Grayscale device color space, the CreateColorSpace method must be called with the string "DeviceRGB", "DeviceCMYK", and "DeviceGray" as the first argument, respectively. The 2nd argument should be omitted as these color spaces require no additional parameters. No additional steps are necessary.

The number of color components for these color spaces are 3, 4 and 1, respectively.

Examples:

Set csRGB = Doc.CreateColorSpace("DeviceRGB")

Set csCMYK = Doc.CreateColorSpace("DeviceCMYK")

Set csGray = Doc.CreateColorSpace("DeviceGray")

CalGray

Calibrated color in PDF is defined in terms of an international standard used in the graphic arts, television and printing industries. CIE-based color spaces enable a page description to specify color values in a way that is related to human visual perception. The CalGray color space (as well as CalRGB, Lab and ICCBased described below) is a CIE-based color space. It has a single color component.

To create a CalGray color space, the CreateColorSpace method must be called with the string "CalGray" as the first argument. The 2nd argument must be set to a PdfParam object or parameter string specifying CalGray-specific parameters. These are:

Example:

Set Param = PDF.CreateParam
Param("Xw") = 0.95
Param("Yw") = 1
Param("Zw") = 1.089
Param("Gamma") = 2.222
Set csCalGray = doc.CreateColorSpace("CalGray", Param)

CalRGB

CalRGB is another CIE-based color space which has 3 color components (usually referred to as ABC.) To create a CalRGB color space, the CreateColorSpace method must be called with the string "CalRGB" as the first argument. The 2nd argument must be set to a PdfParam object or parameter string specifying CalRGB-specific parameters. These are:

Example:

Set Param = PDF.CreateParam
Param("Xw") = 0.95
Param("Yw") = 1
Param("Zw") = 1.089
Param("GammaR") = 1.8
Param("GammaG") = 1.8
Param("GammaB") = 1.8
Param.Add( "m1=0.449; m2=0.244; m3=0.025" )
Param.Add( "m4=0.316; m5=0.672; m6=0.141" )
Param.Add( "m7=0.184; m8=0.083; m9=0.923" )
Set csCalRGB = doc.CreateColorSpace("CalRGB", Param)

Lab

Lab is yet another CIE-based color space which has 3 color components (usually referred to as La*b*.) To create a Lab color space, the CreateColorSpace method must be called with the string "Lab" as the first argument. The 2nd argument must be set to a PdfParam object or parameter string specifying Lab-specific parameters. These are:

Example:

Set Param = PDF.CreateParam
Param("Xw") = 0.95
Param("Yw") = 1
Param("Zw") = 1.089
Param("amin") = -128
Param("amax") = 127
Param("bmin") = -128
Param("bmax") = 127
Set csLab = doc.CreateColorSpace("Lab", Param)

ICCBased

ICCBased color spaces are based on a cross-platform color profile as defined by the International Color Consortium (ICC). To create an ICCBased color space, the CreateColorSpace method must be called with the string "ICCBased" as the first argument. The 2nd argument must be set to a PdfParam object or parameter string specifying ICCBased-specific parameters. These are:

Additional steps must be taken to make an ICCBased color space object valid and complete. An actual color profile (usually a file with the extension .icc) must be assigned to the object via the method LoadDataFromFile. Many free color profiles can be downloaded from the Adobe web site.

Optionally, an alternative color space for this ICCBased color space can be specified via the SetAltColorSpace method.

Example:

Set csICC = doc.CreateColorSpace("ICCBased", "N=3")
csICC.LoadDataFromFile "c:\path\VideoPAL.icc"

Indexed

An Indexed color space enables you to specify a color with a single integer number. This number represents an index into a color table rather than an actual color value. Using an indexed color space can significantly reduce the amount of data required to represent an image. To create an Indexed color space, the CreateColorSpace method must be called with the string "Indexed" as the first argument. The 2nd argument must be set to a PdfParam object or parameter string specifying Indexed-specific parameters. These are:

Additional steps must be taken to make an Indexed color space object valid and complete. The base color space in which the values in the color table are to be interpreted needs to be specified, as well as the color table itself. Both are specified via the method SetIndexedParams. The first argument to this function is another instance of the PdfColorSpace object representing the base color space, and the 2nd argument a VARIANT-packed array of numbers. The size of the array should be (Hival + 1) times the number of color components in the base color space.

Example:

Dim numbers(767)
For i = 0 To 767
   numbers(i) = Rnd() * 255
' Fill color table with random numbers
Next

Set csRGB = doc.CreateColorSpace("DeviceRGB")

Set csIndexed = doc.CreateColorSpace("Indexed", "Hival=255")
csIndexed.SetIndexedParams csRGB, numbers

Separation

A Separation color space provides a means for specifying separate (spot) colorants used in offset printing. Under this color space, a color is specified by a single numeric value in the range [0.0, 1.0] called a tint. To create a Separation color space, the CreateColorSpace method must be called with the string "Separation" as the first argument. The 2nd argument is not used.

Additional steps must be taken to make a Separation color space object valid and complete. The name of the colorant that this color space is intended to represent needs to be specified. Also, an alternate color space and transformation function need to be specified to approximate the colorant on a device where the PDF document is viewed or printed in case the intended colorant is not available on that device.

These three additional parameters are specified via the method SetSeparationParams. The first argument is a string containing the name of the colorant. The second argument is another instance of the PdfColorSpace representing the alternate color space. The third argument is an instance of the PdfFunction object representing the transformation function. Functions are described in detail in the next section of this chapter.

In the example below, a Separation color space is created to represent the Pantone color 273. The alternate color space is DeviceRGB and transformation function performs a simple linear transformation from a tint value in the range [0.0, 1.0] to RGB values in the range [ (0, 0, 0), (56, 25, 122) ].

Example:

Set Func = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1;Rmin2=0;Rmax2=1;Rmin3=0;Rmax3=1; Size1=2; BitsPerSample=8")
Func.SetSampleData Array(0, 0, 0, 56, 25, 122)

Set csRGB = doc.CreateColorSpace("DeviceRGB")

Set csSeparation = doc.CreateColorSpace("Separation")
csSeparation.SetSeparationParams "PANTONE 273", csRGB, Func

DeviceN

DeviceN color spaces can contain an arbitrary number of color components. They provide greater flexibility than is possible with standard device color spaces such as DeviceCMYK or with individual Separation color spaces.

DeviceN color spaces are defined in a similar way to Separation color space - in fact, a Separation color space can be defined as a DeviceN color space with only one component.

To create a DeviceN color space, the CreateColorSpace method must be called with the string "DeviceN" as the first argument. The 2nd argument is not used.

Additional steps must be taken to make a DeviceN color space object valid and complete. A list of colorant names separated by the delimiter "##" needs to be specified. Also, an alternate color space and transformation function need to be specified to approximate the colorants on a device where the PDF document is viewed or printed in case the intended colorants are not available on that device. These three additional parameters are specified via the method SetSeparationParams. The transformation function passed to the SetSeparationParams method must have as many inputs as there are colorants in this color space.

Optionally, additional information can be specified for each individual colorant via the method AddColorant. This method accepts three arguments: the name of the colorant, alternate color space and transformation function. These parameters describe the appearance of this particular colorant alone, while the parameters passed to SetSeparationParams describe the appearance of all its colorants in combination. The transformation function passed to the AddColorant method must have a single input.

Example 1:

Set FuncN = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Dmin2=0;Dmax2=1; Rmin1=0;Rmax1=1; Rmin2=0;Rmax2=1; Rmin3=0;Rmax3=1; Size1=2; Size2=2; BitsPerSample=8")
FuncN.SetSampleData Array(0, 0, 0, 255, 45, 142, 56, 25, 122, 128, 128, 128)

Set csDeviceN = doc.CreateColorSpace("DeviceN")
csDeviceN.SetSeparationParams "PANTONE 806 U##PANTONE 273", csRGB, FuncN

In the example above, we define a 2-colorant DeviceN color space based on Pantone colors 806-U and 273. The transformation function, which has 2 inputs and 3 outputs, is defined to convert color (0, 0) to RGB (0, 0, 0), color (1, 0) to RGB (255, 45, 142), color (0, 1) to (56, 25, 122), and, arbitrarily, color (1, 1) to RGB (128, 128, 128). Linear approximation is used for all colors in between.

Example 2:

Set csRGB = doc.CreateColorSpace("DeviceRGB")

Set Func1 = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Rmin2=0;Rmax2=1; Rmin3=0;Rmax3=1; Size1=2; BitsPerSample=8")
Func1.SetSampleData Array(0, 0, 0, 255, 45, 142)

Set Func2 = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Rmin2=0;Rmax2=1; Rmin3=0;Rmax3=1; Size1=2; BitsPerSample=8")
Func2.SetSampleData Array(0, 0, 0, 56, 25, 122)

Set FuncN = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Dmin2=0;Dmax2=1; Rmin1=0;Rmax1=1; Rmin2=0;Rmax2=1; Rmin3=0;Rmax3=1; Size1=2; Size2=2; BitsPerSample=8")
FuncN.SetSampleData Array(0, 0, 0, 255, 45, 142, 56, 25, 122, 128, 128, 128)

Set csDeviceN = doc.CreateColorSpace("DeviceN")
csDeviceN.SetSeparationParams "PANTONE 806 U##PANTONE 273", csRGB, FuncN

csDeviceN.AddColorant "PANTONE 806 U", csRGB, Func1
csDeviceN.AddColorant "PANTONE 273", csRGB, Func2

Example 2 extends Example 1 to include an individual transformation function for each colorant.

15.2 PdfFunction Object
15.2.1 Function Overview

As shown above, some of the color spaces make use of functions, a special class of PDF objects representing numeric transformations from a set of input values to a set of output values.

A function to add two numbers has two input values and one output value:

f(x0, x1) = x0 + x1

Similarly, a function that computes the arithmetic and geometric means of two numbers could be viewed as a function of two input values and two output values:

f(x0, x1) = (x0 + x1) / 2, sqrt( x0 * x1 )

In general, a function can take any number (m) of input values and produce any number (n) of output values:

f(x0, x1, ..., xm-1) = y0, y1, ..., yn-1

Each function definition includes a domain, the set of legal values for the input. Some types of functions also include a range, the set of legal values for the output. Input values passed to the function are clipped to the domain, and output values produced by the function are clipped to the range.

In PDF, there are 4 types of functions:

  • A sampled function (type 0) uses a table of sample values to define the function. Values that fall between the sample values are interpolated. All color space examples in the previous section use sampled functions.
  • An exponential interpolation function (type 2) defines a set of coefficients for an exponential function.
  • A stitching function (type 3) is a combination of other functions, partitioned across a domain.
  • A PostScript calculator function (type 4) uses operators from the PostScript language to describe an arithmetic expression.

As of Version 2.5, AspPDF provides support for PDF functions via the PdfFunction object.

NOTE: A detailed discussion of PDF functions is outside the scope of this user manual. Please refer to the Adobe PDF Reference for detailed information on this subject.

15.2.2 PdfFunction Object Usage

In AspPDF, PDF functions are represented by the PdfFunction object introduced in version 2.5. To create an instance of PdfFunction, the PdfDocument method CreateFunction should be called. This method expects a single argument, a PdfParam object or parameter string specifying the function type and various other properties for the function being created. Some function types require that additional steps be taken to make the PdfFunction object valid and complete, as described below.

For debugging purposes, the PdfFunction object exposes the property IsValid which returns True if this object is valid and complete, and False otherwise. The exact verbal reason for the object not being valid should be retrieved via the property ValidationError.

The required parameters for the CreateFunction method are:

Once a valid PdfFunction object is created, it can be passed to any of the methods expecting a function as an argument, such as the PdfColorSpace method SetSeparationParams (see the previous section.)

15.2.3 Function Types

This sub-section describes all function types in detail, and the parameters and steps required to create a PdfFunction object representing each one.

Sampled Functions

Sampled (type 0) functions use a sequence of sample values to provide an approximation for functions whose domains and ranges are bounded. The samples are organized as an m-dimensional table in which each entry has n components.

To create an instance of the PdfFunction object representing a type 0 function, the CreateFunction method must be called with the parameter Type=0, and the required domain and range parameters for each input and output values, as described above. Additionally, a number of required and optional parameters specific to sampled functions are also used. These are:

To explain the relationship between the domain, encode, size, decode and range parameters, the following notation is used:

y = Interpolate( x, xmin, xmax, ymin, ymax ) = ymin + ( (x - xmin) * (ymax - ymin) / (xmax - xmin) )

For a given value of x, Interpolate calculates the y value on the line defined by the two points (xmin, ymin) and (xmax, ymax).

When a sampled function is called, each input value xi (where i is between 1 and m) is clipped to the domain:

x'i = min( max( xi, Dmini), Dmaxi)

That value is encoded:

ei = Interpolate( x'i, Dmini, Dmaxi, EncodeMini, EncodeMaxi )

That value is clipped to the size of the sample table in that dimension:

e'i = min( max( ei, 0 ), Sizei - 1 )

The encoded input values are real numbers, not restricted to integers. Interpolation is used to determine output values from the nearest surrounding values in the sample table. Each output value rj (where j is between 1 and n) is then decoded:

r'j = Interpolate( rj, 0, 2BitsPerSample-1, DecodeMinj, DecodeMaxj )

Finally, each decoded value is clipped to the range:

yj = min( max( r'j, Rminj), Rmaxj )

In addition to specifying the parameters described above, the method SetSampleData must be called on the PdfFunction object to make it valid and complete. This method expects a single argument, a VARIANT-packed array of numbers. The array length should be n * Size1 * Size2 * ... * Sizem.

For a function with multidimensional input (more than one input variable), the sample values in the first dimension vary fastest, and the values in the last dimension vary slowest. For example, for a function f(a, b, c), where a, b, and c vary from 0 to 9 in steps of 1, the sample values would appear in this order: f(0, 0, 0), f(1, 0, 0), ..., f(9, 0, 0), f(0, 1, 0), f(1, 1, 0), ..., f(9, 1, 0), f(0, 2, 0), f(1, 2, 0), ..., f(9, 9, 0), f(0, 0, 1), f(1, 0, 1), and so on.

For a function with multidimensional output (more than one output value), the values are stored in the same order as in the range parameters.

Example:

The graph above shows a simple 1-input, 1-output function with the domain [0, 1] and range [0, 1], approximated with 5 samples:

f(0.00) = 0.0
f(0.25) = 0.125
f(0.50) = 0.25
f(0.75) = 0.5
f(1.00) = 1.0

For this example, we have chosen the BitsPerSample value of 8. The sample data needs to be recalculated according to the normalized range [0, 2BitsPerSample - 1], or [0, 255], as follows:

(0.0, 0.125, 0.25, 0.5, 1.0) -> (0, 32, 64, 128, 255)

The following code sample implements this function and uses it for grayscale transformation in a Separation color space:

Set Func = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Size1=5; BitsPerSample=8")
Func.SetSampleData Array(0, 32, 64, 128, 255)

Set csGray = doc.CreateColorSpace("DeviceGray")
Set cs = doc.CreateColorSpace("Separation")
cs.SetSeparationParams "PANTONE 360 CV", csGray, Func

If we were to choose the BitsPerSample value of 16 instead, we would need to recalculate the sample values according to the range [0, 65535] and the code would be as follows:

Set Func = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Size1=5; BitsPerSample=16")
Func.SetSampleData Array(0, 8192, 16384, 32768, 65535)
...

Please refer to the previous section of this chapter for more sampled function examples.

Exponential Interpolation Functions

Exponential interpolation (type 2) functions define an exponential interpolation of one input and n output values.

To create an instance of the PdfFunction object representing a type 2 function, the CreateFunction method must be called with the parameter Type=2, and the required domain and optional range parameters for the one input and n output values, as described above. Additionally, a number of required and optional parameters specific to exponential interpolation functions are also used. These are:

Example:

Set Param = PDF.CreateParam
Param("Type") = 2
Param("Dmin1") = 0
Param("Dmax1") = 1
Param("CZero1") = 0.2
Param("COne1") = 0.9
Param("CZero2") = 0.2
Param("COne2") = 0.9
Param("CZero3") = 0.2
Param("COne3") = 0.9
Param("N") = 0.6

Set Func = doc.CreateFunction(Param)

Stitching Functions

Stitching (type 3) functions define a stitching of the subdomains of several 1-input functions (referred to as "subfunctions" in this manual) to produce a single new 1-input function. This manual uses the letter k to denote the total number of subfunctions in this stitching function.

To create an instance of the PdfFunction object representing a type 3 function, the CreateFunction method must be called with the parameter Type=3, and the required Domain and optional Range parameters for the one input and n output values, as described above. Additionally, a number of required parameters specific to stitching functions are also used. These are:

In addition to these parameters, the method AddFunction must be called on this PdfFunction object k times. This method expects a single argument, another instance of the PdfFunction object which represents a subfunction for this stitching function. The output dimensionality of all subfunctions must be the same and match the range parameters of this stitching function, if they are specified.

The Bounds parameters describes a series of half-opened intervals, closed on the left and open on the right (except the last which is closed on the right as well.)

The Encode parameters specify k pairs of numbers. A value x from the ith subdomain is encoded as follows:

x' = Interpolate( x, Boundsi-1, Boundsi, EncodeMini, EncodeMaxi) for i between 1 and k. In this equation, Bounds0 means Dmin1, and Boundsk means Dmax1.

Example:

The following example combines two subfunctions, an exponential-interpolation one and a sampled one, into a single stitching function. Both subfunctions have the domains [0, 1]. The first subfunction is mapped to the subdomain [0, 0.5] of the main function, and the 2nd subfunction to the subdomain [0.5, 1].

Exponential interpolation subfunction:

Sampled subfunction:

Resultant stitching function:

Set Func1 = doc.CreateFunction("type=2; Dmin1=0; Dmax1=1; Rmin1=0; Rmax1=1; N=2; CZero1=0; COne1=0.5")

Set Func2 = doc.CreateFunction("type=0; Dmin1=0; Dmax1=1; Rmin1=0; Rmax1=1; Size1=5;BitsPerSample=8")
Func2.SetSampleData Array(125, 191, 223, 231, 255)

Set Func3 = doc.CreateFunction("type=3; Dmin1=0; Dmax1=1; bounds1=0.5; EncodeMin1=0; EncodeMax1=1; EncodeMin2=0; EncodeMax2=1")
Func3.AddFunction Func1
Func3.AddFunction Func2

PostScript Calculator Functions

A PostScript Calculator (type 4) function is defined as a text string of code written in a small subset of the PostScript language. The detailed description of the commands that can be used in a type 4 function is outside the scope of this manual. Please refer to the Adobe PDF Reference for more information.

To create an instance of the PdfFunction object representing a type 4 function, the CreateFunction method must be called with the parameter Type=4, and the required domain and range parameters for each input and output values, as described above. There are no additional parameters.

An additional step must be taken to make a PdfFunction object representing this type of function valid and complete. A text string containing the actual PostScript code must be specified via the property PostScript.

Example:

Set Func = doc.CreateFunction("Type=4;Dmin1=-1; Dmax1=1; Dmin2=-1; Dmax2=1; Rmin1=-1; Rmax1=1")
Func.PostScript = "{ 360 mul sin 2 div exch 360 mul sin 2 div add }"

15.3 Code Sample
This chapter would not be complete without a few PDF-generating VBScript/C# lines of code. The following code sample displays an image in its default form as well as under an ICCBased color space. It also draws a series of bars under the default grayscale color space, a Separation color space with a sampled transformation function, and a calibrated grayscale color space.

VBScript
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument
Set Page = Doc.Pages.Add

' Create some color spaces
Set csICC = Doc.CreateColorSpace( "ICCBased", "N=3" )
csICC.LoadDataFromFile Server.MapPath( "AdobeRGB1998.icc" )

' Draw an image with the default color space, and the same image with an ICC profile
Set Image = Doc.OpenImage( Server.MapPath( "apple.jpg" ) )
Page.Canvas.DrawImage Image, "x=20; y=600; scalex=0.5, scaley=0.5"
Page.Canvas.DrawText "Without a profile", "x=20; y=595", Doc.Fonts("Helvetica")

Set Image2 = Doc.OpenImage( Server.MapPath( "apple.jpg" ) )
Image2.SetColorSpace csICC
Page.Canvas.DrawImage Image2, "x=240; y=600; scalex=0.5, scaley=0.5"
Page.Canvas.DrawText "With an ICC profile", "x=240; y=595", Doc.Fonts("Helvetica")

' Grayscale color space
Set csGray = doc.CreateColorSpace("DeviceGray")
DrawBars 300, csGray, "Default Grayscale"

' Separation color space with a sampled transformation function
Set Func = doc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Size1=5; BitsPerSample=8")
Func.SetSampleData Array(0, 32, 64, 128, 255)
Set csSep = doc.CreateColorSpace("Separation")
csSep.SetSeparationParams "PANTONE 4525 C", csGray, Func
DrawBars 180, csSep, "Grayscale based on a sampled function"

' Calibrated Gray
Set csCalGray = doc.CreateColorSpace("CalGray", "Xw=0.7; Yw=1; Zw=1.3")
DrawBars 60, csCalGray, "Calibrated Grayscale"

Filename = Doc.Save( Server.MapPath( "colorspace.pdf" ), False )

Response.Write "Success! Download your PDF file <A HREF=" & Filename & ">here</A>"

Sub DrawBars( YCoord, ColorSpace, Title )
   
' stroking colors: for bar borders
   Page.Canvas.SetColorEx "c1=0"
   Page.Canvas.LineWidth = 5

   Page.Canvas.SetFillColorSpace ColorSpace

   For c = 0 to 1 step 0.1
      Page.Canvas.SetFillColorEx "c1=" & c

      Page.Canvas.DrawRect 20 + c * 500, YCoord, 50, 100
      Page.Canvas.FillRect 20 + c * 500, YCoord, 50, 100
   Next

   Page.Canvas.DrawText Title, "x=20; y=" & YCoord + 10, Doc.Fonts("Helvetica")
End Sub

C#
IPdfPage objPage;
IPdfDocument objDoc;

void Page_Load(Object Source, EventArgs E)
{
   IPdfManager objPdf = new PdfManager();
   objDoc = objPdf.CreateDocument( Missing.Value );
   objPage = objDoc.Pages.Add( Missing.Value, Missing.Value, Missing.Value );

   // Create some color spaces
   IPdfColorSpace csICC = objDoc.CreateColorSpace( "ICCBased", "N=3" );
   csICC.LoadDataFromFile( Server.MapPath( "AdobeRGB1998.icc" ) );

   // Draw an image with the default color space, and the same image with an ICC profile
   IPdfImage objImage = objDoc.OpenImage( Server.MapPath( "apple.jpg" ), Missing.Value );
   objPage.Canvas.DrawImage( objImage, "x=20; y=600; scalex=0.5, scaley=0.5" );
   objPage.Canvas.DrawText( "Without a profile", "x=20; y=595", objDoc.Fonts["Helvetica", Missing.Value] );

   IPdfImage objImage2 = objDoc.OpenImage( Server.MapPath( "apple.jpg" ), Missing.Value );
   objImage2.SetColorSpace( csICC );
   objPage.Canvas.DrawImage( objImage2, "x=240; y=600; scalex=0.5, scaley=0.5" );
   objPage.Canvas.DrawText( "With an ICC profile", "x=240; y=595", objDoc.Fonts["Helvetica", Missing.Value] );

   // Grayscale color space
   IPdfColorSpace csGray = objDoc.CreateColorSpace("DeviceGray", Missing.Value);
   DrawBars( 300, csGray, "Default Grayscale" );

   // Separation color space with a sampled transformation function
   IPdfFunction objFunc = objDoc.CreateFunction("type=0; Dmin1=0;Dmax1=1; Rmin1=0;Rmax1=1; Size1=5; BitsPerSample=8");
   object [] arrSamples = new object[5] { (object)0, (object)32, (object)64, (object)128, (object)255 };
   objFunc.SetSampleData( arrSamples );
   IPdfColorSpace csSep = objDoc.CreateColorSpace("Separation", Missing.Value);
   csSep.SetSeparationParams( "PANTONE 4525 C", csGray, objFunc );

   DrawBars( 180, csSep, "Grayscale based on a sampled function" );

   // Calibrated Gray
   IPdfColorSpace csCalGray = objDoc.CreateColorSpace("CalGray", "Xw=0.7; Yw=1; Zw=1.3");
   DrawBars( 60, csCalGray, "Calibrated Grayscale" );

   String strFilename = objDoc.Save( Server.MapPath("colorspace.pdf"), false );

   lblResult.Text = "Success! Download your PDF file <A HREF=" + strFilename + ">here</A>";
}

void DrawBars( int YCoord, IPdfColorSpace ColorSpace, string Title )
{
   
// stroking colors: for bar borders
   objPage.Canvas.SetColorEx( "c1=0" );
   objPage.Canvas.LineWidth = 5;

   objPage.Canvas.SetFillColorSpace( ColorSpace );

   for( double c = 0; c <= 1; c += 0.1 )
   {
      objPage.Canvas.SetFillColorEx( "c1=" + c.ToString() );

      objPage.Canvas.DrawRect( 20 + (float)c * 500, YCoord, 50, 100 );
      objPage.Canvas.FillRect( 20 + (float)c * 500, YCoord, 50, 100 );
   }

   objPage.Canvas.DrawText( Title, "x=20; y=" + (YCoord + 10).ToString(), objDoc.Fonts["Helvetica", Missing.Value] );
}

Click the links below to run this code sample:

http://localhost/asppdf/manual_15/15_colorspace.asp
http://localhost/asppdf/manual_15/15_colorspace.aspx  Why is this link not working?

15.4 Using Color Spaces with PdfTable and Other Objects
As of Version 2.7, the PdfTable object has been retrofitted to enable the border and background colors of a table to be specified in color spaces other than RGB (previous versions only allowed the table colors to be in the RGB color space.) To allow color spaces to be specified via PdfParam objects or parameter strings, the color space index, a unique integer identifier, is passed instead of an instance of the PdfColorSpace object.

Two new methods have been added to enable arbitrary colors to be specified: PdfTable.SetColor and PdfCell.SetColor. The former can be used to specify the table border, table background, cell border and cell background colors for the entire table. The latter specifies the border and background colors for an individual cell.

Both SetColor methods expect a single argument: a PdfParam object or parameter string containing the following parameters:

  • Type - specifies which color is to be modified. The valid values are: 1 (border color), 2 (cell border color), 3 (background color) and 4 (cell background color). The PdfTable.SetColor method allows all four values, while PdfCell.SetColor only allows values 2 and 4.
  • ColorSpace - specifies the color space index. This value should be obtained from the Index property of the PdfColorSpace object.
  • c1, c2, ..., cN - specify the individual color components for the color. These values must be in the range [0, 1] and the number of color components must match the color space being used.
  • Top, Bottom, Left, Right - if set to True, specify which side(s) of the cell the specified border color should apply to. These parameters are only used by PdfCell.SetColor and only if Type=2. If none of these are set to True, the color applies to the entire cell border.

Starting with Version 2.7, the ColorSpace and c1, c2, ..., cN parameters can also be used with the PdfCanvas.DrawText and PdfCell.AddText methods to specify text colors in color spaces other than RGB and CMYK.

The following code sample creates an ICC-based color space and uses it to paint an entire table, an individual cell, and a text string inside a cell:

VBScript
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument

Set Page = Doc.Pages.Add

' Create an ICC color space
Set csICC = Doc.CreateColorSpace( "ICCBased", "N=3" )
csICC.LoadDataFromFile Server.MapPath( "AdobeRGB1998.icc" )

Set Table = doc.CreateTable("rows=2; cols=3; width=400; height=300; border=10; cellborder=5; cellspacing=10; cellpadding=2")
Table.Font = Doc.Fonts("Helvetica")

' Border color
Table.SetColor "Type=1; ColorSpace=" & csICC.Index & "; c1=1; c2=0.5; c3=0.2"

' Cell Border Color
Table.SetColor "Type=2; ColorSpace=" & csICC.Index & "; c1=0; c2=0; c3=1"

' BG Color
Table.SetColor "Type=3; ColorSpace=" & csICC.Index & "; c1=1; c2=1; c3=0"

' Cell BG Color
Table.SetColor "Type=4; ColorSpace=" & csICC.Index & "; c1=0; c2=1; c3=0"

' Border color for individual cell
Table(1, 1).SetColor "Type=2; ColorSpace=" & csICC.Index & "; c1=1; c2=0; c3=1"

' Draw text, use the same color space for it
Table(1, 1).AddText "Test", "ColorSpace=" & csICC.Index & ";c1=0.2; c2=0.4; c3=0.3"

' Render table
Page.Canvas.DrawTable Table, "x=106; y=700"

Filename = Doc.Save( Server.MapPath( "cs_table.pdf" ), False )

C#
IPdfManager objPdf = new PdfManager();
objDoc = objPdf.CreateDocument( Missing.Value );

objPage = objDoc.Pages.Add( Missing.Value, Missing.Value, Missing.Value );

// Create an ICC color space
IPdfColorSpace csICC = objDoc.CreateColorSpace( "ICCBased", "N=3" );
csICC.LoadDataFromFile( Server.MapPath( "AdobeRGB1998.icc" ) );

IPdfTable objTable = objDoc.CreateTable("rows=2; cols=3; width=400; height=300; border=10; cellborder=5; cellspacing=10; cellpadding=2");
objTable.Font = objDoc.Fonts["Helvetica", Missing.Value];

// Border color
objTable.SetColor("Type=1; ColorSpace=" + csICC.Index + "; c1=1; c2=0.5; c3=0.2");

// Cell Border Color
objTable.SetColor("Type=2; ColorSpace=" + csICC.Index + "; c1=0; c2=0; c3=1");

// BG Color
objTable.SetColor("Type=3; ColorSpace=" + csICC.Index + "; c1=1; c2=1; c3=0");

// Cell BG Color
objTable.SetColor("Type=4; ColorSpace=" + csICC.Index + "; c1=0; c2=1; c3=0");

// Border color for individual cell
objTable[1, 1].SetColor("Type=2; ColorSpace=" + csICC.Index + "; c1=1; c2=0; c3=1");

// Draw text, use the same color space for it
objTable[1, 1].AddText("Test", "ColorSpace=" + csICC.Index + ";c1=0.2; c2=0.4; c3=0.3", Missing.Value);

// Render table
objPage.Canvas.DrawTable( objTable, "x=106; y=700" );

String strFilename = objDoc.Save( Server.MapPath("cs_table.pdf"), false );

Click the links below to run this code sample:

http://localhost/asppdf/manual_15/15_table.asp
http://localhost/asppdf/manual_15/15_table.aspx  Why is this link not working?

Chapter 16: IE-based HTML-to-PDF Conversion Chapter 14: PDF to Image Conversion
Search AspPDF.com

  This site is owned and maintained by Persits Software, Inc. Copyright © 2003 - 2013. All Rights Reserved.