Vectorial optical fields have sparked considerable interest with potential applications such as optical nano-fabrication, optical micromachining, quantum information processing, optical imaging, and so on. Traditional compact vectorial optical generators with amplitude modulation perform poorly in terms of diffraction effect reduction. In order to address this issue, the refractive 4f system in amplitude modulation is longitudinally aligned using an optimization approach presented in this research. The phase images used for longitudinal alignment are loaded into the liquid crystal spatial light modulator (SLM), and the distance between the lens and the mirror in the reflective 4f system is adjusted for longitudinal alignment by compensating for the neglected phase in the integrated module of the compact vectorial optical field generator. The spot images collected by the CCD are processed using the improved eight-direction Sobel operator, and the longitudinal alignment in the reflective 4f system is determined by the sharpness of the image. The sharpness of the lines' edges and the overall image are both enhanced after optimization compared to before optimization. The results show that the proposed method can significantly reduce the longitudinal alignment error of the reflective 4f system in the amplitude modulation of the compact vectorial optical field generator, lessen the diffraction effect, and improve the performance of the system.