Content Control Wrapping Issue

Hi Team,

I have removed the whitespace and the header/footer using the page-wise logic below. However, after doing this, the specific content control mentioned below is not wrapping correctly. I am using Aspose.Words Version : 25.10.0.0

In the screenshot below, you can see that the content control with ID “228-e69-a25-9e0-e569” is only partially wrapped and is not wrapped properly as per word document. Additionally, “Section_3” is being appended to “Section_2” instead of appearing as a separate section.

Could you please explain why this is happening and how we can resolve this issue?

Code :

private void CreateSectionBreak(ref Document doc)
{
    try
    {
        Aspose.Words.Document tempDoc = (Aspose.Words.Document)doc.Clone(false);
        int pageNumber = 1;
        Aspose.Words.Document page = null;

        for (int i = 0; i < doc.PageCount; i++)
        {
            //Console.WriteLine("page number :" + i + " started");
            page = doc.ExtractPages(i, 1);

            // Remove section breaks in the page.
            while (page.Sections.Count > 1)
            {
                page.FirstSection.AppendContent(page.Sections[1]);
                page.Sections[1].Remove();
            }


            // Reset section start of the section.
            page.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

            // unlink page field
            FixPageNumber(page, ref pageNumber);

            SetHeaderFooterIntoBody(page);
            // Remove headers/footers since we already moved their content to main body.
            page.GetChildNodes(NodeType.HeaderFooter, true).Clear();
            RemoveExtraSpace(ref page);
            //RemoveSectionBreak(ref page);
            //tempDoc.AppendDocument(page, ImportFormatMode.KeepSourceFormatting);
            tempDoc.AppendDocument(page, ImportFormatMode.KeepSourceFormatting, new ImportFormatOptions() { KeepSourceNumbering = true });
            //destDoc.LastSection.PageSetup.RestartPageNumbering = true;
            //Console.WriteLine("page number :" + i + " completed");
            page = null;
        }
        doc = tempDoc;
        tempDoc = null;
    }
    catch (Exception ex)
    {

    }
}

private void FixPageNumber(Document page, ref int pageNumber)
{
    try
    {
        //// unlink page field
        //page.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList()
        //      .ForEach(f => { f.Update(); f.Unlink(); });
        DocumentBuilder builder = new DocumentBuilder(page);

        var pageFields = page.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList();
        if (pageFields.Count > 0)
        {
            for (int i = 0; i < pageFields.Count; i++)
            {
                builder.MoveToField(pageFields[i], true);
                builder.Write(pageNumber.ToString());
                pageFields[i].Remove();
            }
            pageNumber++;
        }
    }
    catch (Exception ex)
    {
    }
}

private void SetHeaderFooterIntoBody(Document pageDoc)
{
    // Check whether header/footer displaid
    // This might be either primary or field page header/footer.
    HeaderFooter displayedHeader = pageDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter ?
        pageDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst] :
        pageDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
    HeaderFooter displayedFooter = pageDoc.LastSection.PageSetup.DifferentFirstPageHeaderFooter ?
        pageDoc.LastSection.HeadersFooters[HeaderFooterType.FooterFirst] :
        pageDoc.LastSection.HeadersFooters[HeaderFooterType.FooterPrimary];

    // Move content into the main body.
    if (displayedHeader != null)
    {
        while (displayedHeader.HasChildNodes)
            pageDoc.FirstSection.Body.PrependChild(displayedHeader.LastChild);
    }
    if (displayedFooter != null)
    {
        //Paragraph paragraph = new Paragraph(pageDoc);
        //Run run = new Run(pageDoc, ControlChar.LineBreak);
        //paragraph.AppendChild(run);
        //pageDoc.LastSection.Body.AppendChild(paragraph);

        while (displayedFooter.HasChildNodes)
            pageDoc.LastSection.Body.AppendChild(displayedFooter.FirstChild);

    }
}
private void RemoveExtraSpace(ref Document page)
{
    Paragraph firstPara = page.FirstSection.Body.FirstParagraph;
    //Remove empty paragraphs from the start
    while (page.FirstSection.Body.FirstParagraph != null && string.IsNullOrWhiteSpace(page.FirstSection.Body.FirstParagraph.Range.Text.Trim()))
    {
        int count = page.FirstSection.Body.FirstParagraph.GetChildNodes(NodeType.Shape, isDeep: true).Count;
        if (count != 0)
            break;
        page.FirstSection.Body.FirstParagraph.Remove();
    }

    //Remove empty paragraphs from the end
    while (page.LastSection.Body.LastParagraph != null && string.IsNullOrWhiteSpace(page.LastSection.Body.LastParagraph.Range.Text.Trim()))
    {
        int count = page.LastSection.Body.LastParagraph.GetChildNodes(NodeType.Shape, isDeep: true).Count;
        if (count != 0)
            break;
        page.LastSection.Body.LastParagraph.Remove();
    }
    if (!CheckIsHavePageBreak(ref page))
    {
        Paragraph paragraph = new Paragraph(page);
        Run run = new Run(page, " ");
        paragraph.AppendChild(run);
        page.LastSection.Body.AppendChild(paragraph);
    }
    //else if (page.LastSection.Body.LastParagraph != null && page.LastSection.Body.LastParagraph.Range.Text.Trim() == "")
    //{
    //	int count = page.LastSection.Body.LastParagraph.GetChildNodes(NodeType.Shape, isDeep: true).Count;
    //	if (count == 0)
    //		page.LastSection.Body.LastParagraph.Remove();
    //}
}
private bool CheckIsHavePageBreak(ref Document page)
{
    var pageBreak = page.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().FirstOrDefault(x => x.Tag.StartsWith("PG_") || x.Tag.StartsWith("CPG_") || x.Tag.StartsWith("PBS_"));
    if (pageBreak != null)
    {
        return true;
    }
    return false;
}


var loadOption = new Aspose.Words.Loading.LoadOptions() { LoadFormat = LoadFormat.Docx, };
Document doc = new Document(@"Document.docx", loadOption);
CreateSectionBreak(ref doc);
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
    ExportImagesAsBase64 = true,
    ExportXhtmlTransitional = true,
    Encoding = System.Text.Encoding.UTF8,
    ExportHeadersFootersMode = ExportHeadersFootersMode.None,
    ExportPageMargins = true,
    ExportPageSetup = true,
    UseHighQualityRendering = false,
    AllowEmbeddingPostScriptFonts = true,
    PrettyFormat = false,
    SaveFormat = SaveFormat.Html,
    ExportTocPageNumbers = true,
    CssStyleSheetType = CssStyleSheetType.Inline,
    MemoryOptimization = true,
    ExportListLabels = ExportListLabels.AsInlineText,
    ImageResolution = 200
};
doc.Save(@"Output.html", options);

Screenshot :

Source :
MainDocument_640-876-5cc-dab-4042 - Copy.docx (153.9 KB)
Output.zip (11.0 KB)

@AlpeshChaudhariDev Unfortunately, the problem is not quite clear enough. Could you please provide the expected output you would like to get?

Actually, I am using the CreateSectionBreak function to move the header and footer into the document body. During this process, I encountered an issue where the content control is not wrapping its content correctly, as it does in the Word document.

The content control tag ID is 228-e69-a25-9e0-e569. I expect the output to display the content control with its content properly wrapped inside the corresponding div.

Right now i am not able to get the whole content wrapped with div. You can also refer the screenshot.

@AlpeshChaudhariDev Thank you for additional information. Do you mean your goal is to get the same visual document appearance as it is displayed in MS Word? If so there is no way to achieve this in flow HTML output, because flow HTML document have page boundaries as MS Word document. You can consider rendering the document to HtmlFixed format, this format is designed for viewing purposes and the output should look the same as in MS Word.

The content control tag ID is 228-e69-a25-9e0-e569. I expect the output to display the content control with all of its content properly wrapped inside the corresponding div.
However, as shown in my HTML output, the content associated with the tag ID 228-e69-a25-9e0-e569 is not wrapping all the content that is wrapped in the Word output. I want the entire content to be wrapped inside the div, because the content control is applied to the whole content. I am not taking about the formatting of the output.

@AlpeshChaudhariDev Thank you for explanation. In your code you are splitting the document an rejoining it back after processing. This changes the original document nodes structure. In your case SDT is spans several pages. After splitting the document into pages, there are two separate documents with SDTs, so after rejoining the original document nodes structure is changed. This is expected.