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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to get the background from multiple images by
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.
You can use setFullCompression on PdfStamper to deflate the PDF.
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.
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.
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
Be careful to maintain the aspect ratio. Check the ImageIO API found con Java 1.4.2+ to save the jpeg file.
Are the PDFs you are getting compressed? If not, you can use iText to read the PDF and write a compressed PDF.