I am trying to optimize PDF file size generated by our Java based system. My first thought is to reduce the size of images in them by converting them to gray scale. Do you know any other tool, preferably a java library or component, which can be used for PDF documents to shrink the size?
问题:
回答1:
If your input images have larger dimensions I would suggest that you shrink them before adding them to the pdf file. In pure Java you can do it wish something like
Image image; // Say you have loaded the larger image
image = image.getScaledInstance(width, height, 0); // where width and height is something like 640x480
// Now save the image to some temporary file and use the shrunk version in your pdf file.
Be careful to maintain the aspect ratio. Check the ImageIO API found con Java 1.4.2+ to save the jpeg file.
回答2:
You can use setFullCompression on PdfStamper to deflate the PDF.
回答3:
Acrobat Pro 9 & X both have a "space audit" feature that will tell you which parts of your PDF are taking up the most space so you don't waste your time fixing things that aren't broken.
For example, if you're not careful you can accidentally include several copies of a given font or image. A static header and footer could be placed in a single PdfTemplate that is reused on the appropriate pages.
PdfSmartCopy will merge identical streams when importing various pages. It will not merged font subsets. I don't know of any PDF software outside Adobe's that does.
回答4:
Are the PDFs you are getting compressed? If not, you can use iText to read the PDF and write a compressed PDF.
回答5:
You can reduce the size of a PDF file by using a feature called "object stream". Object stream is not supported by iText, but you can postprocess the PDF generated using some tool like PDFLeo. See http://mdn.morovia.com/manuals/pdfleo-1/PDF-Optimization-Size-Reduction.php for more info.
If you reduce the image color depth, you might wan to do so before creating the PDF in iText. In some scenarios, using JPEG format or reducing the color depth to 256 colors will save a lot of space.
回答6:
If your images consist of vectors, that is, if you use Graphics2D to draw the image as the case is with e.g. JFreeChart graphs, you can add them as iText images, and they will be drawn as vectors in the PDF. This greatly reduces the size compared to binary images.
Furthermore you can either by reading and writing the pdf, or on time of generation scale the image using Java's ImageIO API.