Using Java Based Samples to Convert PDF to TIFF

PDF to TIFF file conversion is often accomplished by using a printer driver. If you are an individual user only converting a few files, this method is sufficient. However, if you have multiple users dealing with a large number of documents and images across your organization, using a tool to convert your images is a more efficient method. Snowbound Software provides tools that convert PDF to TIFF files directly and rapidly without having to use a printer driver.
Snowbound Software’s RasterMaster Java Imaging SDK provides samples to allow you to automatically convert from the PDF to TIFF file format. The simpleconvertionDS and converter samples appear below and can be found in the Samples folder included with RasterMaster for the Java TM Platform:

Simpleconvertionds Sample

package simpleconvertionds;

/**

* Title: Simple Conversion Sample

* Description: Takes the path to an image you want to convert and the

* Format Name or Integer Value of the type of file format you want to output.

* It then saves the image to the current directory as "out.sbd".

* Copyright: Copyright (c) 2005

* Company: Snowbound Software, Inc.

* @author Martin Peloquin

* @version 1.0

*/

import java.awt.*;

import java.io.*;

import java.lang.Throwable;

import Snow.*;

public class ds

{

ConsoleDisplay display = null;

Snowbnd simage = null;

File inputfile = null;

File outputfile = null;

int saveformatvalue = 0;

//***********************************************

//**CONSTRUCTOR**********************************

//***********************************************

//Constructor: Determines if the arguments passed are valid or not then passes

// those arguments to the proper run method

public ds(String pargs[])

{

//Initialize display console

display = new ConsoleDisplay();

try{

//Check for valid number of arguments

if (pargs.length != 0) {

//Check if it is a valid input file

setInputFile(pargs[0]);

if (pargs.length >= 2) {

setSaveFormat(pargs[1]);

}

//Initialize Output File Name

outputfile = new File(inputfile.getName()+".sbd");

//Initialize Snowbound Image Object and Run Conversion

simage = new Snowbnd();

run();

}

else //Display No Arguments

display.addText("ERROR: Incorrect number of arguments -- Optional Output Format Name or Integer Value>");

}catch(Throwable t){

//Display All Thrown Error Messages

display.addText(t.getMessage());

}

}

//***********************************************

//**MAIN ACTION METHODS: RUN**********************************************

//***********************************************

private void run() throws Throwable

{

//Decompress Image

int pg = 0;

int stat = simage.IMG_decompress_bitmap(inputfile.toString(),pg);

if (stat < 0)

throw (new Throwable("ERROR: Could not decompress Image "+inputfile.toString()));

//Display Decompressed image info

display.addText(" OK: *******Decompress Successful*******");

display.addText(" OK: Input File Name -- " + inputfile.toString());

display.addText(" OK: Input File Size -- " + inputfile.length());

stat = simage.IMGLOW_get_filetype(inputfile.toString());

if (stat >= 0){

display.addText(" OK: Input File Type -- " + FormatHash.getFormatName(stat));

display.addText(" OK: Input File Type Value -- " + stat);

}

else

display.addText("ERROR: Could not retrieve input file File Type: Error Code -- " + stat);

display.addText(" OK: Input Number of Pages -- " + simage.IMGLOW_get_pages(inputfile.toString()));

display.addText(" OK: Input Page Decompressed -- " + pg);

display.addText(" OK: Width -- " + simage.getWidth());

display.addText(" OK: Height -- " + simage.getHeight());

display.addText(" OK: XDpi -- " + simage.getXdpi());

display.addText(" OK: YDpi -- " + simage.getXdpi());

//Save Image

stat = simage.IMG_save_bitmap(outputfile.toString(),saveformatvalue);

if (stat < 0)

throw (new Throwable("ERROR: Could not Save Image -- Error Code: "+stat+" -- Format: "+saveformatvalue+" = "+FormatHash.getFormatName(saveformatvalue)));

//Display Saved Image Info

display.addText(" ");

display.addText(" OK: *******Save Successful*******");

display.addText(" OK: Output File Name -- "+outputfile.toString());

display.addText(" OK: Output File Size -- "+stat);

display.addText(" OK: Output File Type -- "+FormatHash.getFormatName(saveformatvalue));

display.addText(" OK: Output File Type Value -- "+saveformatvalue);

stat = simage.IMGLOW_get_pages(outputfile.toString());

if (stat >= 1){

display.addText(" OK: Output Number of Pages -- " + stat);

display.addText(" OK: Output Image Save To Page -- " + (stat + 1));

}

else

display.addText("ERROR: Could not retrieve number of pages of output file: Error Code -- " + stat);

display.addText(" OK: Width -- "+simage.getWidth());

display.addText(" OK: Height -- "+simage.getHeight());

display.addText(" OK: XDpi -- "+simage.getXdpi());

display.addText(" OK: YDpi -- "+simage.getXdpi());

}

//***********************************************

//**SUPPORT METHODS******************************
//***********************************************

private void setInputFile(String infile) throws Throwable

{

inputfile = new File(infile);

if (!inputfile.isFile())

throw (new Throwable("ERROR: Invalid First Argumemt -- Need Image File Name"));

}

private void setSaveFormat(String sformat) throws Throwable

{

//Check if format is valid

try {

saveformatvalue = Integer.parseInt(sformat);

if (!FormatHash.isValidFormat(saveformatvalue))

throw (new Throwable("ERROR: Invalid Second Argument -- Not a Recognized Snowbound Format Integer"));

}catch (java.lang.NumberFormatException nfe2) {

if (!FormatHash.isValidFormat(sformat.toUpperCase()))

throw (new Throwable("ERROR: Invalid Second Argument -- Not a Recognized Snowbound Format Name"));

saveformatvalue = FormatHash.getFormat(sformat.toUpperCase());

}

}

//***********************************************

//**START POINT**********************************

//***********************************************

public static void main(String[] args)

{

ds ds1 = new ds(args);

}

}

Converter Sample

package converter;

import java.io.File;

import Snow.*;

public class Convert

{

public File f=null;

private String[] inputFiles=null;

private String loadDirectory=null;

private String saveDirectory=null;

private int outputFileType;

private String extension;

private Snowbnd Simage=null;

private boolean createThumbs=false;

public Convert(String loadDir, String saveDir, int fileType, String outputExtension)

{

extension=outputExtension;

Simage= new Snowbnd();

setSaveDirectory(saveDir);

setLoadDirectory(loadDir);

setOutputFileType(fileType);

f = new File(loadDir);

inputFiles = f.list();

}

public void setSaveDirectory(String save)

{

saveDirectory = save;

}

public void setLoadDirectory(String load)

{

loadDirectory = load;

}

public void setOutputFileType(int ftype)

{

outputFileType = ftype;

}

public String getSaveDirectory()

{

return saveDirectory;

}

public String getLoadDirectory()

{

return loadDirectory;

}

public int getOutputFileType()

{

return outputFileType;

}

public void setJPEGparameters(int quality, int horizontalInterleave, int verticalInterleave)

{

Simage.IMGLOW_set_comp_quality(quality);

Simage.IMGLOW_set_jpg_interleave(horizontalInterleave,
verticalInterleave);

}

public void setThumbnailCreation(boolean create)

{

createThumbs = create;

}

private void makeThumbnail(String outName, int outType)

{

int bits = Simage.getBitsPerPixel();

int horiz = 120;

//calculate the vertical height to maintain aspect ratio of thumbnail

int vert = (int)(((double)horiz / (double)Simage.getWidth()) * (double)Simage.getHeight());

if (bits == 1)

Simage.IMG_resize_to_gray(horiz,vert);

else

Simage.IMG_resize_bitmap_bicubic(horiz,vert);

Simage.IMG_save_bitmap(outName,outType);

}

public void performConversion()

{

int status;

String inputFileName = null;

String outputFileName = null;

String baseFileName = null;

int extnLocation;

//Loop through each file in the directory

for (int i=0; i < inputFiles.length; i++)

{

//Gets the location of the . so we can pull off the extension

//if there is no extension sets it to the end of the file.

if (inputFiles[i].indexOf(".") < 1)

extnLocation = inputFiles[i].length();

else

extnLocation = inputFiles[i].indexOf(".");

//extract The Base from the fileExtension

baseFileName = inputFiles[i].substring(0,extnLocation);

//create File String

inputFileName = loadDirectory + System.getProperty("file.separator") + inputFiles[i];

//decompress image

status = Simage.IMG_decompress_bitmap(inputFileName,0);

//only go forward if the image can be properly decompressed

//otherwise just move on to the next image.

if (status >= 0)

{

//create outputFile String

outputFileName = saveDirectory + System.getProperty("file.separator") + baseFileName + extension;

//save new image

if (createThumbs == false)

Simage.IMG_save_bitmap(outputFileName,outputFileType);

else

makeThumbnail(outputFileName,outputFileType);

}

} //end for loop

} //end perform conversion

} //end convert class

Additional Resources
Learn more about Snowbound's solutions for image conversion and batch file conversion.

Category:

Connect With Us
Free Trial  Demo
Phone +1-617-607-2010
Email  Email
Start Chat

Support Login  Support Login
Newsletter Signup  Newsletter Signup

Online Demo