Quick Start (SwaRef): Converting a PDF document to a set of JPEG files using the web service API

The following Java code example converts a PDF document called Loan.pdf to a set of JPEG files and stores them in the C:\Adobe directory. Each file is named tempFile[index].jpg, where the first image file is named tempFile0.jpg. (See Converting PDF Documents to Image Formats.)

/** 
    * Ensure that you create Java proxy files that consume 
    * the Convert PDF services  WSDL. You can use JAX-WS to create 
    * the proxy Java files.   
    *    
    * For information, see "Invoking LiveCycle using SwaRef" in Programming with LiveCycle.   
    */ 
     
import java.io.*; 
import java.io.InputStream; 
import java.util.*; 
 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.xml.ws.BindingProvider; s 
 
import com.adobe.idp.services.*; 
 
public class ConvertPDFToImageSwaRef { 
     
       public static void main(String[] args) { 
        try { 
            // Setting configurations to retrieve the Web service 
            String url = "http://localhost:8080/soap/services/convertpdfService?blob=swaRef"; 
            String username = "administrator"; 
            String password = "password"; 
             
            // Create the service Client objects needed 
            ConvertPdfServiceService convertPdfService = new ConvertPdfServiceService(); 
            ConvertPdfService convClient =  convertPdfService.getConvertpdfService(); 
 
            // Retrieve the Web services from the LiveCycle Server. 
            ((BindingProvider) convClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
            ((BindingProvider) convClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
            ((BindingProvider) convClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
                         
               // Create a BLOB that represents the input PDF file 
            String path = "C:\\Adobe\Loan.pdf"; 
            FileDataSource ds = new FileDataSource(path);  
            DataHandler handler = new DataHandler(ds); 
            BLOB inDoc = new BLOB(); 
            inDoc.setSwaRef(handler); 
 
               //Set up the runtime options for the new JPEG files to create 
               ToImageOptionsSpec spec = new ToImageOptionsSpec(); 
               spec.setImageConvertFormat(ImageConvertFormat.JPEG); 
               spec.setGrayScaleCompression(GrayScaleCompression.LOW); 
               spec.setColorCompression(ColorCompression.LOW); 
               spec.setFormat(JPEGFormat.BASELINE_OPTIMIZED); 
               spec.setRgbPolicy(RGBPolicy.OFF); 
               spec.setCmykPolicy(CMYKPolicy.OFF); 
               spec.setColorSpace(ColorSpace.RGB); 
               spec.setResolution("72"); 
               spec.setMonochrome(MonochromeCompression.NONE); 
               spec.setFilter(PNGFilter.SUB); 
               spec.setInterlace(Interlace.ADAM_7); 
               spec.setTileSize(180); 
               spec.setGrayScalePolicy(GrayScalePolicy.OFF); 
 
               //Send the conversion request to the ConvertPDF Service 
               MyArrayOfBLOB allImages = convClient.toImage2(inDoc, spec); 
 
               //Get the List of BLOBs and create iterator 
               List<BLOB> imageList = allImages.getItem(); 
               Iterator<BLOB> bit = imageList.iterator(); 
 
               //Get all images from the Object array 
               int i = 0; 
               while(bit.hasNext()) 
               { 
                   BLOB imageData = bit.next(); 
 
                   //Create a new file that represents the image 
                File f = new File("C:\\Adobe\tempFile" + i + ".jpg"); 
                BLOB outBlob = imageData; 
                DataHandler handler2 = outBlob.getSwaRef(); 
                 
                //Get an InputStream from DataHandler and 
                //write to a file 
                InputStream inputStream = handler2.getInputStream(); 
                OutputStream out = new FileOutputStream(f); 
 
                //Iterate through the buffer 
                byte buf[] = new byte[1024]; 
                int len; 
                while ((len = inputStream.read(buf)) > 0) 
                    out.write(buf, 0, len); 
                out.close(); 
                inputStream.close(); 
 
               } 
               System.out.println("The PDF file was converted to a set of JPEG document(s)."); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
 
    } 
 
}