I am trying to insert new documents, images, or pieces of text into a document. After each major section (section break new page)…I want to reset the formatting and remove headers/footers for new sections, but this code it not working…if I insert a document with a header and then an image in a new section, the header carries over to the page with the image on it.
using Aspose.Words;
using Azure.Storage.Blobs;
using DocumentGenerationTestExample.Services;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace DocumentFromManifestService.Controllers
{
[ApiController]
[Route("[controller]")]
public class DocumentController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly BlobFileService _blobFileService;
public DocumentController(IConfiguration configuration)
{
_configuration = configuration;
var connectionString = _configuration["AzureBlobStorage:ConnectionString"];
var containerName = _configuration["AzureBlobStorage:ContainerName"];
_blobFileService = new BlobFileService(new BlobServiceClient(connectionString), containerName);
SetLicense();
}
private static void SetLicense()
{
var license = new License();
license.SetLicense("Licenses/license.lic");
}
[HttpPost("generate")]
public async Task<IActionResult> GenerateDocumentFromManifest([FromBody] JsonElement manifest)
{
var doc = new Document();
var builder = new DocumentBuilder(doc);
string lastMajorNumber = null;
foreach (var item in manifest.EnumerateArray())
{
var number = item.GetProperty("number").GetString();
var nameContent = item.GetProperty("nameContent").GetString();
var type = item.GetProperty("type").GetString();
var details = item.GetProperty("details").GetString();
var currentMajorNumber = number.Split('.')[0];
Section section;
if (lastMajorNumber != null && currentMajorNumber != lastMajorNumber)
{
builder.InsertBreak(BreakType.SectionBreakNewPage);
section = builder.CurrentSection;
}
else if (lastMajorNumber != null && currentMajorNumber == lastMajorNumber)
{
builder.InsertBreak(BreakType.SectionBreakContinuous);
section = builder.CurrentSection;
}
else
{
section = builder.CurrentSection;
}
ResetSectionFormatting(section);
if (type == "Image" || type == "Word")
{
string tempFilePath = await _blobFileService.DownloadFileToTempAsync(nameContent);
ProcessContent(builder, type, tempFilePath);
System.IO.File.Delete(tempFilePath);
}
else if (type == "Text")
{
builder.Writeln(nameContent);
}
lastMajorNumber = currentMajorNumber;
}
var outputStream = new MemoryStream();
doc.Save(outputStream, SaveFormat.Docx);
outputStream.Position = 0;
return File(outputStream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "GeneratedDocument.docx");
}
private static void ResetSectionFormatting(Section section)
{
section.ClearHeadersFooters();
foreach (HeaderFooter hf in section.HeadersFooters)
{
hf.IsLinkedToPrevious = false;
}
PageSetup pageSetup = section.PageSetup;
pageSetup.ClearFormatting();
}
private static void ProcessContent(DocumentBuilder builder, string type, string filePath)
{
if (type == "Image")
{
builder.InsertImage(filePath);
}
else if (type == "Word")
{
Document subDoc = new Document(filePath);
builder.InsertDocument(subDoc, ImportFormatMode.KeepSourceFormatting);
}
}
}
}
I think some of the issue might be because of linking between section headers/footers, but not sure how to fix.
ExampleNotFixed.docx (284.6 KB)
ExampleFixed.docx (275.8 KB)