Pattern

Sample C# code for using Apryse SDK to create various patterns and shadings in PDF files. Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12namespace PatternTestCS
13{
14 /// <summary>
15 /// This example illustrates how to create PDF patterns and shadings.
16 /// </summary>
17 class PatternTest
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static PatternTest() {}
21
22 // Relative path to the folder containing test files.
23 const string input_path = "../../../../TestFiles/";
24 const string output_path = "../../../../TestFiles/Output/";
25
26 static void Main(string[] args)
27 {
28 PDFNet.Initialize(PDFTronLicense.Key);
29 try
30 {
31 using (PDFDoc doc = new PDFDoc())
32 using (ElementWriter writer = new ElementWriter())
33 using (ElementBuilder eb = new ElementBuilder())
34 {
35 // The following sample illustrates how to create and use tiling patterns
36 Page page = doc.PageCreate();
37 writer.Begin(page);
38
39 Element element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_bold), 1);
40 writer.WriteElement(element); // Begin the text block
41
42 element = eb.CreateTextRun("G");
43 element.SetTextMatrix(720, 0, 0, 720, 20, 240);
44 GState gs = element.GetGState();
45 gs.SetTextRenderMode(GState.TextRenderingMode.e_fill_stroke_text);
46 gs.SetLineWidth(4);
47
48 // Set the fill color space to the Pattern color space.
49 gs.SetFillColorSpace(ColorSpace.CreatePattern());
50 gs.SetFillColor(CreateTilingPattern(doc));
51
52 writer.WriteElement(element);
53 writer.WriteElement(eb.CreateTextEnd()); // Finish the text block
54
55 writer.End(); // Save the page
56 doc.PagePushBack(page);
57 //-----------------------------------------------
58
59 /// The following sample illustrates how to create and use image tiling pattern
60 page = doc.PageCreate();
61 writer.Begin(page);
62
63 eb.Reset();
64 element = eb.CreateRect(0, 0, 612, 794);
65
66 // Set the fill color space to the Pattern color space.
67 gs = element.GetGState();
68 gs.SetFillColorSpace(ColorSpace.CreatePattern());
69 gs.SetFillColor(CreateImageTilingPattern(doc));
70 element.SetPathFill(true);
71
72 writer.WriteElement(element);
73
74 writer.End(); // Save the page
75 doc.PagePushBack(page);
76 //-----------------------------------------------
77
78 /// The following sample illustrates how to create and use PDF shadings
79 page = doc.PageCreate();
80 writer.Begin(page);
81
82 eb.Reset();
83 element = eb.CreateRect(0, 0, 612, 794);
84
85 // Set the fill color space to the Pattern color space.
86 gs = element.GetGState();
87 gs.SetFillColorSpace(ColorSpace.CreatePattern());
88 gs.SetFillColor(CreateAxialShading(doc));
89 element.SetPathFill(true);
90
91 writer.WriteElement(element);
92
93 writer.End(); // save the page
94 doc.PagePushBack(page);
95 //-----------------------------------------------
96
97 doc.Save(output_path + "patterns.pdf", SDFDoc.SaveOptions.e_remove_unused);
98 Console.WriteLine("Done. Result saved in patterns.pdf...");
99 }
100 }
101 catch (PDFNetException e)
102 {
103 Console.WriteLine(e.Message);
104 }
105 PDFNet.Terminate();
106 }
107
108 static PatternColor CreateTilingPattern(PDFDoc doc)
109 {
110 using (ElementWriter writer = new ElementWriter())
111 using (ElementBuilder eb = new ElementBuilder())
112 {
113 // Create a new pattern content stream - a heart. ------------
114 writer.Begin(doc);
115 eb.PathBegin();
116 eb.MoveTo(0, 0);
117 eb.CurveTo(500, 500, 125, 625, 0, 500);
118 eb.CurveTo(-125, 625, -500, 500, 0, 0);
119 Element heart = eb.PathEnd();
120 heart.SetPathFill(true);
121
122 // Set heart color to red.
123 heart.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
124 heart.GetGState().SetFillColor(new ColorPt(1, 0, 0));
125 writer.WriteElement(heart);
126
127 Obj pattern_dict = writer.End();
128
129 // Initialize pattern dictionary. For details on what each parameter represents please
130 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
131 pattern_dict.PutName("Type", "Pattern");
132 pattern_dict.PutNumber("PatternType", 1);
133
134 // TilingType - Constant spacing.
135 pattern_dict.PutNumber("TilingType", 1);
136
137 // This is a Type1 pattern - A colored tiling pattern.
138 pattern_dict.PutNumber("PaintType", 1);
139
140 // Set bounding box
141 pattern_dict.PutRect("BBox", -253, 0, 253, 545);
142
143 // Set the pattern matrix
144 pattern_dict.PutMatrix("Matrix", new Matrix2D(0.04, 0, 0, 0.04, 0, 0));
145
146 // Set the desired horizontal and vertical spacing between pattern cells,
147 // measured in the pattern coordinate system.
148 pattern_dict.PutNumber("XStep", 1000);
149 pattern_dict.PutNumber("YStep", 1000);
150
151 return new PatternColor(pattern_dict); // finished creating the Pattern resource
152 }
153 }
154
155 static PatternColor CreateImageTilingPattern(PDFDoc doc)
156 {
157 using (ElementWriter writer = new ElementWriter())
158 using (ElementBuilder eb = new ElementBuilder())
159 {
160
161 // Create a new pattern content stream - a single bitmap object ----------
162 writer.Begin(doc);
163 Image img = Image.Create(doc, input_path + "butterfly.png");
164 Element img_element = eb.CreateImage(img, 0, 0, img.GetImageWidth(), img.GetImageHeight());
165 writer.WritePlacedElement(img_element);
166 Obj pattern_dict = writer.End();
167
168 // Initialize pattern dictionary. For details on what each parameter represents please
169 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
170 pattern_dict.PutName("Type", "Pattern");
171 pattern_dict.PutNumber("PatternType", 1);
172
173 // TilingType - Constant spacing.
174 pattern_dict.PutNumber("TilingType", 1);
175
176 // This is a Type1 pattern - A colored tiling pattern.
177 pattern_dict.PutNumber("PaintType", 1);
178
179 // Set bounding box
180 pattern_dict.PutRect("BBox", -253, 0, 253, 545);
181
182 // Set the pattern matrix
183 pattern_dict.PutMatrix("Matrix", new Matrix2D(0.3, 0, 0, 0.3, 0, 0));
184
185 // Set the desired horizontal and vertical spacing between pattern cells,
186 // measured in the pattern coordinate system.
187 pattern_dict.PutNumber("XStep", 300);
188 pattern_dict.PutNumber("YStep", 300);
189
190 return new PatternColor(pattern_dict); // finished creating the Pattern resource
191 }
192 }
193
194 static PatternColor CreateAxialShading(PDFDoc doc)
195 {
196 // Create a new Shading object ------------
197 Obj pattern_dict = doc.CreateIndirectDict();
198
199 // Initialize pattern dictionary. For details on what each parameter represents
200 // please refer to Tables 4.30 and 4.26 in PDF Reference Manual
201 pattern_dict.PutName("Type", "Pattern");
202 pattern_dict.PutNumber("PatternType", 2); // 2 stands for shading
203
204 Obj shadingDict = pattern_dict.PutDict("Shading");
205 shadingDict.PutNumber("ShadingType", 2);
206 shadingDict.PutName("ColorSpace", "DeviceCMYK");
207
208 // Set the coordinates of the axial shading to the output
209 shadingDict.PutRect("Coords", 0, 0, 612, 794);
210
211 // Set the Functions for the axial shading
212 Obj funct = shadingDict.PutDict("Function");
213 Obj C0 = funct.PutArray("C0");
214 C0.PushBackNumber(1);
215 C0.PushBackNumber(0);
216 C0.PushBackNumber(0);
217 C0.PushBackNumber(0);
218
219 Obj C1 = funct.PutArray("C1");
220 C1.PushBackNumber(0);
221 C1.PushBackNumber(1);
222 C1.PushBackNumber(0);
223 C1.PushBackNumber(0);
224
225 Obj domain = funct.PutArray("Domain");
226 domain.PushBackNumber(0);
227 domain.PushBackNumber(1);
228
229 funct.PutNumber("FunctionType", 2);
230 funct.PutNumber("N", 1);
231
232 return new PatternColor(pattern_dict);
233 }
234 }
235}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales