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
| import java.awt.Desktop;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.jpedal.PdfDecoder;
public class PdfToTiff {
/**
* @param args
*/
public static void main(String[] args) {
File inputPdfFile = new File("result11286.pdf");
InputStream is;
FileOutputStream fos = null;
byte[] b;
try {
is = new FileInputStream(inputPdfFile);
b = toByteArray(is);
is.close();
File out = new File("out.tiff");
fos = new FileOutputStream(out);
fos.write(PDFToImageMultiPage(b, "tiff"));
fos.close();
openFile(out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void openFile(File fileToOpen) {
if (Desktop.isDesktopSupported()) {
if (Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
try {
Desktop.getDesktop().open(fileToOpen);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static byte[] PDFToImageMultiPage(byte[] bai, String imageType) {
byte[] out = null;
try {
PdfDecoder decoder = new PdfDecoder();
decoder.openPdfArray(bai);
if (decoder.isFileViewable()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
boolean foundWriter = false;
Iterator writerIter = ImageIO
.getImageWritersByFormatName(imageType);
while (writerIter.hasNext() && !foundWriter) {
foundWriter = true;
ImageWriter writer = (ImageWriter) writerIter.next();
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String[] comp = param.getCompressionTypes();
for (int i= 0; i< comp.length; i++) {
System.out.println(comp[i]);
}
param.setCompressionType("ZLib");
param.setCompressionQuality(0.8f);
writer.prepareWriteSequence(null);
for (int i = 0; i < decoder.getPageCount(); i++) {
int pageNumber = i + 1;
BufferedImage image = decoder
.getPageAsImage(pageNumber);
IIOImage iioImage = new IIOImage(image, null, null);
writer.writeToSequence(iioImage, param);
}
writer.endWriteSequence();
ios.flush();
writer.dispose();
ios.close();
out = baos.toByteArray();
}
if (!foundWriter) {
throw new RuntimeException(
"Error: no writer found for image type '"
+ imageType + "'");
}
}
decoder.closePdfFile();
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
// Method reads from an InputStream and returns a byteArray
private static byte[] toByteArray(InputStream in) throws IOException {
if (in == null) {
return null;
}
OutputStream buffer = new ByteArrayOutputStream();
byte[] transfer = new byte[1024];
int count = 0;
while (count != -1) {
count = in.read(transfer);
if (count > 0) {
buffer.write(transfer, 0, count);
}
}
return ((ByteArrayOutputStream) buffer).toByteArray();
}
} |
Partager