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}
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.*
15import com.pdftron.sdf.Obj
16import com.pdftron.sdf.SDFDoc
17import java.util.*
18
19class PatternTest : PDFNetSample() {
20 init {
21 setTitle(R.string.sample_pattern_title)
22 setDescription(R.string.sample_pattern_description)
23 }
24
25 override fun run(outputListener: OutputListener?) {
26 super.run(outputListener)
27 mOutputListener = outputListener
28 mFileList.clear()
29 printHeader(outputListener!!)
30
31 try {
32 PDFDoc().use { doc ->
33 val writer = ElementWriter()
34 val eb = ElementBuilder()
35
36 // The following sample illustrates how to create and use tiling patterns
37 var page = doc.pageCreate()
38 writer.begin(page)
39
40 var element = eb.createTextBegin(Font.create(doc, Font.e_times_bold), 1.0)
41 writer.writeElement(element) // Begin the text block
42
43 val data = "G"
44 element = eb.createTextRun(data)
45 element.setTextMatrix(720.0, 0.0, 0.0, 720.0, 20.0, 240.0)
46 var gs = element.gState
47 gs.textRenderMode = GState.e_fill_stroke_text
48 gs.lineWidth = 4.0
49
50 // Set the fill color space to the Pattern color space.
51 gs.fillColorSpace = ColorSpace.createPattern()
52 gs.setFillColor(PatternColor(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, 0.0, 612.0, 794.0)
67
68 // Set the fill color space to the Pattern color space.
69 gs = element.gState
70 gs.fillColorSpace = ColorSpace.createPattern()
71 gs.setFillColor(PatternColor(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, 0.0, 612.0, 794.0)
86
87 // Set the fill color space to the Pattern color space.
88 gs = element.gState
89 gs.fillColorSpace = ColorSpace.createPattern()
90
91 gs.setFillColor(PatternColor(CreateAxialShading(doc)))
92 element.setPathFill(true)
93
94 writer.writeElement(element)
95
96 writer.end() // save the page
97 doc.pagePushBack(page)
98 //-----------------------------------------------
99
100 doc.save(Utils.createExternalFile("patterns.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
101 mOutputListener!!.println("Done. Result saved in patterns.pdf...")
102 }
103 } catch (e: Exception) {
104 mOutputListener!!.printError(e.stackTrace)
105 }
106
107 for (file in mFileList) {
108 addToFileList(file)
109 }
110 printFooter(outputListener)
111 }
112
113 companion object {
114
115 private var mOutputListener: OutputListener? = null
116
117 private val mFileList = ArrayList<String>()
118
119 @Throws(PDFNetException::class)
120 internal fun CreateTilingPattern(doc: PDFDoc): Obj {
121 val writer = ElementWriter()
122 val eb = ElementBuilder()
123
124 // Create a new pattern content stream - a heart. ------------
125 writer.begin(doc)
126 eb.pathBegin()
127 eb.moveTo(0.0, 0.0)
128 eb.curveTo(500.0, 500.0, 125.0, 625.0, 0.0, 500.0)
129 eb.curveTo(-125.0, 625.0, -500.0, 500.0, 0.0, 0.0)
130 val heart = eb.pathEnd()
131 heart.setPathFill(true)
132
133 // Set heart color to red.
134 heart.gState.fillColorSpace = ColorSpace.createDeviceRGB()
135 heart.gState.fillColor = ColorPt(1.0, 0.0, 0.0)
136 writer.writeElement(heart)
137
138 val pattern_dict = writer.end()
139
140 // Initialize pattern dictionary. For details on what each parameter represents please
141 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
142 pattern_dict.putName("Type", "Pattern")
143 pattern_dict.putNumber("PatternType", 1.0)
144
145 // TilingType - Constant spacing.
146 pattern_dict.putNumber("TilingType", 1.0)
147
148 // This is a Type1 pattern - A colored tiling pattern.
149 pattern_dict.putNumber("PaintType", 1.0)
150
151 // Set bounding box
152 pattern_dict.putRect("BBox", -253.0, 0.0, 253.0, 545.0)
153
154 // Create and set the matrix
155 val pattern_mtx = Matrix2D(0.04, 0.0, 0.0, 0.04, 0.0, 0.0)
156 pattern_dict.putMatrix("Matrix", pattern_mtx)
157
158 // Set the desired horizontal and vertical spacing between pattern cells,
159 // measured in the pattern coordinate system.
160 pattern_dict.putNumber("XStep", 1000.0)
161 pattern_dict.putNumber("YStep", 1000.0)
162
163 return pattern_dict // finished creating the Pattern resource
164 }
165
166 @Throws(PDFNetException::class)
167 internal fun CreateImageTilingPattern(doc: PDFDoc): Obj {
168 val writer = ElementWriter()
169 val eb = ElementBuilder()
170
171 // Create a new pattern content stream - a single bitmap object ----------
172 writer.begin(doc)
173 val image = Image.create(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "dice.jpg")!!.absolutePath)
174 val img_element = eb.createImage(image, 0.0, 0.0, image.imageWidth.toDouble(), image.imageHeight.toDouble())
175 writer.writePlacedElement(img_element)
176 val pattern_dict = writer.end()
177
178 // Initialize pattern dictionary. For details on what each parameter represents please
179 // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
180 pattern_dict.putName("Type", "Pattern")
181 pattern_dict.putNumber("PatternType", 1.0)
182
183 // TilingType - Constant spacing.
184 pattern_dict.putNumber("TilingType", 1.0)
185
186 // This is a Type1 pattern - A colored tiling pattern.
187 pattern_dict.putNumber("PaintType", 1.0)
188
189 // Set bounding box
190 pattern_dict.putRect("BBox", -253.0, 0.0, 253.0, 545.0)
191
192 // Create and set the matrix
193 val pattern_mtx = Matrix2D(0.3, 0.0, 0.0, 0.3, 0.0, 0.0)
194 pattern_dict.putMatrix("Matrix", pattern_mtx)
195
196 // Set the desired horizontal and vertical spacing between pattern cells,
197 // measured in the pattern coordinate system.
198 pattern_dict.putNumber("XStep", 300.0)
199 pattern_dict.putNumber("YStep", 300.0)
200
201 return pattern_dict // finished creating the Pattern resource
202 }
203
204 @Throws(PDFNetException::class)
205 internal fun CreateAxialShading(doc: PDFDoc): Obj {
206 // Create a new Shading object ------------
207 val pattern_dict = doc.createIndirectDict()
208
209 // Initialize pattern dictionary. For details on what each parameter represents
210 // please refer to Tables 4.30 and 4.26 in PDF Reference Manual
211 pattern_dict.putName("Type", "Pattern")
212 pattern_dict.putNumber("PatternType", 2.0) // 2 stands for shading
213
214 val shadingDict = pattern_dict.putDict("Shading")
215 shadingDict.putNumber("ShadingType", 2.0)
216 shadingDict.putName("ColorSpace", "DeviceCMYK")
217
218 // pass the coordinates of the axial shading to the output
219 val shadingCoords = shadingDict.putArray("Coords")
220 shadingCoords.pushBackNumber(0.0)
221 shadingCoords.pushBackNumber(0.0)
222 shadingCoords.pushBackNumber(612.0)
223 shadingCoords.pushBackNumber(794.0)
224
225 // pass the function to the axial shading
226 val function = shadingDict.putDict("Function")
227 val C0 = function.putArray("C0")
228 C0.pushBackNumber(1.0)
229 C0.pushBackNumber(0.0)
230 C0.pushBackNumber(0.0)
231 C0.pushBackNumber(0.0)
232
233 val C1 = function.putArray("C1")
234 C1.pushBackNumber(0.0)
235 C1.pushBackNumber(1.0)
236 C1.pushBackNumber(0.0)
237 C1.pushBackNumber(0.0)
238
239 val domain = function.putArray("Domain")
240 domain.pushBackNumber(0.0)
241 domain.pushBackNumber(1.0)
242
243 function.putNumber("FunctionType", 2.0)
244 function.putNumber("N", 1.0)
245
246 return pattern_dict
247 }
248 }
249
250}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales