Exemplo n.º 1
0
 /**
  * This will set an item in the dictionary. If value is null then the result will be the same as
  * removeItem( key ).
  *
  * @param key The key to the dictionary object.
  * @param value The value to the dictionary object.
  */
 public void setItem(COSName key, COSObjectable value) {
   COSBase base = null;
   if (value != null) {
     base = value.getCOSObject();
   }
   setItem(key, base);
 }
Exemplo n.º 2
0
 /**
  * This is a convenience method that will convert the value to a COSString object. If it is null
  * then the object will be removed.
  *
  * @param key The key to the object,
  * @param value The string value for the name.
  */
 public void setString(COSName key, String value) {
   COSString name = null;
   if (value != null) {
     name = new COSString(value);
   }
   setItem(key, name);
 }
Exemplo n.º 3
0
  private void writeColorSpace(PDColorSpace colorSpace) throws IOException {
    COSName key = null;
    if (colorSpace instanceof PDDeviceGray
        || colorSpace instanceof PDDeviceRGB
        || colorSpace instanceof PDDeviceCMYK) {
      key = COSName.getPDFName(colorSpace.getName());
    } else {
      COSDictionary colorSpaces =
          (COSDictionary) resources.getCOSDictionary().getDictionaryObject(COSName.COLORSPACE);
      if (colorSpaces == null) {
        colorSpaces = new COSDictionary();
        resources.getCOSDictionary().setItem(COSName.COLORSPACE, colorSpaces);
      }
      key = colorSpaces.getKeyForValue(colorSpace.getCOSObject());

      if (key == null) {
        int counter = 0;
        String csName = "CS";
        while (colorSpaces.containsValue(csName + counter)) {
          counter++;
        }
        key = COSName.getPDFName(csName + counter);
        colorSpaces.setItem(key, colorSpace);
      }
    }
    key.writePDF(output);
    appendRawCommands(SPACE);
  }
 /**
  * This will set the font family.
  *
  * @param fontFamily The font family.
  */
 public void setFontFamily(String fontFamily) {
   COSString name = null;
   if (fontFamily != null) {
     name = new COSString(fontFamily);
   }
   dic.setItem(COSName.FONT_FAMILY, name);
 }
Exemplo n.º 5
0
 /**
  * This will add all of the dictionarys keys/values to this dictionary, but only if they don't
  * already exist. If a key already exists in this dictionary then nothing is changed.
  *
  * @param dic The dic to get the keys from.
  */
 public void mergeInto(COSDictionary dic) {
   for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
     if (getItem(entry.getKey()) == null) {
       setItem(entry.getKey(), entry.getValue());
     }
   }
 }
 /**
  * This will set the font stretch.
  *
  * @param fontStretch The new stretch for the font.
  */
 public void setFontStretch(String fontStretch) {
   COSName name = null;
   if (fontStretch != null) {
     name = COSName.getPDFName(fontStretch);
   }
   dic.setItem(COSName.FONT_STRETCH, name);
 }
 /**
  * Set the fonts bounding box.
  *
  * @param rect The new bouding box.
  */
 public void setFontBoundingBox(PDRectangle rect) {
   COSArray array = null;
   if (rect != null) {
     array = rect.getCOSArray();
   }
   dic.setItem(COSName.FONT_BBOX, array);
 }
 /**
  * This will set the character set for the font.
  *
  * @param charSet The new character set for the font.
  */
 public void setCharacterSet(String charSet) {
   COSString name = null;
   if (charSet != null) {
     name = new COSString(charSet);
   }
   dic.setItem(COSName.CHAR_SET, name);
 }
Exemplo n.º 9
0
 /**
  * This is a convenience method that will convert the value to a COSName object. If it is null
  * then the object will be removed.
  *
  * @param key The key to the object,
  * @param value The string value for the name.
  */
 public void setName(COSName key, String value) {
   COSName name = null;
   if (value != null) {
     name = COSName.getPDFName(value);
   }
   setItem(key, name);
 }
 /**
  * This will set the font name.
  *
  * @param fontName The new name for the font.
  */
 public void setFontName(String fontName) {
   COSName name = null;
   if (fontName != null) {
     name = COSName.getPDFName(fontName);
   }
   dic.setItem(COSName.FONT_NAME, name);
 }
Exemplo n.º 11
0
 /**
  * This is a convenience method that will convert the value to a COSInteger object.
  *
  * @param embeddedDictionary The embedded dictionary.
  * @param key The key to the object,
  * @param value The int value for the name.
  */
 public void setEmbeddedInt(String embeddedDictionary, COSName key, int value) {
   COSDictionary embedded = (COSDictionary) getDictionaryObject(embeddedDictionary);
   if (embedded == null) {
     embedded = new COSDictionary();
     setItem(embeddedDictionary, embedded);
   }
   embedded.setInt(key, value);
 }
Exemplo n.º 12
0
 /**
  * This is a convenience method that will convert the value to a COSString object. If it is null
  * then the object will be removed.
  *
  * @param embedded The embedded dictionary to set the item in.
  * @param key The key to the object,
  * @param value The string value for the name.
  */
 public void setEmbeddedString(String embedded, COSName key, String value) {
   COSDictionary dic = (COSDictionary) getDictionaryObject(embedded);
   if (dic == null && value != null) {
     dic = new COSDictionary();
     setItem(embedded, dic);
   }
   if (dic != null) {
     dic.setString(key, value);
   }
 }
 public COSBase getCOSObject() {
   COSDictionary dict = new COSDictionary();
   COSArray arr = new COSArray();
   for (Entry<Integer, PDPageLabelRange> i : labels.entrySet()) {
     arr.add(COSInteger.get(i.getKey()));
     arr.add(i.getValue());
   }
   dict.setItem(COSName.NUMS, arr);
   return dict;
 }
Exemplo n.º 14
0
 /**
  * Set the date object.
  *
  * @param embedded The embedded dictionary.
  * @param key The key to the date.
  * @param date The date to set.
  */
 public void setEmbeddedDate(String embedded, COSName key, Calendar date) {
   COSDictionary dic = (COSDictionary) getDictionaryObject(embedded);
   if (dic == null && date != null) {
     dic = new COSDictionary();
     setItem(embedded, dic);
   }
   if (dic != null) {
     dic.setDate(key, date);
   }
 }
Exemplo n.º 15
0
 /**
  * This will add all of the dictionarys keys/values to this dictionary. Only called when adding
  * keys to a trailer that already exists.
  *
  * @param dic The dic to get the keys from.
  */
 public void addAll(COSDictionary dic) {
   for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
     /*
      * If we're at a second trailer, we have a linearized
      * pdf file, meaning that the first Size entry represents
      * all of the objects so we don't need to grab the second.
      */
     if (!entry.getKey().getName().equals("Size")
         || !items.containsKey(COSName.getPDFName("Size"))) {
       setItem(entry.getKey(), entry.getValue());
     }
   }
 }
Exemplo n.º 16
0
 /**
  * (Optional, PDF 1.7) An array of names indicating acceptable digest algorithms to use when
  * signing. The value shall be one of <b>SHA1</b>, <b>SHA256</b>, <b>SHA384</b>, <b>SHA512</b>,
  * <b>RIPEMD160</b>. The default value is implementation-specific.
  *
  * <p>This property is only applicable if the digital credential signing contains RSA
  * public/privat keys
  *
  * @param digestMethod is a list of possible names of the digests, that should be used for
  *     signing.
  */
 public void setDigestMethod(List<COSName> digestMethod) {
   // integrity check
   for (COSName cosName : digestMethod) {
     if (!(cosName.equals(COSName.DIGEST_SHA1)
         || cosName.equals(COSName.DIGEST_SHA256)
         || cosName.equals(COSName.DIGEST_SHA384)
         || cosName.equals(COSName.DIGEST_SHA512)
         || cosName.equals(COSName.DIGEST_RIPEMD160))) {
       throw new IllegalArgumentException(
           "Specified digest " + cosName.getName() + " isn't allowed.");
     }
   }
   dictionary.setItem(COSName.DIGEST_METHOD, COSArrayList.converterToCOSArray(digestMethod));
 }
Exemplo n.º 17
0
  @Override
  public void addDssDictionary(
      InputStream inputStream, OutputStream outpuStream, List<DSSDictionaryCallback> callbacks) {
    File toSignFile = null;
    File signedFile = null;
    FileInputStream fis = null;
    PDDocument pdDocument = null;
    try {
      toSignFile = DSSPDFUtils.getFileFromPdfData(inputStream);
      pdDocument = PDDocument.load(toSignFile);

      signedFile = File.createTempFile("sd-dss-", "-signed.pdf");

      final FileOutputStream fileOutputStream =
          DSSPDFUtils.getFileOutputStream(toSignFile, signedFile);

      if (CollectionUtils.isNotEmpty(callbacks)) {
        final COSDictionary cosDictionary = pdDocument.getDocumentCatalog().getCOSDictionary();
        cosDictionary.setItem("DSS", buildDSSDictionary(callbacks));
        cosDictionary.setNeedToBeUpdate(true);
      }

      if (pdDocument.getDocumentId() == null) {
        pdDocument.setDocumentId(0L);
      }
      pdDocument.saveIncremental(inputStream, fileOutputStream);

      fis = new FileInputStream(signedFile);
      IOUtils.copy(fis, outpuStream);
    } catch (Exception e) {
      throw new DSSException(e);
    } finally {
      IOUtils.closeQuietly(pdDocument);
      IOUtils.closeQuietly(fis);
      DSSUtils.delete(toSignFile);
      DSSUtils.delete(signedFile);
    }
  }
Exemplo n.º 18
0
  /**
   * This will import a fdf field from a fdf document.
   *
   * @param fdfField The fdf field to import.
   * @throws IOException If there is an error importing the data for this field.
   */
  void importFDF(FDFField fdfField) throws IOException {
    COSBase fieldValue = fdfField.getCOSValue();
    if (fieldValue != null) {
      dictionary.setItem(COSName.V, fieldValue);
    }
    Integer ff = fdfField.getFieldFlags();
    if (ff != null) {
      setFieldFlags(ff);
    } else {
      // these are suppose to be ignored if the Ff is set.
      Integer setFf = fdfField.getSetFieldFlags();
      int fieldFlags = getFieldFlags();

      if (setFf != null) {
        int setFfInt = setFf;
        fieldFlags = fieldFlags | setFfInt;
        setFieldFlags(fieldFlags);
      }

      Integer clrFf = fdfField.getClearFieldFlags();
      if (clrFf != null) {
        // we have to clear the bits of the document fields for every bit that is
        // set in this field.
        //
        // Example:
        // docFf = 1011
        // clrFf = 1101
        // clrFfValue = 0010;
        // newValue = 1011 & 0010 which is 0010
        int clrFfValue = clrFf;
        clrFfValue ^= 0xFFFFFFFF;
        fieldFlags = fieldFlags & clrFfValue;
        setFieldFlags(fieldFlags);
      }
    }
  }
Exemplo n.º 19
0
 /**
  * This will set an item in the dictionary. If value is null then the result will be the same as
  * removeItem( key ).
  *
  * @param key The key to the dictionary object.
  * @param value The value to the dictionary object.
  */
 public void setItem(String key, COSBase value) {
   setItem(COSName.getPDFName(key), value);
 }
 /**
  * Set a stream containing a font program that is not true type or type 1.
  *
  * @param stream The font program stream.
  */
 public void setFontFile3(PDStream stream) {
   dic.setItem(COSName.FONT_FILE3, stream);
 }
 /**
  * Set the true type font program.
  *
  * @param ttfStream The true type stream.
  */
 public void setFontFile2(PDStream ttfStream) {
   dic.setItem(COSName.FONT_FILE2, ttfStream);
 }
 /**
  * Set the type 1 font program.
  *
  * @param type1Stream The type 1 stream.
  */
 public void setFontFile(PDStream type1Stream) {
   dic.setItem(COSName.FONT_FILE, type1Stream);
 }
 /** Constructor. */
 public PDFontDescriptorDictionary() {
   dic = new COSDictionary();
   dic.setItem(COSName.TYPE, COSName.FONT_DESC);
 }
Exemplo n.º 24
0
 /** Default constructor. */
 public PDSeedValue() {
   dictionary = new COSDictionary();
   dictionary.setItem(COSName.TYPE, COSName.SV);
   dictionary.setDirect(true); // the specification claim to use direct objects
 }
Exemplo n.º 25
0
 /**
  * This will set an item in the dictionary.
  *
  * @param key The key to the dictionary object.
  * @param value The value to the dictionary object.
  */
 public void setBoolean(COSName key, boolean value) {
   setItem(key, COSBoolean.getBoolean(value));
 }
Exemplo n.º 26
0
 /**
  * This is a convenience method that will convert the value to a COSInteger object.
  *
  * @param key The key to the object,
  * @param value The int value for the name.
  */
 public void setInt(COSName key, int value) {
   setItem(key, COSInteger.get(value));
 }
Exemplo n.º 27
0
 /**
  * This will set an item in the dictionary. If value is null then the result will be the same as
  * removeItem( key ).
  *
  * @param key The key to the dictionary object.
  * @param value The value to the dictionary object.
  */
 public void setItem(String key, COSObjectable value) {
   setItem(COSName.getPDFName(key), value);
 }
Exemplo n.º 28
0
 /**
  * This is a convenience method that will convert the value to a COSFloat object.
  *
  * @param key The key to the object,
  * @param value The int value for the name.
  */
 public void setFloat(COSName key, float value) {
   COSFloat fltVal = new COSFloat(value);
   setItem(key, fltVal);
 }
Exemplo n.º 29
0
 /**
  * This will set an item in the dictionary.
  *
  * @param key The key to the dictionary object.
  * @param value The value to the dictionary object.
  */
 public void setBoolean(String key, boolean value) {
   setItem(COSName.getPDFName(key), COSBoolean.getBoolean(value));
 }
Exemplo n.º 30
0
 /**
  * This is a convenience method that will convert the value to a COSInteger object.
  *
  * @param key The key to the object,
  * @param value The int value for the name.
  */
 public void setLong(COSName key, long value) {
   COSInteger intVal = null;
   intVal = COSInteger.get(value);
   setItem(key, intVal);
 }