1. A method for optimizing a layout of a document, the method comprising: from a template document having a plurality of individually-specified components each having a corresponding predefined original position and original size, wherein the plurality of individually-specified components comprises at least one user-content component configured to receive user content from a user of the template document, determining a plurality of spacing distances representing ideal spacing distances between the at least one user-content component and corresponding one or more others of the individually-specified components; from a user document constructed based on the template document and having at least some user content inserted in the at least one user-content component, determining current spacing distances between the at least one user-content component and the corresponding one or more others of the individually-specified components; generating an energy model of the layout of the user document based on the ideal spacing distances from the template document and the current spacing distances in the user document, wherein the energy model models the spacing distances between the at least one user-content component and the corresponding one or more others of the individually-specified components as deformable springs in a closed system, the energy model having a calculated deformation energy for each deformable spring representing an amount the distance between the respective at least one user-content component and a corresponding other of the individually-specified components diverges from the corresponding ideal spacing distance determined from the template document, the energy model further having at least one constraint represented by a calculated at least one of user-content component overlap energy representing how much the at least one user-content component overlaps another user-content component, a background overlap energy representing how much the at least one user-content component overlaps a background component, and a color proximity energy representing how close in color the at least one user-content component is to the color of a background image on which it lies; and based on the calculated deformation energy for each deformable spring and the calculated at least one of the user-content component overlap energy, the background overlap energy, and the color proximity energy, adjusting one or more positions of the at least one user-content components in the user document to minimize the energy of the layout in the user document.