Exemplo n.º 1
0
 public void expandNode(PdfObjectTreeNode node) {
   if (node.getChildCount() > 0) {
     return;
   }
   PdfObject object = node.getPdfObject();
   PdfObjectTreeNode leaf;
   switch (object.type()) {
     case PdfObject.INDIRECT:
       PdfIndirectReference ref = (PdfIndirectReference) object;
       leaf = getNode(ref.getNumber());
       addNodes(node, leaf);
       if (leaf instanceof PdfPagesTreeNode) expandNode(leaf);
       return;
     case PdfObject.ARRAY:
       PdfArray array = (PdfArray) object;
       for (Iterator<PdfObject> it = array.listIterator(); it.hasNext(); ) {
         leaf = PdfObjectTreeNode.getInstance(it.next());
         addNodes(node, leaf);
         expandNode(leaf);
       }
       return;
     case PdfObject.DICTIONARY:
     case PdfObject.STREAM:
       PdfDictionary dict = (PdfDictionary) object;
       for (Iterator<PdfName> it = dict.getKeys().iterator(); it.hasNext(); ) {
         leaf = PdfObjectTreeNode.getInstance(dict, it.next());
         addNodes(node, leaf);
         expandNode(leaf);
       }
       return;
   }
 }
 public void invoke(
     PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
   PdfArray array = (PdfArray) operands.get(0);
   ArrayList<PdfObject> entries = array.getArrayList();
   float tj = 0;
   for (PdfObject entryObj : entries) {
     if (entryObj instanceof PdfString) {
       processor.displayPdfString((PdfString) entryObj, tj);
       tj = 0;
     } else {
       tj = ((PdfNumber) entryObj).floatValue();
     }
   }
 }
Exemplo n.º 3
0
 /** @see javax.swing.table.TableModel#getValueAt(int, int) */
 public Object getValueAt(int rowIndex, int columnIndex) {
   switch (columnIndex) {
     case 0:
       return array.getPdfObject(rowIndex);
     default:
       return null;
   }
 }
Exemplo n.º 4
0
  /** @see com.lowagie.toolbox.AbstractTool#execute() */
  public void execute() {
    try {
      if (getValue("srcfile") == null)
        throw new InstantiationException("You need to choose a sourcefile");
      File src = (File) getValue("srcfile");

      // we create a reader for a certain document
      PdfReader reader = new PdfReader(src.getAbsolutePath());
      final File parentFile = src.getParentFile();
      final String outPath;
      if (parentFile != null) {
        outPath = parentFile.getAbsolutePath();
      } else {
        outPath = "";
      }
      PdfDictionary catalog = reader.getCatalog();
      PdfDictionary names = catalog.getAsDict(PdfName.NAMES);
      if (names != null) {
        PdfDictionary embFiles = names.getAsDict(new PdfName("EmbeddedFiles"));
        if (embFiles != null) {
          HashMap<String, PdfObject> embMap = PdfNameTree.readTree(embFiles);
          for (Iterator<PdfObject> i = embMap.values().iterator(); i.hasNext(); ) {
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(i.next());
            unpackFile(reader, filespec, outPath);
          }
        }
      }
      for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = reader.getPageN(k).getAsArray(PdfName.ANNOTS);
        if (annots == null) continue;
        for (Iterator<PdfObject> i = annots.listIterator(); i.hasNext(); ) {
          PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(i.next());
          PdfName subType = annot.getAsName(PdfName.SUBTYPE);
          if (!PdfName.FILEATTACHMENT.equals(subType)) continue;
          PdfDictionary filespec = annot.getAsDict(PdfName.FS);
          unpackFile(reader, filespec, outPath);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 5
0
  /** extracts attachments from PDF File */
  @SuppressWarnings("unchecked")
  protected Map extractAttachments(PdfReader reader) throws IOException {
    Map fileMap = new HashMap();
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
      PdfDictionary embFiles =
          (PdfDictionary) PdfReader.getPdfObject(names.get(new PdfName("EmbeddedFiles")));
      if (embFiles != null) {
        HashMap embMap = PdfNameTree.readTree(embFiles);
        for (Iterator i = embMap.values().iterator(); i.hasNext(); ) {
          PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
          Object fileInfo[] = unpackFile(reader, filespec);
          if (fileMap.containsKey(fileInfo[0])) {
            throw new RuntimeException(DUPLICATE_FILE_NAMES);
          }
          fileMap.put(fileInfo[0], fileInfo[1]);
        }
      }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
      PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
      if (annots == null) {
        continue;
      }
      for (Iterator i = annots.getArrayList().listIterator(); i.hasNext(); ) {
        PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
        PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
        if (!PdfName.FILEATTACHMENT.equals(subType)) {
          continue;
        }
        PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
        Object fileInfo[] = unpackFile(reader, filespec);
        if (fileMap.containsKey(fileInfo[0])) {
          throw new RuntimeException(DUPLICATE_FILE_NAMES);
        }

        fileMap.put(fileInfo[0], fileInfo[1]);
      }
    }

    return fileMap;
  }
    public void invoke(
        PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
      PdfName dictionaryName = (PdfName) operands.get(0);
      PdfDictionary extGState = processor.resources.getAsDict(PdfName.EXTGSTATE);
      if (extGState == null)
        throw new IllegalArgumentException(
            "Resources do not contain ExtGState entry. Unable to process operator " + operator);
      PdfDictionary gsDic = extGState.getAsDict(dictionaryName);
      if (gsDic == null)
        throw new IllegalArgumentException(
            dictionaryName + " is an unknown graphics state dictionary");

      PdfArray fontParameter = gsDic.getAsArray(PdfName.FONT);
      if (fontParameter != null) {
        CMapAwareDocumentFont font =
            new CMapAwareDocumentFont((PRIndirectReference) fontParameter.getPdfObject(0));
        float size = fontParameter.getAsNumber(1).floatValue();

        processor.gs().font = font;
        processor.gs().fontSize = size;
      }
    }
Exemplo n.º 7
0
 @SuppressWarnings("unchecked")
 public static byte[] extractAttachmentFiles(PdfReader reader, String suffix) throws IOException {
   PdfDictionary catalog = reader.getCatalog();
   PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
   if (names != null) {
     PdfDictionary embFiles =
         (PdfDictionary) PdfReader.getPdfObject(names.get(new PdfName("EmbeddedFiles")));
     if (embFiles != null) {
       HashMap<?, PdfObject> embMap = PdfNameTree.readTree(embFiles);
       for (Iterator<PdfObject> i = embMap.values().iterator(); i.hasNext(); ) {
         PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(i.next());
         byte[] ret = unpackFile(reader, filespec, suffix);
         if (ret != null) {
           return ret;
         }
       }
     }
   }
   for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
     PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
     if (annots == null) {
       continue;
     }
     for (Iterator<PdfObject> i = annots.listIterator(); i.hasNext(); ) {
       PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(i.next());
       PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
       if (!PdfName.FILEATTACHMENT.equals(subType)) {
         continue;
       }
       PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
       byte[] ret = unpackFile(reader, filespec, suffix);
       if (ret != null) {
         return ret;
       }
     }
   }
   return null;
 }
Exemplo n.º 8
0
 /** @see javax.swing.table.TableModel#getRowCount() */
 public int getRowCount() {
   return array.size();
 }
Exemplo n.º 9
0
 PdfObject getColorspace() {
   if (icc_profile != null) {
     if ((colorType & 2) == 0) return PdfName.DEVICEGRAY;
     else return PdfName.DEVICERGB;
   }
   if (gamma == 1f && !hasCHRM) {
     if ((colorType & 2) == 0) return PdfName.DEVICEGRAY;
     else return PdfName.DEVICERGB;
   } else {
     PdfArray array = new PdfArray();
     PdfDictionary dic = new PdfDictionary();
     if ((colorType & 2) == 0) {
       if (gamma == 1f) return PdfName.DEVICEGRAY;
       array.add(PdfName.CALGRAY);
       dic.put(PdfName.GAMMA, new PdfNumber(gamma));
       dic.put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]"));
       array.add(dic);
     } else {
       PdfObject wp = new PdfLiteral("[1 1 1]");
       array.add(PdfName.CALRGB);
       if (gamma != 1f) {
         PdfArray gm = new PdfArray();
         PdfNumber n = new PdfNumber(gamma);
         gm.add(n);
         gm.add(n);
         gm.add(n);
         dic.put(PdfName.GAMMA, gm);
       }
       if (hasCHRM) {
         float z = yW * ((xG - xB) * yR - (xR - xB) * yG + (xR - xG) * yB);
         float YA = yR * ((xG - xB) * yW - (xW - xB) * yG + (xW - xG) * yB) / z;
         float XA = YA * xR / yR;
         float ZA = YA * ((1 - xR) / yR - 1);
         float YB = -yG * ((xR - xB) * yW - (xW - xB) * yR + (xW - xR) * yB) / z;
         float XB = YB * xG / yG;
         float ZB = YB * ((1 - xG) / yG - 1);
         float YC = yB * ((xR - xG) * yW - (xW - xG) * yW + (xW - xR) * yG) / z;
         float XC = YC * xB / yB;
         float ZC = YC * ((1 - xB) / yB - 1);
         float XW = XA + XB + XC;
         float YW = 1; // YA+YB+YC;
         float ZW = ZA + ZB + ZC;
         PdfArray wpa = new PdfArray();
         wpa.add(new PdfNumber(XW));
         wpa.add(new PdfNumber(YW));
         wpa.add(new PdfNumber(ZW));
         wp = wpa;
         PdfArray matrix = new PdfArray();
         matrix.add(new PdfNumber(XA));
         matrix.add(new PdfNumber(YA));
         matrix.add(new PdfNumber(ZA));
         matrix.add(new PdfNumber(XB));
         matrix.add(new PdfNumber(YB));
         matrix.add(new PdfNumber(ZB));
         matrix.add(new PdfNumber(XC));
         matrix.add(new PdfNumber(YC));
         matrix.add(new PdfNumber(ZC));
         dic.put(PdfName.MATRIX, matrix);
       }
       dic.put(PdfName.WHITEPOINT, wp);
       array.add(dic);
     }
     return array;
   }
 }
Exemplo n.º 10
0
  void readPng() throws IOException {
    for (int i = 0; i < PNGID.length; i++) {
      if (PNGID[i] != is.read()) {
        throw new IOException("File is not a valid PNG.");
      }
    }
    byte buffer[] = new byte[TRANSFERSIZE];
    while (true) {
      int len = getInt(is);
      String marker = getString(is);
      if (len < 0 || !checkMarker(marker)) throw new IOException("Corrupted PNG file.");
      if (IDAT.equals(marker)) {
        int size;
        while (len != 0) {
          size = is.read(buffer, 0, Math.min(len, TRANSFERSIZE));
          if (size < 0) return;
          idat.write(buffer, 0, size);
          len -= size;
        }
      } else if (tRNS.equals(marker)) {
        switch (colorType) {
          case 0:
            if (len >= 2) {
              len -= 2;
              int gray = getWord(is);
              if (bitDepth == 16) transRedGray = gray;
              else additional.put(PdfName.MASK, new PdfLiteral("[" + gray + " " + gray + "]"));
            }
            break;
          case 2:
            if (len >= 6) {
              len -= 6;
              int red = getWord(is);
              int green = getWord(is);
              int blue = getWord(is);
              if (bitDepth == 16) {
                transRedGray = red;
                transGreen = green;
                transBlue = blue;
              } else
                additional.put(
                    PdfName.MASK,
                    new PdfLiteral(
                        "[" + red + " " + red + " " + green + " " + green + " " + blue + " " + blue
                            + "]"));
            }
            break;
          case 3:
            if (len > 0) {
              trans = new byte[len];
              for (int k = 0; k < len; ++k) trans[k] = (byte) is.read();
              len = 0;
            }
            break;
        }
        Utilities.skip(is, len);
      } else if (IHDR.equals(marker)) {
        width = getInt(is);
        height = getInt(is);

        bitDepth = is.read();
        colorType = is.read();
        compressionMethod = is.read();
        filterMethod = is.read();
        interlaceMethod = is.read();
      } else if (PLTE.equals(marker)) {
        if (colorType == 3) {
          PdfArray colorspace = new PdfArray();
          colorspace.add(PdfName.INDEXED);
          colorspace.add(getColorspace());
          colorspace.add(new PdfNumber(len / 3 - 1));
          ByteBuffer colortable = new ByteBuffer();
          while ((len--) > 0) {
            colortable.append_i(is.read());
          }
          colorspace.add(new PdfString(colorTable = colortable.toByteArray()));
          additional.put(PdfName.COLORSPACE, colorspace);
        } else {
          Utilities.skip(is, len);
        }
      } else if (pHYs.equals(marker)) {
        int dx = getInt(is);
        int dy = getInt(is);
        int unit = is.read();
        if (unit == 1) {
          dpiX = (int) ((float) dx * 0.0254f + 0.5f);
          dpiY = (int) ((float) dy * 0.0254f + 0.5f);
        } else {
          if (dy != 0) XYRatio = (float) dx / (float) dy;
        }
      } else if (cHRM.equals(marker)) {
        xW = (float) getInt(is) / 100000f;
        yW = (float) getInt(is) / 100000f;
        xR = (float) getInt(is) / 100000f;
        yR = (float) getInt(is) / 100000f;
        xG = (float) getInt(is) / 100000f;
        yG = (float) getInt(is) / 100000f;
        xB = (float) getInt(is) / 100000f;
        yB = (float) getInt(is) / 100000f;
        hasCHRM =
            !(Math.abs(xW) < 0.0001f
                || Math.abs(yW) < 0.0001f
                || Math.abs(xR) < 0.0001f
                || Math.abs(yR) < 0.0001f
                || Math.abs(xG) < 0.0001f
                || Math.abs(yG) < 0.0001f
                || Math.abs(xB) < 0.0001f
                || Math.abs(yB) < 0.0001f);
      } else if (sRGB.equals(marker)) {
        int ri = is.read();
        intent = intents[ri];
        gamma = 2.2f;
        xW = 0.3127f;
        yW = 0.329f;
        xR = 0.64f;
        yR = 0.33f;
        xG = 0.3f;
        yG = 0.6f;
        xB = 0.15f;
        yB = 0.06f;
        hasCHRM = true;
      } else if (gAMA.equals(marker)) {
        int gm = getInt(is);
        if (gm != 0) {
          gamma = 100000f / (float) gm;
          if (!hasCHRM) {
            xW = 0.3127f;
            yW = 0.329f;
            xR = 0.64f;
            yR = 0.33f;
            xG = 0.3f;
            yG = 0.6f;
            xB = 0.15f;
            yB = 0.06f;
            hasCHRM = true;
          }
        }
      } else if (iCCP.equals(marker)) {
        do {
          --len;
        } while (is.read() != 0);
        is.read();
        --len;
        byte icccom[] = new byte[len];
        int p = 0;
        while (len > 0) {
          int r = is.read(icccom, p, len);
          if (r < 0) throw new IOException("Premature end of file.");
          p += r;
          len -= r;
        }
        byte iccp[] = PdfReader.FlateDecode(icccom, true);
        icccom = null;
        //                try {
        //                    icc_profile = ICC_Profile.getInstance(iccp);
        //                }
        // catch (Exception e) {
        icc_profile = null;
        // }
      } else if (IEND.equals(marker)) {
        break;
      } else {
        Utilities.skip(is, len);
      }
      Utilities.skip(is, 4);
    }
  }