How do I add a PI or Comment before the root element?You must access the document content as a List. Either get the list and add content to its head, or set the list of content explicitly.doc.getContent().add(0, pi); 
or
doc.setContent(listOfContent);