Hi
I have a service which fills docx template with provided data.
Template is just a file with title page, table of contents page, empty page data inserted into and final page.
Everything was all right until I switched to new template.
After that words started freezing after opening of generated document - no logs, no errors, just freeze and process killed from task manager.
Problem appears when following conditions are met:
- document.MailMerge.Execute method is called
- document is long enough ( ~10 pages is enough for problem to appear )
- Word 2019 is used (there were no problems during test on Word 2016)
I managed to reproduce problem on latest version of Aspose.Words (21.9.0) and prepared code (below) which causes problem.
See my template here: Template_PL.docx (45.6 KB)
using Aspose.Words;
using Aspose.Words.Layout;
using System.IO;
namespace BrokenGeneration
{
class Program
{
static void Main(string[] args)
{
var asposeWordsLicense = new Aspose.Words.License();
asposeWordsLicense.SetLicense("..\\..\\..\\Aspose.Words.Product.Family.lic");
var generator = new ProblemGenerator();
generator.Generate("..\\..\\..\\Template_PL.docx", "..\\..\\..\\Generated.docx");
}
class ProblemGenerator
{
private DocumentBuilder _documentBuilder;
public void Generate(string templatePath, string targetPath)
{
var fileBytes = File.ReadAllBytes(templatePath);
var stream = new MemoryStream(fileBytes);
var document = new Document(stream);
_documentBuilder = new DocumentBuilder(document);
SetLocationInDocument(document);
GenerateContent();
//if line below is commented out - generated file opens without any trouble
document.MailMerge.Execute(new []{ "ProcessName" } , new [] { "dummy process name" });
ApplyChanges(document);
SaveDocument(document, targetPath);
}
private void GenerateContent()
{
for (var i = 0; i < 10; i++)
{
RepeatedContent(i + 1);
}
}
private void RepeatedContent(int i)
{
_documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
_documentBuilder.Writeln($"Header{i}");
_documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
_documentBuilder.Writeln($"Text{i}");
_documentBuilder.Writeln($"Text{i}");
_documentBuilder.Font.ClearFormatting();
_documentBuilder.ParagraphFormat.ClearFormatting();
_documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
_documentBuilder.Font.Bold = true;
_documentBuilder.Writeln("Header Level 2");
_documentBuilder.Font.ClearFormatting();
_documentBuilder.ParagraphFormat.ClearFormatting();
_documentBuilder.Font.Bold = true;
_documentBuilder.Writeln($"Text{i}");
_documentBuilder.Font.Bold = false;
_documentBuilder.Writeln($"Text{i}");
}
private void SetLocationInDocument(Document document)
{
var generatedContentPageStart = GetNodeByPageNumber(3, document);
_documentBuilder.MoveTo(generatedContentPageStart);
}
private static Node GetNodeByPageNumber(int page, Document document)
{
LayoutCollector lc = new LayoutCollector(document);
foreach (Section section in document.Sections)
{
foreach (Node node in section.Body.ChildNodes)
{
if (lc.GetStartPageIndex(node) == page)
return node;
}
}
return null;
}
private void ApplyChanges(Document document)
{
document.UpdateFields();
document.UpdatePageLayout();
}
private void SaveDocument(Document document, string targetPath)
{
using (var stream = new MemoryStream())
{
document.Save(stream, SaveFormat.Docx);
stream.Seek(0, SeekOrigin.Begin);
File.WriteAllBytes(targetPath, stream.ToArray());
}
}
}
}
}