how to setup Document Library template via code in SharePoint

Here is simple example how to setup Document Library template via code:

using (SPSite site = new SPSite("http://mycoolsite")) { 
     using (SPWeb web = site.OpenWeb()) 
          SPList list = web.Lists["MyLibrary"]; 
          SPDocumentLibrary docLib = (SPDocumentLibrary) list; 
          docLib.DocumentTemplateUrl = "/mylibrary/Forms/NewTemplate.doc"; 
     } } 
However, if template is outside Document Library Forms folder you will receive error:
Invalid template URL.
The template must exist in the Forms directory of this document library. Create the template in the Forms directory, and then re-type the Web address. Note that you cannot move or copy a template into the Forms directory.
You can provide document template to forms folder of Document Library via feature using module element. Your feature element.xml would look something like this:
<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns=""> 
        Name="MyLibrary" SetupPath="Features\MyLibrary" 
        Path="" Url="_cts/MyLibrary"> 
        <File Url="NewTemplate.doc" /> 
    </Module> </Elements> 
IMO this is not right solution for you. I suggest that you deploy custom content type already associated to custom template and when you create new library you just add content type to it. There are plenty articles on web covering this. Here are few (found by simple google search) to get you started:
Or you can use custom document templates (as you pointed in your question):