Exemplo n.º 1
0
  private void mergePage(COSArray array, PDPage page) {
    int layoutPageNum = pageCount % layoutPages.size();
    LayoutPage layoutPage = (LayoutPage) layoutPages.get(layoutPageNum);
    PDResources resources = page.findResources();
    if (resources == null) {
      resources = new PDResources();
      page.setResources(resources);
    }
    COSDictionary docResDict = resources.getCOSDictionary();
    COSDictionary layoutResDict = layoutPage.res;
    mergeArray(COSName.PROC_SET, docResDict, layoutResDict);
    mergeDictionary(COSName.FONT, docResDict, layoutResDict, layoutPage.objectNameMap);
    mergeDictionary(COSName.XOBJECT, docResDict, layoutResDict, layoutPage.objectNameMap);
    mergeDictionary(COSName.EXT_G_STATE, docResDict, layoutResDict, layoutPage.objectNameMap);

    // we are going to wrap the existing content around some save/restore
    // graphics state, so the result is
    //
    // <save graphics state>
    // <all existing content streams>
    // <restore graphics state>
    // <overlay content>
    array.add(0, saveGraphicsStateStream);
    array.add(restoreGraphicsStateStream);
    array.add(layoutPage.contents);
  }
Exemplo 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++;
    }
  }
Exemplo n.º 3
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);
        }
      }
    }
  }