3

As I understand it, PDF files cannot have gradients from one ppot colour to another spot colour. Using iTextSharp, if I try this, it results in an exception when the spot colours on the gradient stops do not match.

However, Adobe Illustator CS6 is able to create a PDF file which includes a linear gradient from one spot colour to a different spot colour.

Looking at the resulting PDF file, I see the following:

13 0 obj
<</Colorants 15 0 R/Subtype/NChannel>>
endobj
15 0 obj
<</Spot#20Blue 16 0 R/Spot#20Red 17 0 R>>
endobj
16 0 obj
[/Separation/Spot#20Blue/DeviceRGB<</C0[1.0 1.0 1.0]/C1[0.0 0.0 1.0]/Domain[0 1]/FunctionType 2/N 1.0/Range[0.0 1.0 0.0 1.0 0.0 1.0]>>]
endobj
17 0 obj
[/Separation/Spot#20Red/DeviceCMYK<</C0[0.0 0.0 0.0 0.0]/C1[0.0 0.993988 1.0 0.0]/Domain[0 1]/FunctionType 2/N 1.0/Range[0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0]>>]
endobj
9 0 obj
<</Color[20224 32768 65535]/Dimmed false/Editable true/Preview true/Printed true/Title(Layer 1)/Visible true>>
endobj 

Based on the above, it looks like a multi-spot-colour gradient can be accomplished using a "Colorants" dictionary.

Does anyone know how to do this using iTextSharp, or iText (java) ? I cannot seem to find references to Colorants in the iTextSharp source code.

mwhouser
  • 31
  • 1

1 Answers1

0

This appears to be possible if you manually define type 2 axial PdfShading instead of using PdfShading.SimpleAxial:

var dest = @"C:\publish\shaded_fill.pdf";

var document = new Document();
var writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));
document.Open();

var spotBlue = new PdfSpotColor("Spot Blue", new BaseColor(0.0f, 0.0f, 1.0f));
var spotRed = new PdfSpotColor("Spot Red", new CMYKColor(0.0f, 0.993988f, 1.0f, 0.0f));
var devn = new PdfDeviceNColor(new[] { spotBlue, spotRed });

var functionType2 = PdfFunction.Type2(
    writer,
    new[] { 0.0f, 1.0f },
    null,
    new[] { 0.0f, 1.0f },
    new[] { 1.0f, 0.0f },
    1.0f);

var functionType3 = PdfFunction.Type3(
    writer,
    new[] { 0.0f, 1.0f },
    null,
    new[] { functionType2 },
    new float[] { },
    new[] { 0.0f, 1.0f });

var shading = PdfShading.Type2(
    writer,
    new DeviceNColor(devn, new[] { 1.0f, 1.0f }),
    new[] { 0.0f, 0.0f, 1.0f, 0.0f },
    new[] { 0.0f, 1.0f },
    functionType3,
    new[] { true, true });

var pattern = new PdfShadingPattern(shading);

var x = 40f;
var y = 641.89f;
var w = 400f;
var h = 150f;

var canvas = writer.DirectContent;
canvas.SaveState();
canvas.Rectangle(x, y, w, h);
canvas.Clip();
canvas.NewPath();
canvas.SetShadingFill(pattern);
canvas.ConcatCTM(-w, 0f, 0f, w, x + w, y + (h / 2f));
canvas.PaintShading(shading);
canvas.RestoreState();

document.Close();