From amplitudes to form factors in the N=4 SYM theory