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)
