/
TestMaskedDraws.java
155 lines (129 loc) · 6.14 KB
/
TestMaskedDraws.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package de.rototor.pdfbox.graphics2d;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.shading.PDShading;
import org.apache.pdfbox.util.Matrix;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class TestMaskedDraws
{
private static final float POINTS_PER_INCH = 72;
/**
* user space units per millimeter
*/
private static final float POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH;
@Test
public void testMaskedDraws() throws IOException
{
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(new PDRectangle(302 * POINTS_PER_MM, 111 * POINTS_PER_MM));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
pdfBoxGraphics2D.setPaintApplier(new PdfBoxGraphics2DPaintApplier()
{
@Override
protected PDShading applyPaint(Paint paint, PaintApplierState state) throws IOException
{
PDShading ret = super.applyPaint(paint, state);
InputStream resourceAsStream = PdfBoxGraphics2dTest.class.getResourceAsStream(
"maskingExample.png");
assert resourceAsStream != null;
BufferedImage mask = ImageIO.read(resourceAsStream);
state.setupLuminosityMasking(mask, page.getMediaBox());
return ret;
}
});
LinearGradientPaint linearGradientPaint = new LinearGradientPaint(0, 0,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight(),
new float[] { 0.0f, .2f, .4f, .9f, 1f },
new Color[] { Color.YELLOW, Color.GREEN, Color.RED, Color.BLUE, Color.PINK });
pdfBoxGraphics2D.setPaint(linearGradientPaint);
pdfBoxGraphics2D.fill(new Rectangle2D.Double(0, 0, page.getMediaBox().getWidth(),
page.getMediaBox().getHeight()));
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 0);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
File file = new File("target/test/masked_draws.pdf");
// noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
document.save(file);
document.close();
}
@Test
public void testFormMasking() throws IOException
{
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(new PDRectangle(302 * POINTS_PER_MM, 111 * POINTS_PER_MM));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D maskingGraphics = new PdfBoxGraphics2D(document,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
LinearGradientPaint linearGradientPaintForMask = new LinearGradientPaint(0, 0,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight(),
new float[] { 0.0f, .2f, .4f, .9f, 1f },
new Color[] { Color.GRAY, Color.LIGHT_GRAY, new Color(120, 120, 120),
new Color(30, 30, 30), new Color(255, 255, 255) });
maskingGraphics.setPaint(linearGradientPaintForMask);
double wRadius = page.getMediaBox().getWidth() / 2;
double hRadius = page.getMediaBox().getHeight() / 2;
maskingGraphics.setStroke(new BasicStroke(10));
for (int i = 1; i < 10; i++)
{
double ourWRadius = wRadius * i / 10;
double ourHRadius = hRadius * i / 10;
maskingGraphics.draw(
new Ellipse2D.Double(wRadius - ourWRadius, hRadius - ourHRadius, ourWRadius * 2,
ourHRadius * 2));
}
maskingGraphics.dispose();
final PDFormXObject xFormObject = maskingGraphics.getXFormObject();
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
pdfBoxGraphics2D.setPaintApplier(new PdfBoxGraphics2DPaintApplier()
{
@Override
protected PDShading applyPaint(Paint paint, PaintApplierState state) throws IOException
{
PDShading ret = super.applyPaint(paint, state);
state.setupLuminosityMasking(xFormObject);
return ret;
}
});
LinearGradientPaint linearGradientPaint = new LinearGradientPaint(0, 0,
page.getMediaBox().getWidth(), page.getMediaBox().getHeight(),
new float[] { 0.0f, .2f, .4f, .9f, 1f },
new Color[] { Color.YELLOW, Color.GREEN, Color.RED, Color.BLUE, Color.PINK });
pdfBoxGraphics2D.setPaint(linearGradientPaint);
pdfBoxGraphics2D.fill(new Rectangle2D.Double(0, 0, page.getMediaBox().getWidth(),
page.getMediaBox().getHeight()));
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 0);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
File file = new File("target/test/masked_form.pdf");
// noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
document.save(file);
document.close();
}
}