I would suggest you work it out by hand first. Write down the directions, in English (or any other natural language), that a 10 year old child can understand. Once you have that, writing the code is a lot simpler.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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