Exemplo n.º 1
0
    /// <summary>
    /// Writes the specified value to the PDF stream.
    /// </summary>
    public void Write(PdfString value)
    {
      WriteSeparator(CharCat.Delimiter);
#if true
      PdfStringEncoding encoding = (PdfStringEncoding)(value.Flags & PdfStringFlags.EncodingMask);
      string pdf;
      if ((value.Flags & PdfStringFlags.HexLiteral) == 0)
        pdf = PdfEncoders.ToStringLiteral(value.Value, encoding, SecurityHandler);
      else
        pdf = PdfEncoders.ToHexStringLiteral(value.Value, encoding, SecurityHandler);
      WriteRaw(pdf);
#else
      switch (value.Flags & PdfStringFlags.EncodingMask)
      {
        case PdfStringFlags.Undefined:
        case PdfStringFlags.PDFDocEncoding:
          if ((value.Flags & PdfStringFlags.HexLiteral) == 0)
            WriteRaw(PdfEncoders.DocEncode(value.Value, false));
          else
            WriteRaw(PdfEncoders.DocEncodeHex(value.Value, false));
          break;

        case PdfStringFlags.WinAnsiEncoding:
          throw new NotImplementedException("Unexpected encoding: WinAnsiEncoding");

        case PdfStringFlags.Unicode:
          if ((value.Flags & PdfStringFlags.HexLiteral) == 0)
            WriteRaw(PdfEncoders.DocEncode(value.Value, true));
          else
            WriteRaw(PdfEncoders.DocEncodeHex(value.Value, true));
          break;

        case PdfStringFlags.StandardEncoding:
        case PdfStringFlags.MacRomanEncoding:
        case PdfStringFlags.MacExpertEncoding:
        default:
          throw new NotImplementedException("Unexpected encoding");
      }
#endif
      this.lastCat = CharCat.Delimiter;
    }
Exemplo n.º 2
0
 /// <summary>
 /// Sets the entry to a direct string value.
 /// </summary>
 public void SetString(string key, string value)
 {
   this[key] = new PdfString(value);
 }
    /// <summary>
    /// Prepares the security handler for encrypting the document.
    /// </summary>
    public void PrepareEncryption()
    {
#if !SILVERLIGHT
      Debug.Assert(this.document.securitySettings.DocumentSecurityLevel != PdfDocumentSecurityLevel.None);
      int permissions = (int)this.Permission;
      bool strongEncryption = this.document.securitySettings.DocumentSecurityLevel == PdfDocumentSecurityLevel.Encrypted128Bit;

      PdfInteger vValue;
      PdfInteger length;
      PdfInteger rValue;

      if (strongEncryption)
      {
        vValue = new PdfInteger(2);
        length = new PdfInteger(128);
        rValue = new PdfInteger(3);
      }
      else
      {
        vValue = new PdfInteger(1);
        length = new PdfInteger(40);
        rValue = new PdfInteger(2);
      }

      if (String.IsNullOrEmpty(this.userPassword))
        this.userPassword = "";
      // Use user password twice if no owner password provided.
      if (String.IsNullOrEmpty(this.ownerPassword))
        this.ownerPassword = this.userPassword;

      // Correct permission bits
      permissions |= (int)(strongEncryption ? (uint)0xfffff0c0 : (uint)0xffffffc0);
      permissions &= unchecked((int)0xfffffffc);

      PdfInteger pValue = new PdfInteger(permissions);

      Debug.Assert(this.ownerPassword.Length > 0, "Empty owner password.");
      byte[] userPad = PadPassword(this.userPassword);
      byte[] ownerPad = PadPassword(this.ownerPassword);

      this.md5.Initialize();
      this.ownerKey = ComputeOwnerKey(userPad, ownerPad, strongEncryption);
      byte[] documentID = PdfEncoders.RawEncoding.GetBytes(this.document.Internals.FirstDocumentID);
      InitWidhUserPassword(documentID, this.userPassword, this.ownerKey, permissions, strongEncryption);

      PdfString oValue = new PdfString(PdfEncoders.RawEncoding.GetString(this.ownerKey));
      PdfString uValue = new PdfString(PdfEncoders.RawEncoding.GetString(this.userKey));

      Elements[Keys.Filter] = new PdfName("/Standard");
      Elements[Keys.V] = vValue;
      Elements[Keys.Length] = length;
      Elements[Keys.R] = rValue;
      Elements[Keys.O] = oValue;
      Elements[Keys.U] = uValue;
      Elements[Keys.P] = pValue;
#endif
    }
Exemplo n.º 4
0
      /// <summary>
      /// Converts the specified value to String.
      /// If the value not exists, the function returns the empty string.
      /// </summary>
      public string GetString(string key, bool create)
      {
        object obj = this[key];
        if (obj == null)
        {
          if (create)
            this[key] = new PdfString();
          return "";
        }
        if (obj is PdfReference)
          obj = ((PdfReference)obj).Value;

        if (obj is PdfString)
          return ((PdfString)obj).Value;
        else if (obj is PdfStringObject)
          return ((PdfStringObject)obj).Value;
        else if (obj is PdfName)
          return ((PdfName)obj).Value;
        else if (obj is PdfNameObject)
          return ((PdfNameObject)obj).Value;
        throw new InvalidCastException("GetString: Object is not a string.");
      }
 /// <summary>
 /// Encrypts a string.
 /// </summary>
 void EncryptString(PdfString value)
 {
   if (value.Length != 0)
   {
     byte[] bytes = value.EncryptionValue;
     PrepareKey();
     EncryptRC4(bytes);
     value.EncryptionValue = bytes;
   }
 }