/
ExtractAnnotationImageTest.java
202 lines (181 loc) · 7.93 KB
/
ExtractAnnotationImageTest.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package mkl.testarea.pdfbox1.extract;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* <a href="http://stackoverflow.com/questions/33295483/how-can-i-extract-image-from-button-icon-in-pdf-using-apache-pdfbox">
* How can i extract image from button icon in PDF using Apache PDFBox
* </a>
* <br/>
* <a href="http://examples.itextpdf.com/results/part2/chapter08/advertisement2.pdf">advertisement2.pdf</a>
* <br/>
* <a href="http://examples.itextpdf.com/results/part2/chapter08/buttons.pdf">buttons.pdf</a>
* <p>
* This test demonstrates how to extract images from annotations in general and,
* therefore, also buttons in particular.
* </p>
*
* @author mkl
*/
public class ExtractAnnotationImageTest
{
final static File RESULT_FOLDER = new File("target/test-outputs", "extract");
/**
* As requested by the OP, {@link #main(String[])} provides a way
* to use this extractor without requiring Jthe Unit framework and
* furthermore without requiring the PDF file to be a resource.
*/
public static void main(String[] args) throws IOException
{
ExtractAnnotationImageTest extractor = new ExtractAnnotationImageTest();
for (String arg : args)
{
try (PDDocument document = PDDocument.load(arg))
{
extractor.extractAnnotationImages(document, arg+"%s.%s");;
}
}
}
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
RESULT_FOLDER.mkdirs();
}
/**
* Test using <a href="http://examples.itextpdf.com/results/part2/chapter08/advertisement2.pdf">advertisement2.pdf</a>
* created by <a href="http://itextpdf.com/examples/iia.php?id=156">part2.chapter08.ReplaceIcon</a>
* from ITEXT IN ACTION — SECOND EDITION.
*/
@Test
public void testAdvertisement2Pdf() throws IOException
{
try (InputStream resource = getClass().getResourceAsStream("advertisement2.pdf"))
{
PDDocument document = PDDocument.load(resource);
extractAnnotationImages(document, new File(RESULT_FOLDER, "advertisement2%s.%s").toString());;
}
}
/**
* Test using <a href="http://examples.itextpdf.com/results/part2/chapter08/buttons.pdf">buttons.pdf</a>
* created by <a href="http://itextpdf.com/examples/iia.php?id=154">part2.chapter08.Buttons</a>
* from ITEXT IN ACTION — SECOND EDITION.
*/
@Test
public void testButtonsPdf() throws IOException
{
try (InputStream resource = getClass().getResourceAsStream("buttons.pdf"))
{
PDDocument document = PDDocument.load(resource);
extractAnnotationImages(document, new File(RESULT_FOLDER, "buttons%s.%s").toString());;
}
}
/**
* Test using <a href="http://www.docdroid.net/TDGVQzg/imageicon.pdf.html">imageicon.pdf</a>
* created by the OP.
*/
@Test
public void testImageiconPdf() throws IOException
{
try (InputStream resource = getClass().getResourceAsStream("imageicon.pdf"))
{
PDDocument document = PDDocument.load(resource);
extractAnnotationImages(document, new File(RESULT_FOLDER, "imageicon%s.%s").toString());;
}
}
public void extractAnnotationImages(PDDocument document, String fileNameFormat) throws IOException
{
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
if (pages == null)
return;
for (int i = 0; i < pages.size(); i++)
{
String pageFormat = String.format(fileNameFormat, "-" + i + "%s", "%s");
extractAnnotationImages(pages.get(i), pageFormat);
}
}
public void extractAnnotationImages(PDPage page, String pageFormat) throws IOException
{
List<PDAnnotation> annotations = page.getAnnotations();
if (annotations == null)
return;
for (int i = 0; i < annotations.size(); i++)
{
PDAnnotation annotation = annotations.get(i);
String annotationFormat = annotation.getAnnotationName() != null && annotation.getAnnotationName().length() > 0
? String.format(pageFormat, "-" + annotation.getAnnotationName() + "%s", "%s")
: String.format(pageFormat, "-" + i + "%s", "%s");
extractAnnotationImages(annotation, annotationFormat);
}
}
public void extractAnnotationImages(PDAnnotation annotation, String annotationFormat) throws IOException
{
PDAppearanceDictionary appearance = annotation.getAppearance();
extractAnnotationImages(appearance.getDownAppearance(), String.format(annotationFormat, "-Down%s", "%s"));
extractAnnotationImages(appearance.getNormalAppearance(), String.format(annotationFormat, "-Normal%s", "%s"));
extractAnnotationImages(appearance.getRolloverAppearance(), String.format(annotationFormat, "-Rollover%s", "%s"));
}
public void extractAnnotationImages(Map<String, PDAppearanceStream> stateAppearances, String stateFormat) throws IOException
{
if (stateAppearances == null)
return;
for (Map.Entry<String, PDAppearanceStream> entry: stateAppearances.entrySet())
{
String appearanceFormat = String.format(stateFormat, "-" + entry.getKey() + "%s", "%s");
extractAnnotationImages(entry.getValue(), appearanceFormat);
}
}
public void extractAnnotationImages(PDAppearanceStream appearance, String appearanceFormat) throws IOException
{
PDResources resources = appearance.getResources();
if (resources == null)
return;
Map<String, PDXObject> xObjects = resources.getXObjects();
if (xObjects == null)
return;
for (Map.Entry<String, PDXObject> entry : xObjects.entrySet())
{
PDXObject xObject = entry.getValue();
String xObjectFormat = String.format(appearanceFormat, "-" + entry.getKey() + "%s", "%s");
if (xObject instanceof PDXObjectForm)
extractAnnotationImages((PDXObjectForm)xObject, xObjectFormat);
else if (xObject instanceof PDXObjectImage)
extractAnnotationImages((PDXObjectImage)xObject, xObjectFormat);
}
}
public void extractAnnotationImages(PDXObjectForm form, String imageFormat) throws IOException
{
PDResources resources = form.getResources();
if (resources == null)
return;
Map<String, PDXObject> xObjects = resources.getXObjects();
if (xObjects == null)
return;
for (Map.Entry<String, PDXObject> entry : xObjects.entrySet())
{
PDXObject xObject = entry.getValue();
String xObjectFormat = String.format(imageFormat, "-" + entry.getKey() + "%s", "%s");
if (xObject instanceof PDXObjectForm)
extractAnnotationImages((PDXObjectForm)xObject, xObjectFormat);
else if (xObject instanceof PDXObjectImage)
extractAnnotationImages((PDXObjectImage)xObject, xObjectFormat);
}
}
public void extractAnnotationImages(PDXObjectImage image, String imageFormat) throws IOException
{
image.write2OutputStream(new FileOutputStream(String.format(imageFormat, "", image.getSuffix())));
}
}