01: package it.stefanochizzolini.clown.samples;
02:
03: import it.stefanochizzolini.clown.bytes.IBuffer;
04: import it.stefanochizzolini.clown.objects.*;
05: import it.stefanochizzolini.clown.files.File;
06: import it.stefanochizzolini.clown.tokens.FileFormatException;
07:
08: /**
09: This sample demonstrates how to extract XObject images from a PDF file.
10: <h3>Remarks</h3>
11: <p>Inline images are ignored.</p>
12: <p>XObject images other than JPEG aren't currently supported for handling.</p>
13: */
14: public class ImageExtractionSample implements ISample {
15: public void run(PDFClownSampleLoader loader) {
16: // (boilerplate user choice -- ignore it)
17: String filePath = loader
18: .getPdfFileChoice("Please select a PDF file");
19:
20: // 1. Open the PDF file!
21: File file;
22: try {
23: file = new File(filePath);
24: } catch (FileFormatException e) {
25: throw new RuntimeException(filePath
26: + " file has a bad file format.", e);
27: } catch (Exception e) {
28: throw new RuntimeException(
29: filePath + " file access error.", e);
30: }
31:
32: // 2.1. Iterating through the indirect object collection...
33: int index = 0;
34: for (PdfIndirectObject indirectObject : file
35: .getIndirectObjects()) {
36: // Get the data object associated to the indirect object!
37: PdfDataObject dataObject = indirectObject.getDataObject();
38: // Is this data object a stream?
39: if (dataObject instanceof PdfStream) {
40: PdfDictionary header = ((PdfStream) dataObject)
41: .getHeader();
42: // Is this stream an image?
43: if (header.containsKey(PdfName.Type)
44: && header.get(PdfName.Type).equals(
45: PdfName.XObject)
46: && header.get(PdfName.Subtype).equals(
47: PdfName.Image)) {
48: // 2.1.1. Is this image a pass-through JPEG image?
49: if (header.get(PdfName.Filter).equals(
50: PdfName.DCTDecode)) // JPEG image.
51: {
52: // Get the image data (keeping it encoded)!
53: IBuffer body = ((PdfStream) dataObject)
54: .getBody(false);
55: // Export the image!
56: exportImage(body, loader.getOutputPath()
57: + java.io.File.separator
58: + "ImageExtractionSample_" + (index++)
59: + ".jpg");
60: } else // Unsupported image.
61: {
62: System.out.println("Image XObject "
63: + indirectObject.getReference()
64: + " couldn't be extracted (filter: "
65: + header.get(PdfName.Filter) + ")");
66: }
67: }
68: }
69: }
70: }
71:
72: private void exportImage(IBuffer data, String outputPath) {
73: java.io.File outputFile = new java.io.File(outputPath);
74: java.io.BufferedOutputStream outputStream;
75: try {
76: outputFile.createNewFile();
77: outputStream = new java.io.BufferedOutputStream(
78: new java.io.FileOutputStream(outputFile));
79: } catch (Exception e) {
80: throw new RuntimeException(outputFile.getPath()
81: + " file couldn't be created.", e);
82: }
83:
84: try {
85: outputStream.write(data.toByteArray());
86: outputStream.close();
87: } catch (Exception e) {
88: throw new RuntimeException(outputFile.getPath()
89: + " file writing has failed.", e);
90: }
91:
92: System.out.println("Output: " + outputFile.getPath());
93: }
94: }
|