My code was based on code from this sample: Spire.Doc-Program-Guide/Others/Preserve-Theme-When-Copying-Sections-from-One-Word-Document-to-Another-in-C.html
- Code: Select all
using var stream = new MemoryStream(fileBytes); // byte[]
Document buffer = new Document(stream,true);
Document doc = new Document(true);
foreach (var placeholder in placeholders) // List<List<Pair<pattern, value>>>
{
foreach (var item in placeholder)
{
buffer.Replace(item.Placeholder, item.Value.ToString(), false, true);
}
doc.Sections.Add(buffer.Sections[0].Clone()); // add prepared page
buffer = new Document(stream, true); // clear buffer or return to template condition
}
placeholders.Clear();
doc.SaveToFile("compressed.pdf"); //50MB Physical Disk size, no more 15min Elapsed time