Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class com.lowagie.text.pdf.PdfDictionary cannot be cast to class com.lowagie.text.pdf.PRIndirectReference #668

Closed
draccagni opened this issue Feb 21, 2022 · 1 comment · Fixed by #673
Labels
Milestone

Comments

@draccagni
Copy link

draccagni commented Feb 21, 2022

Hello,
reading a pdf file the library rises the following exception:

class com.lowagie.text.pdf.PdfDictionary cannot be cast to class com.lowagie.text.pdf.PRIndirectReference

I temporary solved the issue by adding to the PdfReader class the method:

private void iteratePages(PRIndirectReference rpage) {
  PdfDictionary page = (PdfDictionary) getPdfObject(rpage);
  PdfArray kidsPR = page.getAsArray(PdfName.KIDS);
  // reference to a leaf
  if (kidsPR == null) {
    page.put(PdfName.TYPE, PdfName.PAGE);
    PdfDictionary dic = pageInh.get(pageInh.size() - 1);
    PdfName key;
    for (Object o : dic.getKeys()) {
      key = (PdfName) o;
      if (page.get(key) == null)
        page.put(key, dic.get(key));
    }
    if (page.get(PdfName.MEDIABOX) == null) {
      PdfArray arr = new PdfArray(new float[] { 0, 0,
              PageSize.LETTER.getRight(), PageSize.LETTER.getTop() });
      page.put(PdfName.MEDIABOX, arr);
    }
    refsn.add(rpage);
  }
  // reference to a branch
  else {
    page.put(PdfName.TYPE, PdfName.PAGES);
    pushPageAttributes(page);
    for (int k = 0; k < kidsPR.size(); ++k) {
      PdfObject obj = kidsPR.getPdfObject(k);
      if (!obj.isIndirect()) {
        while (k < kidsPR.size())
          kidsPR.remove(k);
        break;
      }
      if (obj instanceof PRIndirectReference)
        iteratePages((PRIndirectReference) obj);
      else if (obj instanceof PdfDictionary)
        iteratePages((PdfDictionary) obj);
    }
    popPageAttributes();
  }
}

private void iteratePages(PdfDictionary page) {
  PdfArray kidsPR = page.getAsArray(PdfName.KIDS);
  // reference to a leaf
  if (kidsPR != null) {
    page.put(PdfName.TYPE, PdfName.PAGES);
    pushPageAttributes(page);
    for (int k = 0; k < kidsPR.size(); ++k) {
      PdfObject obj = kidsPR.getPdfObject(k);
      if (!obj.isIndirect()) {
        while (k < kidsPR.size())
        kidsPR.remove(k);
        break;
      }
      if (obj instanceof PRIndirectReference)
        iteratePages((PRIndirectReference) obj);
      else if (obj instanceof PdfDictionary)
        iteratePages((PdfDictionary) obj);
    }
    popPageAttributes();
  }
}

by invoking it in the method:

void readPages() {
  if (refsn != null)
    return;
  refsp = null;
  refsn = new ArrayList<>();
  pageInh = new ArrayList<>();
  PdfObject obj = reader.catalog.get(PdfName.PAGES);
  if (obj instanceof PRIndirectReference)
    iteratePages((PRIndirectReference) obj);
  else if (obj instanceof PdfDictionary)
    iteratePages((PdfDictionary) obj);
  pageInh = null;
  reader.rootPages.put(PdfName.COUNT, new PdfNumber(refsn.size()));
}

I hope it is helpful. Could the final fix be included in one of the next releases?

Thanks in advance
PdfReader.java.txt

@draccagni draccagni added the bug label Feb 21, 2022
draccagni pushed a commit to draccagni/OpenPDF that referenced this issue Feb 22, 2022
draccagni pushed a commit to draccagni/OpenPDF that referenced this issue Feb 22, 2022
@andreasrosdal
Copy link
Contributor

Pull requests welcome!

draccagni pushed a commit to draccagni/OpenPDF that referenced this issue Feb 25, 2022
draccagni pushed a commit to draccagni/OpenPDF that referenced this issue Feb 25, 2022
@asturio asturio linked a pull request Mar 5, 2022 that will close this issue
asturio pushed a commit that referenced this issue Mar 5, 2022
@asturio asturio added this to the 1.3.28 milestone May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants