10/31/2023 0 Comments Java image size reduction![]() Stream.put(PdfName.HEIGHT, new PdfNumber(height)) stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)) Stream.put(PdfName.WIDTH, new PdfNumber(width)) Stream.put(PdfName.FILTER, PdfName.DCTDECODE) Stream.put(PdfName.SUBTYPE, PdfName.IMAGE) Stream.put(PdfName.TYPE, PdfName.XOBJECT) ![]() ImgBytes.toByteArray(), false, PRStream.NO_COMPRESSION) = AffineTransform.getScaleInstance(FACTOR, FACTOR) īyteArrayOutputStream imgBytes = new ByteArrayOutputStream() = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) Int height = (int)(bi.getHeight() * FACTOR) Int width = (int)(bi.getWidth() * FACTOR) PdfImageObject image = new PdfImageObject(stream) īufferedImage bi = image.getBufferedImage() PdfReader reader = new PdfReader(SpecialId.RESULT) Listing 1 ResizeImage.java PdfName key = new PdfName("ITXT_SpecialId") With the method getXrefSize() we get the highest object number in the PDF document and we loop over every object, searching for a stream that has the special id we’re looking for. Listing 1 uses brute force instead of the PdfReaderContentParser to find images. It’s more likely that the PDF contains high-resolution images, in which case the original question should be rephrased into, “How do I reduce the resolution of the images inside my PDF?” To achieve this, we should extract the image from the PDF, downsample it, and then put it back into the PDF, replacing the high-resolution image. You could try and see if the PdfReader method removeUnusedObjects() yields any results. Maybe the PDF wasn’t compressed or maybe there are plenty of unused objects. Maybe the same image is added multiple times, in which case passing the PDF through PdfSmartCopy could already result in a serious file size reduction. Here’s a question that is often posted to the mailing list: “How do we reduce the size of an existing PDF containing lots of images?” There are many different answers to this question, depending on the nature of the PDF file. Resizing an Image in an Existing Document Introduction When mobile formats become available all customers will be contacted and upgraded. All pBook purchases include free PDF, mobi and epub. ![]() Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. It is being reproduced here by permission from Manning Publications. This article is based on iText in Action, Second Edition, published on October, 2010.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |