I cannot do Multilevel List with merge Document to customize generated Word Document

I have some problem to solve my task, here i provide my problem is, hope you can help me to solve.

Problem :

I won to generate a document from many source document using Syncfusion 'Syncfusion.DocIO.DLS.WordDocument.ImportContent()'. then I want the results of the merged documents to be a multilevel list (follow the indentation of level), according to the documents to be merged. I will try to describe it below : 

EXAMPLE HEADER 1

      I.     Sub Header 1,1

             1.      Item-1

                      document merge using  'Syncfusion.DocIO.DLS.WordDocument.ImportContent()'

             2.      item-2

                      document merge using  'Syncfusion.DocIO.DLS.WordDocument.ImportContent()'

EXAMPLE HEADER 2

  1.   Sub Header 2.1
              document merge using  'Syncfusion.DocIO.DL S.WordDocument.ImportContent()'
      2.   Sub Header 2.2
              document merge using  'Syncfusion.DocIO.DL S.WordDocument.ImportContent()'


I provide the document right now and the expected
  now - NOW_Document.docx
  expected - EXPECTED_Document.docx

here the json of  ItdExportCustomDocumentData
{
    "ItdExportCustomIssueTypeDocumentData": [
        {
            "ItdExportCustomItemDocumentData": [
                {
                    "ItdCososDetail": [],
                    "ItdGroupName": "GROUP 1",
                    "ItdDescription": "/DOCUMENT_1-1.docx",
                    "Recomendation": "/DOCUMENT_1-2.docx",
                    "TindakLanjutUnitKerja": "/DOCUMENT_1-3.docx",
                    "TargetPenyelesaian": "/DOCUMENT_1-4.docx"
                }
            ],
            "TitleIssueType": "OTHER ISSUE/CONCERN"
        },
        {
            "ItdExportCustomItemDocumentData": [
                {
                    "ItdCososDetail": [
                        {
                            "ItdCososDetail": [],
                            "ItdGroupName": "GROUP 2",
                            "ItdDescription": "/DOCUMENT_2-1.docx",
                            "Recomendation": "/DOCUMENT_2-2.docx",
                            "TindakLanjutUnitKerja": "/DOCUMENT_2-3.docx",
                            "TargetPenyelesaian": "/DOCUMENT_2-4.docx"
                        }
                    ],
                    "ItdCoso": "Control Environment"
                },
                {
                    "ItdCososDetail": [
                        {
                            "ItdCososDetail": [],
                            "ItdGroupName": "GROUP 3",
                            "ItdDescription": "/DOCUMENT_3-1.docx",
                            "Recomendation": "/DOCUMENT_3-2.docx",
                            "TindakLanjutUnitKerja": "/DOCUMENT_3-3.docx",
                            "TargetPenyelesaian": "/DOCUMENT_3-4.docx"
                        }
                    ],
                    "ItdCoso": "Risk Assessment"
                }
            ],
            "TitleIssueType": "ISSUE"
        }
    ]
}
here my code :
public FileStreamResult ItdExportV2CustomDocument(ItdExportCustomDocumentData itdExportCustomDocument)
        {

            WDocument wDocument = new WDocument();
            IWParagraphStyle headerStyle = wDocument.AddParagraphStyle("Title_Issue_Type");
            headerStyle.CharacterFormat.FontSize = 14f;
            headerStyle.CharacterFormat.Bold = true;
            headerStyle.ParagraphFormat.LineSpacing = 12f;

            IWParagraphStyle cossosStyle = wDocument.AddParagraphStyle("Cossos_Style");
            cossosStyle.CharacterFormat.FontSize = 12f;
            cossosStyle.ParagraphFormat.AfterSpacing = 15;
            cossosStyle.ParagraphFormat.BeforeSpacing = 10;
            cossosStyle.CharacterFormat.Bold = true;
            cossosStyle.ParagraphFormat.LineSpacing = 12f;

            foreach (var itdExportCustomIssueTypeDocument in itdExportCustomDocument.ItdExportCustomIssueTypeDocumentData!)
            {
                IWSection section = wDocument.AddSection();
                section.PageSetup.PageSize = PageSize.A4;
                section.PageSetup.Margins.All = 72;
                // section.PageSetup.FirstPageTray = PrinterPaperTray.EnvelopeFeed;
                // section.PageSetup.OtherPagesTray = PrinterPaperTray.MiddleBin;

                IWParagraph titleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                titleSection.ApplyStyle("Title_Issue_Type");
                titleSection.AppendText("ISSUE TYPE : " + itdExportCustomIssueTypeDocument.TitleIssueType + "\n");


                if (!string.IsNullOrEmpty(itdExportCustomIssueTypeDocument.TitleIssueType) && itdExportCustomIssueTypeDocument.TitleIssueType!.Equals("ISSUE"))
                {
                    int cossosCounter = 0;
                    foreach (var Cossos in itdExportCustomIssueTypeDocument.ItdExportCustomItemDocumentData!)
                    {
                        IWParagraph cossosSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                        cossosSection.ApplyStyle("Cossos_Style");
                        cossosSection.AppendText("  " + ToRoman(cossosCounter + 1) + ".\t" + Cossos.ItdCoso);
                        cossosCounter++;

                        int itdCounter = 0;
                        foreach (var Itd in Cossos.ItdCososDetail!)
                        {
                            IWParagraph itdTitleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                            itdTitleSection.ApplyStyle("Cossos_Style");
                            itdTitleSection.AppendText("  \t" + (itdCounter + 1) + ".\t" + Itd.ItdGroupName + "\n");
                            itdCounter++;

                            if (!string.IsNullOrEmpty(Itd.ItdDescription))
                            {
                                string docPath = GetObjectFullPath(Itd.ItdDescription);
                                MemoryStream memoryStream = new MemoryStream();
                                if (CheckFileExist(docPath))
                                {
                                    LoadAsync(docPath, memoryStream).Wait();
                                } else {
                                    throw new FileNotFoundException("path: " + Itd.ItdDescription + " doesn't exists. please contact administrator");
                                }
                                WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                               
                                srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                                for (int i = 0; i < srcDocObj.Sections.Count; i++)
                                {
                                    srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                    srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                                }
                                wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                                srcDocObj.Close();
                                memoryStream.Dispose();
                            }

                            IWParagraph recomendationSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                            recomendationSection.ApplyStyle("Cossos_Style");
                            recomendationSection.AppendText("  Rekomendasi : " + "\n");
                            if (!string.IsNullOrEmpty(Itd.Recomendation))
                            {
                                string docPath = GetObjectFullPath(Itd.Recomendation);
                                MemoryStream memoryStream = new MemoryStream();
                                if (CheckFileExist(docPath))
                                {
                                    LoadAsync(docPath, memoryStream).Wait();
                                } else {
                                    throw new FileNotFoundException("path: " + Itd.Recomendation + " doesn't exists. please contact administrator");
                                }

                                WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                                srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                                for (int i = 0; i < srcDocObj.Sections.Count; i++)
                                {
                                    srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                    srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                                }
                                wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                                srcDocObj.Close();
                                memoryStream.Dispose();
                            }

                            IWParagraph tlukSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                            tlukSection.ApplyStyle("Cossos_Style");
                            tlukSection.AppendText("  Tindak Lanjut Unit Kerja : " + "\n");
                            if (!string.IsNullOrEmpty(Itd.TindakLanjutUnitKerja))
                            {
                                string docPath = GetObjectFullPath(Itd.TindakLanjutUnitKerja);
                                MemoryStream memoryStream = new MemoryStream();
                                if (CheckFileExist(docPath))
                                {
                                    LoadAsync(docPath, memoryStream).Wait();
                                } else {
                                    throw new FileNotFoundException("path: " + Itd.TindakLanjutUnitKerja + " doesn't exists. please contact administrator");
                                }
                                WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                                srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                                for (int i = 0; i < srcDocObj.Sections.Count; i++)
                                {
                                    srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                    srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                                }
                                wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                                srcDocObj.Close();
                                memoryStream.Dispose();
                            }

                            IWParagraph targetSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                            targetSection.ApplyStyle("Cossos_Style");
                            targetSection.AppendText("  Target Penyelesaian : " + "\n");
                            if (!string.IsNullOrEmpty(Itd.TargetPenyelesaian))
                            {
                                string docPath = GetObjectFullPath(Itd.TargetPenyelesaian);
                                MemoryStream memoryStream = new MemoryStream();
                                if (CheckFileExist(docPath))
                                {
                                    LoadAsync(docPath, memoryStream).Wait();
                                } else {
                                    throw new FileNotFoundException("path: " + Itd.TargetPenyelesaian + " doesn't exists. please contact administrator");
                                }
                                WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                                srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                                for (int i = 0; i < srcDocObj.Sections.Count; i++)
                                {
                                    srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                    srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                                }
                                wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                                srcDocObj.Close();
                                memoryStream.Dispose();
                            }
                        }
                    }
                }
                else
                {
                    String? cosoTitle = null;
                    long interation = 1;
                    IWParagraph? cosoSection = null;
                    int itdCounter = 0;
                    foreach (var Itd in itdExportCustomIssueTypeDocument.ItdExportCustomItemDocumentData!)
                    {
     
                        IWParagraph itdTitleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                        itdTitleSection.ApplyStyle("Cossos_Style");
                        itdTitleSection.AppendText("  " + (itdCounter + 1) + ".\t" + Itd.ItdGroupName + "\n");
                        itdCounter++;

                        if (!string.IsNullOrEmpty(Itd.ItdDescription))
                        {
                            string docPath = GetObjectFullPath(Itd.ItdDescription);
                            MemoryStream memoryStream = new MemoryStream();
                            if (CheckFileExist(docPath))
                            {
                                LoadAsync(docPath, memoryStream).Wait();
                            } else {
                                throw new FileNotFoundException("path: " + Itd.ItdDescription + " doesn't exists. please contact administrator");
                            }
                            WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                            srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                            for (int i = 0; i < srcDocObj.Sections.Count; i++)
                            {
                                srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                            }
                            wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);

                            srcDocObj.Close();
                            memoryStream.Dispose();
                        }

                            IWParagraph recomendationSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                            recomendationSection.ApplyStyle("Cossos_Style");
                            recomendationSection.AppendText("  Rekomendasi : " + "\n");
                        if (!string.IsNullOrEmpty(Itd.Recomendation))
                        {
                            string docPath = GetObjectFullPath(Itd.Recomendation);
                            MemoryStream memoryStream = new MemoryStream();
                            if (CheckFileExist(docPath))
                            {
                                LoadAsync(docPath, memoryStream).Wait();
                            } else {
                                throw new FileNotFoundException("path: " + Itd.Recomendation + " doesn't exists. please contact administrator");
                            }

                            WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                            srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                            for (int i = 0; i < srcDocObj.Sections.Count; i++)
                            {
                                srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                            }
                            wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                            srcDocObj.Close();
                            memoryStream.Dispose();
                        }

                       
                        IWParagraph tlukSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                        tlukSection.ApplyStyle("Cossos_Style");
                        tlukSection.AppendText("  Tindak Lanjut Unit Kerja : " + "\n");
                        if (!string.IsNullOrEmpty(Itd.TindakLanjutUnitKerja))
                        {
                            string docPath = GetObjectFullPath(Itd.TindakLanjutUnitKerja);
                            MemoryStream memoryStream = new MemoryStream();
                            if (CheckFileExist(docPath))
                            {
                                LoadAsync(docPath, memoryStream).Wait();
                            } else {
                                throw new FileNotFoundException("path: " + Itd.TindakLanjutUnitKerja + " doesn't exists. please contact administrator");
                            }
                            WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                            srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                            for (int i = 0; i < srcDocObj.Sections.Count; i++)
                            {
                                srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                            }
                            wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                            srcDocObj.Close();
                            memoryStream.Dispose();
                        }

                        IWParagraph targetSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
                        targetSection.ApplyStyle("Cossos_Style");
                        targetSection.AppendText("  Target Penyelesaian : " + "\n");
                        if (!string.IsNullOrEmpty(Itd.TargetPenyelesaian))
                        {
                            string docPath = GetObjectFullPath(Itd.TargetPenyelesaian);
                            MemoryStream memoryStream = new MemoryStream();
                            if (CheckFileExist(docPath))
                            {
                                LoadAsync(docPath, memoryStream).Wait();
                            } else {
                                throw new FileNotFoundException("path: " + Itd.TargetPenyelesaian + " doesn't exists. please contact administrator");
                            }
                            WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
                            srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
                            for (int i = 0; i < srcDocObj.Sections.Count; i++)
                            {
                                srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
                                srcDocObj.Sections[i].PageSetup.Margins.All = 72;
                            }
                            wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
                            srcDocObj.Close();
                            memoryStream.Dispose();
                        }
                    }
                }
            }

            FileStream? mergeStreamPath = null;
            if (itdExportCustomDocument.SaveDocument != null && itdExportCustomDocument.SaveDocument == true)
            {
                string fullPath = GetObjectFullPath(itdExportCustomDocument.SaveDocumentTo!);
                string mergeFileName = Path.GetFileName(itdExportCustomDocument.SaveDocumentTo!);
                string tempMergePath = tempPath + "/" + DateTime.Now.ToString("yyMMddHHmm") + "-" + Guid.NewGuid().ToString();
                string tmpMergeFullPath = tempMergePath + "/" + mergeFileName;
                Directory.CreateDirectory(tempMergePath);
                mergeStreamPath = new FileStream(tmpMergeFullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                wDocument.Save(mergeStreamPath, WFormatType.Docx);
                fileTransferUtility.UploadAsync(mergeStreamPath, bucketName, fullPath).Wait();
                DeleteTemporayFiles(tempMergePath);
            }
            if (mergeStreamPath != null)
            {
                mergeStreamPath.Dispose();
            }
            Stream stream = new MemoryStream();
            wDocument.Save(stream, WFormatType.Docx);
            string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            stream.Position = 0;
            return new FileStreamResult(stream, contentType)
            {
                FileDownloadName = "itd.docx"
            };
        }

Attachment: document_4e176a5a.zip

9 Replies

SB Sneha Biju Syncfusion Team February 20, 2024 12:50 PM UTC

Hi Muhammad,

We have found that your requirement is to maintain the indentation level for the imported content from another document. According to the Microsoft Word application, when you import content from another document into a document with a list format paragraph at the end, it does not maintain the list format indentation for the imported content. The same behavior is observed with DocIO as well.

As a workaround, we suggest manually applying the last paragraph style from the main document before importing to all the paragraphs imported from another document. We have prepared a simple console sample for the mentioned workaround. You can find the sample in the attachment below.

Regards,
Sneha.


Attachment: Applyindentationforimportcontent_aac6aa3d.zip


MD Muhammad Dani Rhamadan replied to Sneha Biju February 21, 2024 01:23 PM UTC

Hii, Sneha Biju

I'm currently analyzing it, thank you for your help.

Regards,
Muhammad Dani Ramadhan



CA Chrispine Agunja Imbo Syncfusion Team February 22, 2024 06:30 AM UTC

Hi Muhammad,

please let us know if you need any other assistance.

Regards,

Chris




MD Muhammad Dani Rhamadan February 22, 2024 02:57 PM UTC

Hi  Sneha Biju  &  Chrispine Agunja Imbo.

After I tested with several documents, I found several cases that were not suitable. some text is not indented and some is indented. and also after I add the table to the document, the table is not indented.

I'll send you an example below, I hope it can help solve this.

Regard 

Muhammad Dani Ramadhan



MD Muhammad Dani Rhamadan February 22, 2024 02:58 PM UTC

Hi  Sneha Biju  &  Chrispine Agunja Imbo.

After I tested with several documents, I found several cases that were not suitable. some text is not indented and some is indented. and also after I add the table to the document, the table is not indented.

I'll send you an example below, I hope it can help solve this.

Regard 

Muhammad Dani Ramadhan


Attachment: Applyindentationforimportcontent_(2)_d1daea42.zip


SB Sneha Biju Syncfusion Team February 23, 2024 10:00 AM UTC

Muhammad, we have modified the sample to include indentation for all paragraphs and tables, following the value from the previous list paragraph indentation. You can find the modified sample in the attachment below.


Attachment: Applyindentationforimportcontent_Modified_6dcbc683.zip


MD Muhammad Dani Rhamadan replied to Sneha Biju February 26, 2024 12:02 PM UTC

Hi Sneha Biju. thanks for helping me.

i see another problem that the indent does not work for List. the List do not follow the indentation.

here i send you an example below, thankyou


regards

Muhammad Dani Ramadhan


Attachment: Applyindentationforimportcontent_Modified_c28ae21e.zip


AN Anto Nihil Sahaya Raj Syncfusion Team February 27, 2024 02:56 PM UTC

Muhammad, currently we are checking the reported problem on our side, We will share the details on tomorrow (28th February 2024.)



AA Akash Arul Syncfusion Team February 28, 2024 02:19 PM UTC

Muhammad, for your requirement, the indentation in MS Word will vary on a case by case basis. The MS Word application also does not automatically paste content with the correct indents. We need to manually adjust the indents to the required value. To meet your specific requirement, gradually indent the paragraph until you reach the desired level.

Therefore, there is no generic static value that can be applied programmatically for all cases. Set the paragraph indentation individually for each scenario (different documents) according to the adjustments made in the UI.


Loader.
Up arrow icon