Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:32:30+00:00 2026-05-27T17:32:30+00:00

I’m trying to solve a problem using java, iText, and the Java advanced imaging

  • 0

I’m trying to solve a problem using java, iText, and the Java advanced imaging library. My software system uses ghostscript to create jpg thumbnail images etc… from PDF files. However on CentOS 5.x the highest version of ghostscript is 8.7 which has a known issue of not being able to handle PDF files containing JPEG 2000 images in them. My plan is to scan the file first and see if it contains jpeg2000 images (I’ve already got this part figured out); if so, then use iText and the Java Advanced Imaging library (contains the jpeg2000 read & write codecs) to convert the contained jpeg2000 files into regular jpeg files & then pass the new PDF file to ghostscript. The code below attempts this, but results in another file containing jpeg2000 files. Any help with this would be much appreciated.

public class ImageReplacer{
    public static void main(String [] args){
        try{
            String RESULT = "";
            PdfReader reader = new PdfReader("pdf_containing_jpeg2000_images.pdf");
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            MyImageRenderListener listener = new MyImageRenderListener(RESULT);
            MyImageConverterListener clistener = new MyImageConverterListener(RESULT);
            clistener.setReader(reader);
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                parser.processContent(i, clistener);
            }   
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("out.pdf"));
            stamper.close();            
        }catch(Exception e){
            e.printStackTrace();    
        }
    }
}
class MyImageConverterListener implements RenderListener {
    protected String path = "";
    protected PdfReader reader;
    public MyImageConverterListener(String path) {
        this.path = path;
    }
    public void beginTextBlock() { }
    public void endTextBlock() { }
    public void renderImage(ImageRenderInfo renderInfo) {
        try {
            PdfImageObject image = renderInfo.getImage();
            PdfName filter = (PdfName)image.get(PdfName.FILTER);
            if (PdfName.JPXDECODE.equals(filter)) {
                if(image.getDictionary().isStream()){
                    BufferedImage bi = image.getBufferedImage();
                    if (bi == null) return; 
                    int width = (int)bi.getWidth();
                    int height = (int)bi.getHeight();
                    ByteArrayOutputStream imgBytes = new ByteArrayOutputStream(); 
                    ImageIO.write(bi, "JPG", imgBytes);
                    PRStream stream = new PRStream(reader,imgBytes.toByteArray());
                    stream.clear();
                    stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);
                    stream.put(PdfName.TYPE, PdfName.XOBJECT);
                    stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
                    stream.put(new PdfName("foo"+Math.random()), new PdfName("bar"+Math.random()));
                    stream.put(PdfName.FILTER, PdfName.DCTDECODE);
                    stream.put(PdfName.WIDTH, new PdfNumber(width));
                    stream.put(PdfName.HEIGHT, new PdfNumber(height));
                    stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
                    stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void renderText(TextRenderInfo renderInfo) { }
    public void setReader(PdfReader r){
        reader = r;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:32:30+00:00Added an answer on May 27, 2026 at 5:32 pm

    So I managed to solve this one on my own (with a little help from iText in action by Bruno Lowagie – great book). Just to re-iterate, my intention is to scan a PDF using iText to see if it contains any JPEG2000 images and if it does output the same PDF but with the inner JPEG2000 images replaced with regular JPEG images. This solves the fatal ghostscript 8.7 ‘Unable to process JPXDecode data’ error, but would also provide useful for making PDF’s iOS compatible.

    So without further a do kids; here goes…

    Step 1) Download iText 5.x .jar file, and download jai_imageio-1.1.jar (the Java advanced imaging library that allows you to convert JPEG2000 files)

    Step 2) Create a file called PDFConverter.java and put this code in it:

    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfName;
    import com.itextpdf.text.pdf.PdfObject;
    import com.itextpdf.text.pdf.PRStream;
    import com.itextpdf.text.pdf.parser.PdfImageObject;
    import com.itextpdf.text.pdf.PdfNumber;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import com.itextpdf.text.pdf.PdfStamper;
    import java.io.*;
    
    public class PDFConverter{
        public static void main(String [] args){
            if(args.length==1){
                if(hasJpeg2000(args[0])){
                    System.out.println("Contains JPEG2000 images: Converting them to JPEG..."); 
                    convertPDF(args[0]);
                    System.out.println("Done...");
                }else{
                    System.out.println("Doesn't contain any JPEG2000 images: Nothing to be done...");   
                }
            }else{
                System.out.println("Please specify a PDF filename as a command line argument!");    
            }
        }
        public static boolean hasJpeg2000(String s){
            try{
                PdfReader reader = new PdfReader(s); 
                int n = reader.getXrefSize();
                PdfObject object; 
                PRStream stream; 
                for (int i = 0; i < n; i++) {
                    object = reader.getPdfObject(i); 
                    if (object == null || !object.isStream())continue; 
                    stream = (PRStream)object; 
                    PdfImageObject image = new PdfImageObject(stream);
                    PdfName filter = (PdfName)image.get(PdfName.FILTER);
                    if (PdfName.JPXDECODE.equals(filter)) {
                        return true;
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
        }
        public static void convertPDF(String s){
            try{
                PdfReader reader = new PdfReader(s); 
                int n = reader.getXrefSize();
                PdfObject object; 
                PRStream stream; 
                for (int i = 0; i < n; i++) {
                    object = reader.getPdfObject(i); 
                    if (object == null || !object.isStream())continue; 
                    stream = (PRStream)object; 
                    PdfImageObject image = new PdfImageObject(stream);
                    PdfName filter = (PdfName)image.get(PdfName.FILTER);
                    if (PdfName.JPXDECODE.equals(filter)) {
                        BufferedImage bi = image.getBufferedImage(); 
                        if (bi == null) continue; 
                        int width = (int)(bi.getWidth());
                        int height = (int)(bi.getHeight());
                        ByteArrayOutputStream imgBytes = new ByteArrayOutputStream(); 
                        ImageIO.write(bi, "JPG", imgBytes); 
                        stream.clear(); 
                        stream.setData(imgBytes.toByteArray(),false, PRStream.NO_COMPRESSION); 
                        stream.put(PdfName.TYPE, PdfName.XOBJECT); 
                        stream.put(PdfName.SUBTYPE, PdfName.IMAGE); 
                        stream.put(new PdfName("foo"+Math.random()), new PdfName("bar"+Math.random())); 
                        stream.put(PdfName.FILTER, PdfName.DCTDECODE); 
                        stream.put(PdfName.WIDTH, new PdfNumber(width)); 
                        stream.put(PdfName.HEIGHT, new PdfNumber(height)); 
                        stream.put(PdfName.BITSPERCOMPONENT,new PdfNumber(8)); 
                        stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
                    }
                }
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("out.pdf")); stamper.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    } 
    

    Step 3)Compile the above file in the following manner:

    javac -cp .:iText-5.0.4.jar:jai_imageio-1.1.jar PDFConverter.java

    Step 4)Run the program with a PDF…

    java -cp .:iText-5.0.4.jar:jai_imageio-1.1.jar PDFConverter PDFFileName.pdf

    Booyah…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.