| Author |
Message |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 28/08/2010 00:51:26
|
pedrojrivera
Joined: 20/11/2008 00:00:00
Messages: 55
Offline
|
*** UPDATED Attached Source 10/08/2010 ***
***New Zip file with source.
This source was originally posted in the General Help Section but thought it was more appropriate for it to be under the IcePDF forum.
Since I have received several emails and requests concerning this program I have attached an updated version of the TIFConvert() class. I have been using it in production for a while and have made several changes to it.
Example usage:
Code:
// - Convert with defaults
// - dpi = 200
// - color = CLR_RGB
// - compression = COMPRESSION_LZW
// - compression quality = .25f
TIFConvertPDF.convert(pdf, tif);
Code:
int dpi = 200;
int color = TIFConvert.CLR_GRAYSCALE;
int compression = TIFConvert.COMPRESSION_LZW;
float quality = 1f;
TIFConvertPDF.convert(pdf, tif, dpi, color, compression, quality);
Code:
dpi = 200;
color = TIFConvert.CLR_BLACK_WHITE
compression = TIFConvert.COMPRESSION_CCITT_T_6
quality = 1f;
TIFConvertPDF.convert(pdf, tif, dpi, color, compression, quality);
Code:
dpi = 300;
color = TIFConvert. CLR_RGB
compression = TIFConvert.COMPRESSION_LZW
quality = 1f;
byte[] b = TIFConvertPDF.convert(pdf, dpi, color, compression, quality);
...
// save byte array to DB, etc.
Code:
dpi = 200;
color = TIFConvert. CLR_RGB
compression = TIFConvert.COMPRESSION_JPEG
quality = .2f;
byte[] b = TIFConvertPDF.convert(pdf, dpi, color, compression, quality);
...
// save byte array to DB, etc.
Quality compression is only useful if the compression mechanism supports it. JPEG compression supports this value. Valid values are .01f to 1f.
If you set conversion to grayScale or B&W, it will produce a smaller file. Also changing the DPI setting will affect quality and file size.
All I ask is that if you improve upon it share it with others.
You will need version 1.1 of the JAI jar file. Earlier versions will NOT work.
https://jai-imageio.dev.java.net/binary-builds.html
Enjoy,
Pedro
| Filename |
TiffConvert.java |
Download
|
| Description |
TIFFConvert.java with color and compression parms. |
| Filesize |
9 Kbytes
|
| Downloaded: |
735 time(s) |
| Filename |
TIFConvert.zip |
Download
|
| Description |
Reworked TIFConvert Utility with some added features. Fixed CCITT problem and JPEG quality compression. Also included fixed version of jai_imageio for jvm 5 and 6. |
| Filesize |
1572 Kbytes
|
| Downloaded: |
885 time(s) |
|
 |
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 07/09/2010 07:27:02
|
patrick.corless
Joined: 26/10/2004 00:00:00
Messages: 1097
Online
|
Thanks for the source code Pedro! I"ll see what I can about getting it into its own wiki page as well as into the bundle.
|
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 05/10/2010 15:26:29
|
pedrojrivera
Joined: 20/11/2008 00:00:00
Messages: 55
Offline
|
Patrick,
The TIFConvert.zip that I just posted should be the one to make it to the bundle, if possible. Also I wouldn't mind providing some working examples for the wiki if it would help.
Thanks,
Pedro
|
 |
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 21/11/2010 22:03:30
|
mohitbansal
Joined: 16/09/2010 18:54:55
Messages: 3
Offline
|
hi
we used following code to convert pdf to tiff:
mport org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.DataBuffer;
import java.awt.*;
import java.io.*;
public class MultiPageCapture {
public static final double PRINTER_RESOLUTION = 300.0;
public static final String COMPRESSION_TYPE = "CCITT T.6";
public static void main(String[] args) {
String filePath = "C:\\Support\\temp\\464157875170_1164832_002.pdf";
Document document = new Document();
try {
document.setFile(filePath);
} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);
} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);
} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);
} catch (IOException ex) {
System.out.println("Error handling PDF document " + ex);
}
try {
File file = new File("C:\\Support\\temp\\464157875170_1164832_002.tif");
System.out.println("Output file location " + file);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(ios);
for (int i = 0; i < document.getNumberOfPages(); i++)
{
final double targetDPI = PRINTER_RESOLUTION;
float scale = 1.0f;
float rotation = 0f;
PDimension size = document.getPageDimension(i, rotation, scale);
double dpi = Math.sqrt((size.getWidth()*size.getWidth()) + (size.getHeight()*size.getHeight()) ) /Math.sqrt((8.5*8.5)+(11*11));
if (dpi < (targetDPI-0.1))
{
scale = (float) (targetDPI / dpi);
size = document.getPageDimension(i, rotation, scale);
}
int pageWidth = (int) size.getWidth();
int pageHeight = (int) size.getHeight();
int[] cmap = new int[] { 0xFF000000, 0xFFFFFFFF };
IndexColorModel cm = new IndexColorModel(1, cmap.length, cmap, 0, false, Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_BYTE_BINARY, cm);
Graphics g = image.createGraphics();
document.paintPage(i, g, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX,rotation, scale);
g.dispose();
IIOImage img = new IIOImage(image, null, null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType(COMPRESSION_TYPE);
if (i == 0)
{
writer.write(null, img, param);
}
else
{
writer.writeInsert(-1, img, param);
}
image.flush();
}
ios.flush();
ios.close();
writer.dispose();
System.out.println("PDF converted to Tiff successfully with compression type " + COMPRESSION_TYPE);
}
catch(IOException e) {
System.out.println("Error saving file " + e);
e.printStackTrace();
}
document.dispose();
}
Issue:
The code works fine and generates a good quality tiff. But the problem is it produces tiffs of same size irrespective of pdf size.
For a pdf of 100kb and 500kb, it creates tiff of same size of around 5mb. Also it consumes lot of server memory.
One more thing is we found some icepdf temp files under c:/windows/temp folder. Each file was about 2mb each and consumed 3gb space in total.
Looking forward to quick reply from you as its impacting our production environment.
Regards,
Mohit
|
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 22/11/2010 05:52:56
|
pedrojrivera
Joined: 20/11/2008 00:00:00
Messages: 55
Offline
|
Download the source code that is at the top of this page (TIFConvert.zip)
http://www.icefaces.org/JForum/posts/downloadAttach/3504.page
and just do the following:
Code:
String pdf = "c:\\mydpf.pdf";
String tif = c:\\mytif.tif";
int dpi = 200;
int color = TIFConvert.CLR_BLACK_WHITE
int compression = TIFConvert.COMPRESSION_CCITT_T_6
float quality = 1f;
TIFConvertPDF.convert(pdf, tif, dpi, color, compression, quality);
Change the dpi to a higher or lower setting to control quality of image. The lower the quality the smaller the file size.
|
 |
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 22/11/2010 06:02:49
|
mohitbansal
Joined: 16/09/2010 18:54:55
Messages: 3
Offline
|
Hi Pedro,
Thanks for your quick reply.
I had tried what you have suggested. But the problem is things works fine on my development system and even on test environment. The tiff file size is more or less similar to pdf file size.
But something strange happening on production box where same code generates much bigger tiff files all of same size irrespective of size of pdf.
So a 50kb and 500kb pdf results into same 5000kb tiff image.
Plus the icepdf temp files under c:/windows/temp are accumulating. Can you please advice any possible cause for that.
Any help will be much appreciated. If anyway its possible, can you please share your contact number so that this can be resolved quickly (mohitbansal6@gmail.com)
Regards,
Mohit
|
|
|
 |
![[Post New]](/JForum/templates/default/images/icon_minipost_new.gif) 22/04/2011 10:13:30
|
phamtn8
Joined: 22/04/2011 09:01:27
Messages: 2
Offline
|
Hi Perdro
It is my first time using IcePDF, so please excuse the ignorance of my question. So when I download TIFFConvert.zip, i see some .java file and jai_imageio_5.jar and jai_imageio_6.jar. I look at the source code of the java file, from all the import, I know I need to include IcePDF library in my classpath. But what do I do what the source code .java file. Does those act like my Utility/Helper class? Do I need to include jai_imageio_5.jar and jai_imageio_6.jar in my classpath as well?
|
|
|
 |
|
|
|
|