You have the right formula, provided that (x1, y1) and (x2, y2) were actually the end-points of the line. Perhaps they weren't... we certainly can't determine that by looking at your post. Also read this: ItDoesntWorkIsUseless and then post something which would help us to help you better.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com