Thanks Tahir,
The file is attached. Here is the code:
namespace CreateContracts
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
//grab type id & contract id
p.CreateContract(3008, 2515);
Console.ReadLine();
}
static void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// Make sure that the node is either a paragraph or table.
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
(!insertAfterNode.NodeType.Equals(NodeType.Table)))
throw new ArgumentException("The destination node should be either a paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstDoc = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
// Insert new node after the reference node.
dstDoc.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
public void CreateContract(int contractTypeId, int contractId)
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Words.lic.xml");
string bhatestcnstr = this.GetConnectionStringByName("BHA_TestConnectionString");
string bhatagscnstr = this.GetConnectionStringByName("BHA_TagsConnectionString");
DataSet ds = new DataSet();
using (SqlConnection mySqlConnection = new SqlConnection(bhatestcnstr))
{
SqlCommand scomd = mySqlConnection.CreateCommand();
scomd.CommandText = "GetContractTypeProvisionsByIdwDoc";
scomd.CommandType = CommandType.StoredProcedure;
scomd.Parameters.Add("@contracttype_id", SqlDbType.Int).Value = contractTypeId;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = scomd;
mySqlConnection.Open();
sda.Fill(ds);
}
DataTable provision_dt = ds.Tables[0];
int i = 0;
int rowCount = provision_dt.Rows.Count;
Document srcDoc = new Document(); //get a provision
Document srcDocTemp = new Document(); //hold them all in here temporarily
Document dstDoc = new Document(@"C:\BHATemplates\BHAKTemplate.doc"); //use this to get a template
//get each provision in the type
//write it to a file
//retrieve the file and add it to the dst doc.
for (i = 0; i < rowCount; i++)
{
byte[] buffer = (Byte[])provision_dt.Rows[i]["Provision_Doc"]; //obtaining binary data of provision document
MemoryStream mainStream = new MemoryStream();
mainStream.Write(buffer, 0, buffer.Length);
mainStream.Position = 0;
FileStream fs = new FileStream(@"C:\BHAProvisions" + provision_dt.Rows[i]["document_name"].ToString(), FileMode.Create, FileAccess.ReadWrite);
mainStream.WriteTo(fs);
mainStream.Close();
mainStream.Dispose();
fs.Close();
fs.Dispose();
//reload the file into the source Doc.
srcDoc = new Document(@"C:\BHAProvisions" + provision_dt.Rows[i]["document_name"].ToString());
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
//////xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if (i == 0)
{
//start the temp doc with the first provision
//it will determine the list stlye for the entire document.
srcDocTemp = srcDoc;
}
else
{
//these two commands ensure that there is only one fix style and that the list numbers will increment
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
srcDocTemp.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
}
//////xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
Bookmark bookmark = dstDoc.Range.Bookmarks["insertionPlace"];
InsertDocument(bookmark.BookmarkStart.ParentNode, srcDocTemp);
//The contract now has all of the provisions and it must be written out to a file.
//Create a unique name anyway you want just so there are no name clashes.
// Guid.NewGuid() provides a number that is unique in the universe.
string myUniqueFileName = string.Format(@"{0}", Guid.NewGuid());
dstDoc.Save(@"C:\BHAContracts\Contract_" + myUniqueFileName + ".docx");
rplcDoc.Save(@"C:\BHAContracts\Contract_" + myUniqueFileName + ".docx");
}
public string GetConnectionStringByName(string name)
{
// Assume failure.
string returnValue = null;
// Look for the name in the connectionStrings section.
ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings[name];
// If found, return the connection string.
if (settings != null)
returnValue = settings.ConnectionString;
return returnValue;
}
}
}