My scenario is that specific document headers and footers must be applied to documents stored in SharePoint.
Header and footers are defined in a template document. The process is as follow:
- - loading the template document which contains the header and footer to be applied to processed documents
- loading the documents from SharePoint which should have the header and footer applied
- applying the loaded header and footer to the documents
- storing the modified documents back to SharePoint.
this works great by using the Spire.Doc Nuget - it's stable and easy to read code.
But the processed documents are getting larger and larger as I begin to repeat the mentioned process a couple of times.
I realized that if I open a processed document in Microsoft Word and use the "Inspect document" tool inside Word in order to remove the Custom XML Data parts, the document gets its initial file size back. But then, If I replace the already existing header and footer again by repeating the above process again, the document gets larger again and afterwards contains Custom XML Data again, which I can remove again to get the initial file size back.
Is this a known issue in combination with SharePoint as the storage location? I am afraid that this has something to do with the Office integration of SharePoint. Maybe I'm doing something totally wrong?
My code is below:
- Code: Select all
public class WordDocument: IDocument
{
private Document doc;
public WordDocument(Stream srcDocument)
{
this.doc = new Document();
doc.LoadFromStream(srcDocument, FileFormat.Auto);
}
public void ReplaceHeader(Stream headerTemplateStream, IDictionary<string, string> replacementsList)
{
var headerTemplateDoc = LoadDocument(headerTemplateStream, replacementsList);
var templateHeader = headerTemplateDoc.Sections[0].HeadersFooters.Header;
var docHeader = doc.Sections[0].HeadersFooters.Header;
ReplaceHeaderFooter(templateHeader, docHeader);
}
public void ReplaceFooter(Stream footerTemplateStream, IDictionary<string, string> replacementsList)
{
var footerTemplateDoc = LoadDocument(footerTemplateStream, replacementsList);
var templateFooter = footerTemplateDoc.Sections[0].HeadersFooters.Footer;
var docFooter = doc.Sections[0].HeadersFooters.Footer;
ReplaceHeaderFooter(templateFooter, docFooter);
}
private static void ReplaceHeaderFooter(HeaderFooter template, HeaderFooter doc)
{
doc.EnsureMinimum();
doc.Paragraphs.Clear();
foreach (DocumentObject o in template.ChildObjects)
{
doc.ChildObjects.Add(o.Clone());
}
}
private static Document LoadDocument(Stream docStream, IDictionary<string, string> replacementsList)
{
var doc = new Document();
doc.LoadFromStream(docStream, FileFormat.Auto);
foreach (var replacement in replacementsList)
{
doc.Replace(replacement.Key, replacement.Value, true, false);
}
return doc;
}
public byte[] ToByteArray(string targetFileName)
{
var fileExtension = targetFileName.Substring(targetFileName.LastIndexOf(".") + 1).ToLower();
var fileFormat = fileExtension == "pdf" ? FileFormat.PDF : doc.DetectedFormatType;
byte[] filebytes = null;
using (var stream = new MemoryStream())
{
doc.SaveToStream(stream, fileFormat);
filebytes = stream.ToArray();
}
return filebytes;
}
public void Dispose()
{
this.doc.Dispose();
}