Esempio n. 1
0
  /**
   * merges two arrays.
   *
   * @param dest
   * @param source
   */
  private void mergeArray(COSName name, COSDictionary dest, COSDictionary source) {
    COSArray destDict = (COSArray) dest.getDictionaryObject(name);
    COSArray sourceDict = (COSArray) source.getDictionaryObject(name);

    if (destDict == null) {
      destDict = new COSArray();
      dest.setItem(name, destDict);
    }

    for (int sourceDictIdx = 0;
        sourceDict != null && sourceDictIdx < sourceDict.size();
        sourceDictIdx++) {
      COSBase key = sourceDict.get(sourceDictIdx);
      if (key instanceof COSName) {
        COSName keyname = (COSName) key;

        boolean bFound = false;
        for (int destDictIdx = 0; destDictIdx < destDict.size(); destDictIdx++) {
          COSBase destkey = destDict.get(destDictIdx);
          if (destkey instanceof COSName) {
            COSName destkeyname = (COSName) destkey;
            if (destkeyname.equals(keyname)) {
              bFound = true;
              break;
            }
          }
        }
        if (!bFound) {
          destDict.add(keyname);
        }
      }
    }
  }
Esempio n. 2
0
  private void processPages(List pages) throws IOException {
    Iterator pageIter = pages.iterator();
    while (pageIter.hasNext()) {
      PDPage page = (PDPage) pageIter.next();
      COSDictionary pageDictionary = page.getCOSDictionary();
      COSBase contents = pageDictionary.getDictionaryObject(COSName.CONTENTS);
      if (contents instanceof COSStream) {
        COSStream contentsStream = (COSStream) contents;
        // System.err.println("stream");

        COSArray array = new COSArray();

        array.add(contentsStream);

        mergePage(array, page);

        pageDictionary.setItem(COSName.CONTENTS, array);
      } else if (contents instanceof COSArray) {
        COSArray contentsArray = (COSArray) contents;

        mergePage(contentsArray, page);
      } else {
        throw new IOException("Contents are unknown type:" + contents.getClass().getName());
      }
      pageCount++;
    }
  }
 /**
  * Creates a new instance based on a given {@link COSDictionary}.
  *
  * @param dict the dictionary
  */
 public PDOptionalContentGroup(COSDictionary dict) {
   if (!dict.getItem(COSName.TYPE).equals(COSName.OCG)) {
     throw new IllegalArgumentException(
         "Provided dictionary is not of type '" + COSName.OCG + "'");
   }
   this.ocg = dict;
 }
Esempio n. 4
0
  /**
   * merges two dictionaries.
   *
   * @param dest
   * @param source
   */
  private void mergeDictionary(
      COSName name, COSDictionary dest, COSDictionary source, Map objectNameMap) {
    COSDictionary destDict = (COSDictionary) dest.getDictionaryObject(name);
    COSDictionary sourceDict = (COSDictionary) source.getDictionaryObject(name);

    if (destDict == null) {
      destDict = new COSDictionary();
      dest.setItem(name, destDict);
    }
    if (sourceDict != null) {

      for (Map.Entry<COSName, COSBase> entry : sourceDict.entrySet()) {
        COSName mappedKey = (COSName) objectNameMap.get(entry.getKey().getName());
        if (mappedKey != null) {
          destDict.setItem(mappedKey, entry.getValue());
        }
      }
    }
  }
Esempio n. 5
0
  private COSStream makeUniqObjectNames(Map objectNameMap, COSStream stream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);

    byte[] buf = new byte[10240];
    int read;
    InputStream is = stream.getUnfilteredStream();
    while ((read = is.read(buf)) > -1) {
      baos.write(buf, 0, read);
    }

    buf = baos.toByteArray();
    baos = new ByteArrayOutputStream(buf.length + 100);
    StringBuffer sbObjectName = new StringBuffer(10);
    boolean bInObjectIdent = false;
    boolean bInText = false;
    boolean bInEscape = false;
    for (int i = 0; i < buf.length; i++) {
      byte b = buf[i];

      if (!bInEscape) {
        if (!bInText && b == '(') {
          bInText = true;
        }
        if (bInText && b == ')') {
          bInText = false;
        }
        if (b == '\\') {
          bInEscape = true;
        }

        if (!bInText && !bInEscape) {
          if (b == '/') {
            bInObjectIdent = true;
          } else if (bInObjectIdent && Character.isWhitespace((char) b)) {
            bInObjectIdent = false;

            // System.err.println(sbObjectName);
            // String object = sbObjectName.toString();

            String objectName = sbObjectName.toString().substring(1);
            String newObjectName = objectName + "overlay";
            baos.write('/');
            baos.write(newObjectName.getBytes("ISO-8859-1"));

            objectNameMap.put(objectName, COSName.getPDFName(newObjectName));

            sbObjectName.delete(0, sbObjectName.length());
          }
        }

        if (bInObjectIdent) {
          sbObjectName.append((char) b);
          continue;
        }
      } else {
        bInEscape = false;
      }

      baos.write(b);
    }

    COSDictionary streamDict = new COSDictionary();
    streamDict.setInt(COSName.LENGTH, baos.size());
    COSStream output = new COSStream(streamDict, pdfDocument.getDocument().getScratchFile());
    output.setFilters(stream.getFilters());
    OutputStream os = output.createUnfilteredStream();
    baos.writeTo(os);
    os.close();

    return output;
  }