I want to find the maximum compression the spring will undergo. Is it possible to use the work-energy theorem over the Course of the entire motion?
Yes it is. But you will have to apply it in two parts.
The work energy theorem states that the net work done on an object equals its change in kinetic energy.
During the fall up to the point where the object contacts the spring the only force acting on the object is gravity. So if the object starts at rest you can determine its change kinetic energy at the point of impact based on the work done by gravity. The change in kinetic energy will be $mgh$ where $h$ is the distance the object falls before impacting the spring.
After that you can reapply the work energy theorem after impact. The net work done on the object will then be equal to the integral of the net force applied, $kx-mg$, times the spring displacement, $dx$, and set that equal to the loss in kinetic energy, $mgh$, or
$$\int (kx-mg)dx=mgh$$
$$\frac{kx^2}{2}=mgx+mgh$$
But you can also use conservation of energy where the energy stored in the spring upon stopping the object is $\frac{kx^2}{2}$, where $x$ is the compression of the spring, equals the loss of the kinetic energy, $mgh$, plus the loss of additional gravitational potential energy during compression of the spring, $mgx$, or
$$\frac{kx^2}{2}=mgh+mgx$$
Thanks, i think i get it now. So at the end, I would have just arrived at the same result as if I had just taken thr work done by gravity mg(x+h)? Is this always the case? –
It is only correct if the overall change in kinetic energy is zero, which in this case it is. The object starts at rest and comes to a rest when the spring is fully compressed. Therefore, the overall change in kinetic energy is zero. Per the work energy theorem, that means the net work done on the object is zero. In this case gravity does positive work equal to $mg(x+h)$. The spring does negative work of $-\frac{kx^2}{2}$ because the direction of its force is opposite the direction of displacement. Therefore, the net work is
$$W_{net}=mg(h+x)-\frac{kx^2}{2}=0$$
$$\frac{kx^2}{2}=mg(h+x)$$.
Or, to put it another way, the loss of gravitational potential energy equals the gain in spring potential energy.
Hope this helps.