Device-to-device (D2D) communication is viewed as one promising technology to boost the capacity of wireless networks and the efficiency of resource management. D2D communication heavily depends on the participation of users in sharing contents. Thus, it is imperative to introduce new incentive mechanisms to motivate such user involvement. In this paper, a contract-theoretic approach is proposed to solve the problem of providing incentives for D2D communication in cellular networks. First, using the framework of contract theory, the users' preferences towards D2D communication are classified into a finite number of types, and the service trading between the base station and users is properly modeled. Next, necessary and sufficient conditions are derived to provide incentives for users' engagement in D2D communication. Finally, our analysis is extended to the case in which there is a continuum of users. Simulation results show that the contract can effectively incentivize users' participation, and increase capacity of the cellular network than the other mechanisms.