An example of creating an using uncolored tiling pattern

Q: We need an example of using (and resuing) a PDF pattern containing
an imagemask with a paint type of 2 (no embedded color information).

1) Add the pattern
2) Use the pattern in a pathfill with an RGB fillcolor
3) Use the pattern in a pathfill with a different RGB fillcolor
-----------
A:

You may want to take a look at PatternTest sample project
(http://www.pdftron.com/net/samplecode.html#Pattern).

This sample shows how to build a colored pattern, but creating
uncolored pattern is very similar. The main difference is that you
would set 'PaintType' parameter to '2' (instead of '1' for colored
patterns) and would comment out all references to color space (e.g.
SetFillColor(...))

Another thing to keep in mind is that uncolored patterns can contain
image masks, but
no other kinds of images (e.g. RGB or 8BPP grayscale).

The following sample illustrates how to use an uncolored tiling
pattern:

Page page = doc.PageCreate();
writer.Begin(page);
Element* element = eb.CreateRect(100,100, 312, 394);

// Set the fill color space to the Pattern color space.
GState* gs = element->GetGState();

A Pattern color space representing an uncolored tiling pattern requires
a parameter: an object identifying the underlying color space in which
the actual color of the pattern is to be specified. The underlying
color space is given as the second element of the array that defines
the Pattern color space:

// Set the color space to uncolored pattern
Obj* pattern_arr = doc.CreateIndirectArray();
pattern_arr->PushBack(new Name("Pattern"));
pattern_arr->PushBack(new Name("DeviceRGB")); // or DeviceCMYK etc...
ColorSpace pattern_cs(pattern_arr);
gs->SetFillColorSpace(pattern_cs);

// Create the pattern and set the fill color...
Obj* pattern_color = CreateUncoloredTilingPattern(doc);
ColorPt red(1, 0, 0);
gs->SetFillColor(pattern_color, red);
element->SetPathFill(true);

writer.WriteElement(element);

//----------------------------------------------

// Reuse the same color space and pattern color for other elements....
element = eb.CreateRect(0, 0, 100, 100);
gs = element->GetGState();

gs->SetFillColorSpace(pattern_cs);

// Create the pattern and set the fill color...
ColorPt blue(0, 0, 1);
gs->SetFillColor(pattern_color, blue);
element->SetPathFill(true);

writer.End(); // Save the page
doc.PagePushBack(page);

You can also reuse these objects on other pages.