Trying to do this with the Document while also operating on the model is not a great idea - an exception will be thrown if you try to get offsets into the source later, unless you give it a chance to reparse by ending the current transaction and starting another one.

 DiffElement diff = new DiffElement (pos, pos, "\n");
((ResourceImpl) resource).addExtDiff(diff);

Hint: If you're working with the model and doing some things in a loop, build a sorted list of the positions you want to insert text at, and what you want to insert. When you're done reading info from the model, loop and do all the changes in one pass just before you finish the write transaction.

Source: NetBeans FAQ