Scheduled Maintenance
-
February 10, 2026, from 8:00 AM to 9:00 AM PST. Account-related operations might be temporarily unavailable.

Pattern

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

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.Matrix2D;
13import com.pdftron.common.PDFNetException;
14import com.pdftron.pdf.ColorPt;
15import com.pdftron.pdf.ColorSpace;
16import com.pdftron.pdf.Element;
17import com.pdftron.pdf.ElementBuilder;
18import com.pdftron.pdf.ElementWriter;
19import com.pdftron.pdf.Font;
20import com.pdftron.pdf.GState;
21import com.pdftron.pdf.Image;
22import com.pdftron.pdf.PDFDoc;
23import com.pdftron.pdf.Page;
24import com.pdftron.pdf.PatternColor;
25import com.pdftron.sdf.Obj;
26import com.pdftron.sdf.SDFDoc;
27
28import java.util.ArrayList;
29
30public class PatternTest extends PDFNetSample {
31
32 private static OutputListener mOutputListener;
33
34 private static ArrayList<String> mFileList = new ArrayList<>();
35
36 public PatternTest() {
37 setTitle(R.string.sample_pattern_title);
38 setDescription(R.string.sample_pattern_description);
39 }
40
41 @Override
42 public void run(OutputListener outputListener) {
43 super.run(outputListener);
44 mOutputListener = outputListener;
45 mFileList.clear();
46 printHeader(outputListener);
47
48 try (PDFDoc doc = new PDFDoc()) {
49 ElementWriter writer = new ElementWriter();
50 ElementBuilder eb = new ElementBuilder();
51
52 // The following sample illustrates how to create and use tiling patterns
53 Page page = doc.pageCreate();
54 writer.begin(page);
55
56 Element element = eb.createTextBegin(Font.create(doc, Font.e_times_bold), 1);
57 writer.writeElement(element); // Begin the text block
58
59 String data = "G";
60 element = eb.createTextRun(data);
61 element.setTextMatrix(720, 0, 0, 720, 20, 240);
62 GState gs = element.getGState();
63 gs.setTextRenderMode(GState.e_fill_stroke_text);
64 gs.setLineWidth(4);
65
66 // Set the fill color space to the Pattern color space.
67 gs.setFillColorSpace(ColorSpace.createPattern());
68 gs.setFillColor(new PatternColor(CreateTilingPattern(doc)));
69
70 writer.writeElement(element);
71 writer.writeElement(eb.createTextEnd()); // Finish the text block
72
73 writer.end(); // Save the page
74 doc.pagePushBack(page);
75 //-----------------------------------------------
76
77 /// The following sample illustrates how to create and use image tiling pattern
78 page = doc.pageCreate();
79 writer.begin(page);
80
81 eb.reset();
82 element = eb.createRect(0, 0, 612, 794);
83
84 // Set the fill color space to the Pattern color space.
85 gs = element.getGState();
86 gs.setFillColorSpace(ColorSpace.createPattern());
87 gs.setFillColor(new PatternColor(CreateImageTilingPattern(doc)));
88 element.setPathFill(true);
89
90 writer.writeElement(element);
91
92 writer.end(); // Save the page
93 doc.pagePushBack(page);
94 //-----------------------------------------------
95
96 /// The following sample illustrates how to create and use PDF shadings
97 page = doc.pageCreate();
98 writer.begin(page);
99
100 eb.reset();
101 element = eb.createRect(0, 0, 612, 794);
102
103 // Set the fill color space to the Pattern color space.
104 gs = element.getGState();
105 gs.setFillColorSpace(ColorSpace.createPattern());
106
107 gs.setFillColor(new PatternColor(CreateAxialShading(doc)));
108 element.setPathFill(true);
109
110 writer.writeElement(element);
111
112 writer.end(); // save the page
113 doc.pagePushBack(page);
114 //-----------------------------------------------
115
116 doc.save(Utils.createExternalFile("patterns.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
117 mOutputListener.println("Done. Result saved in patterns.pdf...");
118 } catch (Exception e) {
119 mOutputListener.printError(e.getStackTrace());
120 }
121
122 for (String file : mFileList) {
123 addToFileList(file);
124 }
125 printFooter(outputListener);
126 }
127
128
129
130 static Obj CreateTilingPattern(PDFDoc doc) throws PDFNetException {
131 ElementWriter writer = new ElementWriter();
132 ElementBuilder eb = new ElementBuilder();
133
134 // Create a new pattern content stream - a heart. ------------
135 writer.begin(doc);
136 eb.pathBegin();
137 eb.moveTo(0, 0);
138 eb.curveTo(500, 500, 125, 625, 0, 500);
139 eb.curveTo(-125, 625, -500, 500, 0, 0);
140 Element heart = eb.pathEnd();
141 heart.setPathFill(true);
142
143 // Set heart color to red.
144 heart.getGState().setFillColorSpace(ColorSpace.createDeviceRGB());
145 heart.getGState().setFillColor(new ColorPt(1, 0, 0));
146 writer.writeElement(heart);
147
148 Obj pattern_dict = writer.end();
149
150 // Initialize pattern dictionary. For details on what each parameter represents please
151 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
152 pattern_dict.putName("Type", "Pattern");
153 pattern_dict.putNumber("PatternType", 1);
154
155 // TilingType - Constant spacing.
156 pattern_dict.putNumber("TilingType", 1);
157
158 // This is a Type1 pattern - A colored tiling pattern.
159 pattern_dict.putNumber("PaintType", 1);
160
161 // Set bounding box
162 pattern_dict.putRect("BBox", -253, 0, 253, 545);
163
164 // Create and set the matrix
165 Matrix2D pattern_mtx = new Matrix2D(0.04, 0, 0, 0.04, 0, 0);
166 pattern_dict.putMatrix("Matrix", pattern_mtx);
167
168 // Set the desired horizontal and vertical spacing between pattern cells,
169 // measured in the pattern coordinate system.
170 pattern_dict.putNumber("XStep", 1000);
171 pattern_dict.putNumber("YStep", 1000);
172
173 return pattern_dict; // finished creating the Pattern resource
174 }
175
176 static Obj CreateImageTilingPattern(PDFDoc doc) throws PDFNetException {
177 ElementWriter writer = new ElementWriter();
178 ElementBuilder eb = new ElementBuilder();
179
180 // Create a new pattern content stream - a single bitmap object ----------
181 writer.begin(doc);
182 Image image = Image.create(doc, (Utils.getAssetTempFile(INPUT_PATH + "dice.jpg").getAbsolutePath()));
183 Element img_element = eb.createImage(image, 0, 0, image.getImageWidth(), image.getImageHeight());
184 writer.writePlacedElement(img_element);
185 Obj pattern_dict = writer.end();
186
187 // Initialize pattern dictionary. For details on what each parameter represents please
188 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
189 pattern_dict.putName("Type", "Pattern");
190 pattern_dict.putNumber("PatternType", 1);
191
192 // TilingType - Constant spacing.
193 pattern_dict.putNumber("TilingType", 1);
194
195 // This is a Type1 pattern - A colored tiling pattern.
196 pattern_dict.putNumber("PaintType", 1);
197
198 // Set bounding box
199 pattern_dict.putRect("BBox", -253, 0, 253, 545);
200
201 // Create and set the matrix
202 Matrix2D pattern_mtx = new Matrix2D(0.3, 0, 0, 0.3, 0, 0);
203 pattern_dict.putMatrix("Matrix", pattern_mtx);
204
205 // Set the desired horizontal and vertical spacing between pattern cells,
206 // measured in the pattern coordinate system.
207 pattern_dict.putNumber("XStep", 300);
208 pattern_dict.putNumber("YStep", 300);
209
210 return pattern_dict; // finished creating the Pattern resource
211 }
212
213 static Obj CreateAxialShading(PDFDoc doc) throws PDFNetException {
214 // Create a new Shading object ------------
215 Obj pattern_dict = doc.createIndirectDict();
216
217 // Initialize pattern dictionary. For details on what each parameter represents
218 // please refer to Tables 4.30 and 4.26 in PDF Reference Manual
219 pattern_dict.putName("Type", "Pattern");
220 pattern_dict.putNumber("PatternType", 2); // 2 stands for shading
221
222 Obj shadingDict = pattern_dict.putDict("Shading");
223 shadingDict.putNumber("ShadingType", 2);
224 shadingDict.putName("ColorSpace", "DeviceCMYK");
225
226 // pass the coordinates of the axial shading to the output
227 Obj shadingCoords = shadingDict.putArray("Coords");
228 shadingCoords.pushBackNumber(0);
229 shadingCoords.pushBackNumber(0);
230 shadingCoords.pushBackNumber(612);
231 shadingCoords.pushBackNumber(794);
232
233 // pass the function to the axial shading
234 Obj function = shadingDict.putDict("Function");
235 Obj C0 = function.putArray("C0");
236 C0.pushBackNumber(1);
237 C0.pushBackNumber(0);
238 C0.pushBackNumber(0);
239 C0.pushBackNumber(0);
240
241 Obj C1 = function.putArray("C1");
242 C1.pushBackNumber(0);
243 C1.pushBackNumber(1);
244 C1.pushBackNumber(0);
245 C1.pushBackNumber(0);
246
247 Obj domain = function.putArray("Domain");
248 domain.pushBackNumber(0);
249 domain.pushBackNumber(1);
250
251 function.putNumber("FunctionType", 2);
252 function.putNumber("N", 1);
253
254 return pattern_dict;
255 }
256
257
258}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales