- 论坛徽章:
- 0
|
我收藏过一个文章里面讲到用JMF把PDF转换为PNG 有使用TIF 你看看能不能帮助你
- Here is some code that does the trick..
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import java.awt.image.RenderedImage;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.util.Vector;
- import javax.imageio.ImageIO;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletResponse;
- import com.sun.media.jai.codec.ImageCodec;
- import com.sun.media.jai.codec.ImageDecoder;
- import com.sun.media.jai.codec.ImageEncoder;
- import com.sun.media.jai.codec.PNGEncodeParam;
- import com.sun.media.jai.codec.SeekableStream;
- import com.sun.media.jai.codec.TIFFDecodeParam;
- import com.sun.media.jai.codec.TIFFDirectory;
- import multivalent.Behavior;
- import multivalent.Context;
- import multivalent.Document;
- import multivalent.Node;
- import multivalent.std.adaptor.pdf.PDF;
- public class PDFToPNG extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public byte[] ConvertToPngImage(byte[] tiffRawData, HttpServletResponse res)
- throws Exception {
- Vector pngs = new Vector();
- // set stream to the tiff url
- SeekableStream tiffStream = SeekableStream.wrapInputStream(
- new ByteArrayInputStream(tiffRawData), true);
- // how many pages in one tiff
- int pageNumber = TIFFDirectory.getNumDirectories(tiffStream);
- TIFFDecodeParam decodeParam = new TIFFDecodeParam();
- decodeParam.setDecodePaletteAsShorts(true);
- ImageDecoder tiffDecoder = ImageCodec.createImageDecoder("tiff",
- tiffStream, decodeParam);
- // for (int p = 0; p < pageNumber; p ++) {
- // render the current page
- RenderedImage tiffPage = tiffDecoder.decodeAsRenderedImage();
- PNGEncodeParam png = PNGEncodeParam.getDefaultEncodeParam(tiffPage);
- // The png stream is outputted to a file. Change the directory
- // accordingly.
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // Gets a PNG encoder.
- ImageEncoder pngEncoder = ImageCodec.createImageEncoder("PNG", baos,png);
- // Encodes the RenderedImage object.
- pngEncoder.encode(tiffPage);
- byte[] content = baos.toByteArray();
- baos.close();
- return content;
- }
- public static void main(String args[]) {
- File outfile = new File("c:
- file.png");
- try {
- PDF pdf = (PDF) Behavior.getInstance("AdobePDF", "AdobePDF", null,
- null, null);
- File file = new File("c:
- somepdf.pdf");
- pdf.setInput(file);
- Document doc = new Document("doc", null, null);
- pdf.parse(doc);
- doc.clear();
- doc.putAttr(Document.ATTR_PAGE, Integer.toString(1));
- pdf.parse(doc);
- Node top = doc.childAt(0);
- doc.formatBeforeAfter(200, 200, null);
- int w = top.bbox.width;
- int h = top.bbox.height;
- BufferedImage img = new BufferedImage(w, h,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g = img.createGraphics();
- g.setClip(0, 0, w, h);
- g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
- RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g.setRenderingHint(RenderingHints.KEY_RENDERING,
- RenderingHints.VALUE_RENDER_QUALITY);
- Context cx = doc.getStyleSheet().getContext(g, null);
- top.paintBeforeAfter(g.getClipBounds(), cx);
- ImageIO.write(img, "png", outfile);
- doc.removeAllChildren();
- cx.reset();
- g.dispose();
- pdf.getReader().close();
- outfile = null;
- doc = null;
- } catch (Exception e) {
- }
- }
- }
复制代码 |
|