Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Create PDF with DeviceN Colorspace
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Create PDF with DeviceN Colorspace
This example is the version from Tue, 10th Jan 2022.
Project "Create PDF with DeviceN Colorspace.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu2 = ""
End MenuBar
Class App Inherits Application
EventHandler Sub Open()
test1
test2
test3
test4
End EventHandler
Shared Sub AddOperator(byref dest as string, value as double, index as integer)
// The following function creates the PostScript code to multiply the tint
// values with the components of the CMYK color representation. 0 and 1
// color values are optimized to more efficient code.
if value = 0.0 then
dest = dest + "0 "
elseif value = 1.0 then
dest = dest + str(index)+" index "
else
dest = dest + str(index)+" index "+str(value)+" mul "
end if
End Sub
Shared Sub CreateBlendFunction(byref dest as string, colorants() as double)
// Note that this function does not allocate memory. The string must be
// long enough to hold the resulting PostScript code. The function can
// be used to create the blend function for DeviceN color spaces with up
// to 32 color channels. The alternate color space must be DeviceCMYK or
// an ICCBased color space whose base color space is in turn DeviceCMYK.
// Since the alternate color space must be DeviceCMYK, NumColorants is
// always a multiple of 4!
dim numColorants as integer = UBound(colorants)+1
dim numColors as integer = numColorants/4
dim index as integer = numColors - 1
dest = "{"
// The PostScript code must be enclosed in braces
for i as integer = 0 to 3
for j as integer = 0 to NumColorants-1 step 4
// Create code to multiply the tint values with the color values
AddOperator(Dest, Colorants(i+j), index)
next
// Add the resulting components. No range check is required...
for j as integer = 0 to numColors - 2
dest = dest + "add "
next
index = index + 1
next
// We are nearly finish. Place the tint values on top of the stack
dest = dest + str(index+1)+" 4 roll"
// Remove the tint values from the stack
for j as integer = 0 to numColors-1
dest = dest + " pop"
next
dest = dest + "}"
// Finish the code with a brace
End Sub
Shared Sub TestDeviceNColorSpace2(pdf as MyDynaPDFMBS)
dim cs as integer
dim psFunc as string // Buffer that holds the PostScript code
dim cls() as string = array("PANTONE 345 CVC", "PANTONE 293 CVC", "Yellow")
dim cls2() as string = array("PANTONE 345 CVC", "PANTONE 293 CVC")
dim pcs() as string = array("Cyan", "Magenta", "Yellow", "Black")
dim colors() as double = Array(_ // The CMYK color values are taken from Adobe's Photoshop
0.38, 0.00, 0.34, 0.0,_ // Definition of the first spot color
1.00, 0.56, 0.00, 0.0,_ // Definition of the second spot color
0.00, 0.00, 1.00, 0.0_ // Definition of the process color
)
call pdf.Append
// We need to create the PostScript calculator function first because
// it is a required parameter of pdfCreateDeviceNColorSpace().
CreateBlendFunction(psFunc, colors)
'MsgBox psFunc
// Create the DeviceN color space
cs = pdf.CreateDeviceNColorSpace(cls, psFunc, pdf.kesDeviceCMYK, -1)
// cls: The colorants array
// psfunc: Our PostScript tint transformation function
// kesDeviceCMYK, // Alternate color space
// -1: No handle is required for a device color space
if (cs < 0) then return // error
// We create also Separation color spaces for the spot colors and add
// these color spaces as an attribute to the DeviceN color space:
dim separations(-1) as integer
// First spot color
separations.Append pdf.CreateSeparationCS(cls(0), pdf.kesDeviceCMYK, -1, PDF.CMYK(97, 0, 87, 0)) // 0.38 * 255, 0 * 255, 0.34 * 255, 0 * 255
// Second spot color
separations.Append pdf.CreateSeparationCS(cls(1), pdf.kesDeviceCMYK, -1, PDF.CMYK(255, 143, 0, 0)) // 1.0 * 255, 0.56 * 255, 0 * 0, 0 * 0
call pdf.AddDeviceNSeparations(cs, cls2, separations) // there we pass only 2 colors!
// cs: DeviceN color space handle
// cls: Colorants array ,
// separations: Separation color space handles for these colorants
// Because the DeviceN color space uses a process color we add also
// the attributes of the process color space to it. Note that all
// colorant names must be defined, also if the DeviceN color space
// uses only one component of the process color space.
call pdf.AddDeviceNProcessColorants(cs, pcs, pdf.kesDeviceCMYK, -1)
// cs: DeviceN color space handle
// cls: Array of process colorants
// kesDeviceCMYK: The used process color space
// -1: No handle is required for a device color space
// The DeviceN color space is now fully created so that we can use it
call pdf.SetExtColorSpace(cs)
// Set the DeviceN color space
call pdf.SetFillColorEx 150, 100, 100 // Set a fill color
// Draw a rectangle with this color
call pdf.Rectangle(50.0, 690.0, 200.0, 100.0, pdf.kfmFill)
call pdf.EndPage
call pdf.CloseFile
End Sub
Shared Sub TestDeviceNColorSpace3(pdf As MyDynaPDFMBS)
//
dim cs As Integer
dim psFunc As String // Buffer that holds the PostScript code
dim cls() As String = array( "SpotBlue", "SpotMagenta", "SpotOrange", "Black" )
dim cls_Separations() As String = array( "SpotBlue", "SpotMagenta", "SpotOrange" )
dim pcs() As String = array( "Cyan", "Magenta", "Yellow", "Black" )
//
dim colors() As double = Array( _
1.00, 0.85, 0.00, 0.00, _ // Definition of the first spot color
0.00, 1.00, 0.00, 0.00, _ // Definition of the second spot color
0.00, 0.50, 1.00, 0.00, _ // Definition of the third spot color
0.00, 0.00, 0.00, 1.00 _ // Definition of the process color
)
//
Call pdf.Append
// We need to create the PostScript calculator function first because
// it is a required parameter of pdfCreateDeviceNColorSpace().
CreateBlendFunction( psFunc, colors )
// Create the DeviceN color space
cs = pdf.CreateDeviceNColorSpace( cls, psFunc, pdf.kesDeviceCMYK, -1 )
// cls: The colorants array
// psfunc: Our PostScript tint transformation function
// kesDeviceCMYK, // Alternate color space
// -1: No handle is required for a device color space
if ( cs < 0 ) then Return // error
// We create also Separation color spaces for the spot colors and add
// these color spaces As an attribute to the DeviceN color space:
dim separations( -1 ) As Integer
// First spot color
separations.Append pdf.CreateSeparationCS( cls( 0 ), pdf.kesDeviceCMYK, -1, PDF.CMYK( 255, 216, 0, 0 ) )
// Second spot color
separations.Append pdf.CreateSeparationCS( cls( 1 ), pdf.kesDeviceCMYK, -1, PDF.CMYK( 0, 255, 0, 0 ) )
// Third spot color
separations.Append pdf.CreateSeparationCS( cls( 2 ), pdf.kesDeviceCMYK, -1, PDF.CMYK( 0, 128, 255, 0 ) )
// Add the separation color spaces to the DeviceN color space.
Call pdf.AddDeviceNSeparations( cs, cls_Separations, separations )
// cs: DeviceN color space handle
// cls: Colorants array ,
// separations: Separation color space handles for these colorants
// Because the DeviceN color space uses a process color we add also
// the attributes of the process color space to it. Note that all
// colorant names must be defined, also if the DeviceN color space
// uses only one component of the process color space.
Call pdf.AddDeviceNProcessColorants( cs, pcs, pdf.kesDeviceCMYK, -1 )
// cs: DeviceN color space handle
// cls: Array of process colorants
// kesDeviceCMYK: The used process color space
// -1: No handle is required for a device color space
// The DeviceN color space is now fully created so that we can use it
Call pdf.SetExtColorSpace( cs )
// Set the DeviceN color, Spot1
Call pdf.SetFillColorEx( 255, 0, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 100.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, Spot2
Call pdf.SetFillColorEx( 0, 255, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 200.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, Spot3
Call pdf.SetFillColorEx( 0, 0, 255, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 300.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, Black
Call pdf.SetFillColorEx( 0, 0, 128, 255 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 400.0, 100.0, 100.0, 100.0, pdf.kfmFill )
//
Call pdf.SetFillColorEx( 100, 100, 100, 100 ) // Set a fill color
Call pdf.SetFont( "Arial", pdf.kfsNormal, 8.0, True, pdf.kcpUnicode )
Call pdf.SetTextRect( 400, 100, 100, 100 )
Call pdf.WriteFText( pdf.ktaCenter, "Here is 100 % black and 50 % SpotOrange" )
//
Call pdf.EndPage
Call pdf.CloseFile
End Sub
Shared Sub TestDeviceNColorSpace4(pdf As MyDynaPDFMBS)
//
dim cs As Integer
dim psFunc As String // Buffer that holds the PostScript code
dim cls() As String = array( "Cyan", "Magenta", "Yellow", "Black", "Orange", "Green", "Violet" )
dim cls_Separations() As String = array( "Orange", "Green", "Violet" )
dim pcs() As String = array( "Cyan", "Magenta", "Yellow", "Black" )
// this time DeviceN with 7 colors. Cyan, Magenta, Yellow, Black, Orange, Green, Violet
//
dim colors() As double = Array( _
1.00, 0.00, 0.00, 0.00, _ // Definition of the first spot color
0.00, 1.00, 0.00, 0.00, _ // Definition of the second spot color
0.00, 0.00, 1.00, 0.00, _ // Definition of the third spot color
0.00, 0.00, 0.00, 1.00, _ // black
0.00, 0.62, 1.00, 0.00, _ // orange
0.59, 0.00, 1.00, 0.00, _ // green
0.63, 0.81, 0.00, 0.00 _ // violet
)
//
Call pdf.Append
// We need to create the PostScript calculator function first because
// it is a required parameter of pdfCreateDeviceNColorSpace().
CreateBlendFunction( psFunc, colors )
// Create the DeviceN color space
cs = pdf.CreateDeviceNColorSpace( cls, psFunc, pdf.kesDeviceCMYK, -1 )
// cls: The colorants array
// psfunc: Our PostScript tint transformation function
// kesDeviceCMYK, // Alternate color space
// -1: No handle is required for a device color space
if ( cs < 0 ) then Return // error
// We create also Separation color spaces for the spot colors and add
// these color spaces As an attribute to the DeviceN color space:
dim separations( -1 ) As Integer
// First spot color
separations.Append pdf.CreateSeparationCS( "Orange", pdf.kesDeviceCMYK, -1, PDF.CMYK( 0, 158, 255, 0 ) )
// Second spot color
separations.Append pdf.CreateSeparationCS( "Green", pdf.kesDeviceCMYK, -1, PDF.CMYK( 158, 0, 255, 0 ) )
// Third spot color
separations.Append pdf.CreateSeparationCS( "Violet", pdf.kesDeviceCMYK, -1, PDF.CMYK( 160, 206, 0, 0 ) )
// Add the separation color spaces to the DeviceN color space.
Call pdf.AddDeviceNSeparations( cs, cls_Separations, separations )
// cs: DeviceN color space handle
// cls: Colorants array ,
// separations: Separation color space handles for these colorants
// Because the DeviceN color space uses a process color we add also
// the attributes of the process color space to it. Note that all
// colorant names must be defined, also if the DeviceN color space
// uses only one component of the process color space.
Call pdf.AddDeviceNProcessColorants( cs, pcs, pdf.kesDeviceCMYK, -1 )
// cs: DeviceN color space handle
// cls: Array of process colorants
// kesDeviceCMYK: The used process color space
// -1: No handle is required for a device color space
// The DeviceN color space is now fully created so that we can use it
Call pdf.SetExtColorSpace( cs )
// Set the DeviceN color, c
Call pdf.SetFillColorEx( 255, 0, 0, 0, 0, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 100.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, m
Call pdf.SetFillColorEx( 0, 255, 0, 0, 0, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 200.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, y
Call pdf.SetFillColorEx( 0, 0, 255, 0, 0, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 300.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, k
Call pdf.SetFillColorEx( 0, 0, 0, 255, 0, 0, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 400.0, 100.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, orange
Call pdf.SetFillColorEx( 0, 0, 0, 0, 255, 0, 0) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 100.0, 200.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, green
Call pdf.SetFillColorEx( 0, 0, 0, 0, 0, 255, 0 ) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 200.0, 200.0, 100.0, 100.0, pdf.kfmFill )
// Set the DeviceN color, violet
Call pdf.SetFillColorEx(0, 0, 0, 0, 0, 0, 255) // Set a fill color
// Draw a rectangle with this color
Call pdf.Rectangle( 300.0, 200.0, 100.0, 100.0, pdf.kfmFill )
//
Call pdf.EndPage
Call pdf.CloseFile
End Sub
Sub test1()
// In this example we create a DeviceN color space that contains the process colorants Cyan, Magenta, and Yellow.
// The alternate color space is of course DeviceCMYK. The definition of the PostScript calculator function is
// very simple in this example because we need to add the missing Black component only. The array cls includes
// also the Black component here because we need the colorant names later to add the definition of the process
// components to the DeviceN color space:
Dim pdf As New MyDynapdfMBS
Dim d As New date
Dim f As FolderItem = SpecialFolder.Desktop.Child("Create PDF with DeviceN Colorspace 1.pdf")
pdf.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
Call pdf.CreateNewPDF f
Call pdf.SetDocInfo pdf.kdiSubject, "My first Xojo output"
Call pdf.SetDocInfo pdf.kdiProducer, "Xojo test application"
Call pdf.SetDocInfo pdf.kdiTitle, "My first Xojo output"
// We want to use top-down coordinates
Call pdf.SetPageCoords pdf.kpcTopDown
Call pdf.Append
Dim cls() As String = Array("Cyan", "Magenta", "Yellow") // without "Black"
Dim ps As String = "{0}" // Just add the missing Black component
// Note that the Black component is not part of the DeviceN color space
Dim cs As Integer = pdf.CreateDeviceNColorSpace(cls, ps, pdf.kesDeviceCMYK, -1)
// Optional but strongly recommended: Add the defintion of the process
// colorants to the DeviceN color space.
cls = Array("Cyan", "Magenta", "Yellow", "Black")
Call pdf.AddDeviceNProcessColorants(cs, cls, pdf.kesDeviceCMYK, -1)
// Draw a rectangle in the alternate DeviceCMYK color space
Call pdf.SetFillColorSpace(pdf.kcsDeviceCMYK)
Call pdf.SetFillColor(pdf.CMYK(135, 65, 160, 0))
Call pdf.Rectangle(50.0, 50.0, 200.0, 100.0, pdf.kfmFill)
// Now we use the DeviceN color space. The colors of both rectangles must
// be identically. If you see a difference, then disable the output
// preview in Adobe's Acrobat, since the color is converted into the
// simulation profile otherwise...
Call pdf.SetExtColorSpace(cs)
Call pdf.SetFillColorEx 135, 65, 160
Call pdf.Rectangle(50.0, 150.0, 200.0, 100.0, pdf.kfmFill)
Call pdf.EndPage
Call pdf.CloseFile
f.Launch
End Sub
Sub test2()
Dim pdf As New MyDynapdfMBS
Dim f As FolderItem = SpecialFolder.Desktop.Child("Create PDF with DeviceN Colorspace 2.pdf")
Call pdf.CreateNewPDF f
// We simply try this test function here
Call TestDeviceNColorSpace2(pdf)
f.launch
End Sub
Sub test3()
//
Dim pdf As New MyDynapdfMBS
Dim f As FolderItem = SpecialFolder.Desktop.Child( "Create PDF with DeviceN Colorspace 3.pdf" )
//
Call pdf.CreateNewPDF( f )
// We simply try this test function here
Call TestDeviceNColorSpace3( pdf )
//
f.Launch
End Sub
Sub test4()
//
Dim pdf As New MyDynapdfMBS
Dim f As FolderItem = SpecialFolder.Desktop.Child( "Create PDF with DeviceN Colorspace 4.pdf" )
//
Call pdf.CreateNewPDF( f )
// We simply try this test function here
Call TestDeviceNColorSpace4( pdf )
//
f.Launch
End Sub
Note "About"
In this example we want to define a DeviceN color space that contains 2 spot
colors and the process color yellow. The alternate color space for the spot
colors is DeviceCMYK in this example and the process colorant is defined in this
color space too. The tint transformation function for such a color space must be
able to compute one color from arbitrary combinations of these three colors. So,
we need essentially a blend function expressed in PostScript syntax.
The definition of a blend function is not difficult but long because the entire
code is stack based. However, let us first keep in mind what we need to do. We
have already three values on the stack when the function is executed. These are
the tint values which were passed to the DeviceN color space. We need to
multiply the tint values with the four components of the alternate color
representations of all three channels and finally we need to combine these three
colors so that we get the resulting output color. This sounds difficult, because
a lot of multiplications and additions are required to produce the wished
result, but you'll see it's easier as it looks like. The used colors in the
DeviceN color space are defined as follows:
Colorant name CMYK representation
PANTONE 345 CVC (coated) {0.38, 0.00, 0.34, 0.0} or { 97, 0, 87, 0}
PANTONE 293 CVC (coated) {1.00, 0.56, 0.00, 0.0} or {255, 143, 0, 0}
Yellow {0.00, 0.00, 1.00, 0.0} or { 0, 0, 255, 0}
The CMYK color values of the spot colors are taken from the color picker of
Adobe's Photoshop. Now, we have all information we need to create the PostScript
calculator function. The multiplication of the color channels with the tint
values must be done for all three colors included in the color space. Because
these are repeating operations we can easily develop a function that creates the
necessary code for us:
End Class
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
// and display dialog:
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Continue"
d.CancelButton.Visible=True //show the Cancel button
// a warning or an error?
if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then
// if user decided to ignore, we'll ignore
if IgnoreWarnings then Return 0
d.Message="A warning occurred while processing your PDF code."
// we add a third button to display all warnings
d.AlternateActionButton.Caption = "Ignore warnings"
d.AlternateActionButton.Visible = true
else
d.Message="An error occurred while processing your PDF code."
end if
d.Explanation = str(ErrorCode)+": "+ErrorMessage
b=d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
Return 0 // ignore
Case d.AlternateActionButton
IgnoreWarnings = true
Return 0 // ignore
Case d.CancelButton
Return -1 // stop
End select
End EventHandler
Property IgnoreWarnings As Boolean
End Class
End Project
See also:
- /DynaPDF/Create PDF and Email
- /DynaPDF/Create PDF with Angle Text
- /DynaPDF/Create PDF with DPart Metadata
- /DynaPDF/Create PDF with Glyphs
- /DynaPDF/Create PDF with Image FileLink
- /DynaPDF/Create PDF with japanese font
- /DynaPDF/Create PDF with named destination
- /DynaPDF/Create PDF with paths
- /DynaPDF/Create PDF with Picture
- /DynaPDF/Create PDF with text block
The items on this page are in the following plugins: MBS DynaPDF Plugin.