Pattern

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

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales