Skip to main content
Skip table of contents

How to detect if PDF has compressed xref table in Java?

Is there an API available in iText to detect whether the PDF file has a compressed xref table?

The PdfReader class of this library has some useful API with regards to xref, but none serve my purpose.

The requirement is to:

  1. Check if the PDF has xref compressed table.
  2. If 1 is true -> then "Uncompress" xref table.
  3. Send the byte stream for further processing.
  4. Once the processing completes "Compress" back the xref table to its original form

Any pointers in this regard would be appreciated.

Posted on StackOverflow on Jan 15, 2013 by Bond - Java Bond

As it turns out, this is already supported in iText. You need to create a PdfReader instance and then use isNewXrefType().

To uncompress the XRef table of a PDF document, you can use this method:

public void uncompressXRef(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

To recompress the XRef table, use this method:

public void recompressXRef(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.getWriter().setFullCompression();
    stamper.close();
    reader.close();
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.