简体   繁体   中英

pdf to .tiff conversion using java

I have created a web service that has a method which takes in a parameter of DataHandler. The purpose of this method is to read in what is sent over DataHandler and write it to a file(.tiff). When I pass in a .tiff file i can easily make a conversion but how should I make a conversion from a pdf file to a .tiff file using Java.

So if a user passes in a pdf file using DataHandler, how can I convert that to a .tiff file?

    PDDocument doc = PDDocument.loadNonSeq(new File(filename), null);
    boolean b;
    List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
    for (int p = 0; p < pages.size(); ++p)
    {
        // RGB image with 300 dpi
        BufferedImage bim = pages.get(p).convertToImage(BufferedImage.TYPE_INT_RGB, 300);

        // alternatively: B/W image with 300 dpi
        bim = pages.get(p).convertToImage(BufferedImage.TYPE_BYTE_BINARY, 300);

        // save as TIF with dpi in the metadata
        // PDFBox will choose the best compression for you
        // you need to add jai_imageio to your classpath for this to work
        b = ImageIOUtil.writeImage(bim, "page-" + (p+1) + ".tif", 300);
        if (!b)
        {
            // error handling
        }
    }

If your question is about converting to a multipage TIFF, please say so, I have a solution for this too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM