Random Bold Style Headings

Hello,

My code is taking multiple documents with single contract provisions and assembling them to make one large contract. It works pretty well, but some of the numbering on the second level of numbering is randomly printing with bold text, despite the fact that the numbers were not bold in the original document. Even within the same second level list, some numbering will be bold, while others will not.

-Jake

Hi Jake,

Thanks for your inquiry. Could you please attach your input Word documents here along with code for testing? I will investigate the issue on my side and provide you more information.

Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/insert-and-append-documents/

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;
        }
    }
}

Hi Jake,

Thanks for sharing the code and document. Please note that ImportFormatMode defines how conflicting styles are handled when one document is imported into the other. When you append different document at the end of parent document this option specifies how formatting is resolved when both documents have a style with the same name, but different formatting.

I suggest you please read difference between ImportFormat Modes from here:
https://docs.aspose.com/words/net/insert-and-append-documents/

Unfortunately, I am unable to execute the shared code. It would be great if you please share following detail for investigation purpose.

  • Please share the detail of DataTable. Please create the DataTable as shown in following code snippet to execute the shared code
  • Please supply us with the input document which you have used in your code
  • Please supply us with the output document showing the undesired behavior
  • Please supply us with the expected document showing the desired behavior (You can create this document using Microsoft Word).

I will really appreciate your cooperation for this.

DataTable dt = new DataTable("DataTableName");
dt.Columns.Add("Column1", typeof(string));
dt.Rows.Add("value 1");
dt.Rows.Add("value 2");