Thanks for the code, I've updated and I am getting the desired result, thanks.
This method though does cause an error with one of the documents i am merging (it works fine with other documents). The error is shown in the attachment. If i clone the whole section rather than the childobjects in the sections this error does not occur. The line which errors is
- Code: Select all
childObjects.Add(obj[i1].Clone());
I can provide a copy of the file but would need to email it rather than uploading here.
The full code is below, I'm fetching each document (documentTerms) from a remote API, and merging it into an existing document (document)
- Code: Select all
for (int termsitem = 0; termsitem < filteredterms.Count; termsitem++)
{
using (var response = await httpClientv2.GetAsync(baseURLv2
+ "io-blob?filestore=filestore"
+ "&document_type=" + filteredterms[termsitem].document_type
+ "&document_id=" + filteredterms[termsitem].document_id
+ "&document_name=" + filteredterms[termsitem].document_name
+ "&tenant_id=" + filteredterms[termsitem].tenant_id
+ "&function_type=download"
+ "&prod=" + environment))
{
var ms = new MemoryStream();
var content = response.Content.CopyToAsync(ms);
Spire.Doc.Document documentTerms = new Document();
documentTerms.LoadFromStream(ms, Spire.Doc.FileFormat.Docx2019);
int lastSection = document.Sections.Count - 1;
documentTerms.KeepSameFormat = true;
float docTop = documentTerms.Sections[0].PageSetup.Margins.Top;
float docBottom = documentTerms.Sections[0].PageSetup.Margins.Bottom;
float docLeft = documentTerms.Sections[0].PageSetup.Margins.Left;
float docRight = documentTerms.Sections[0].PageSetup.Margins.Right;
float footerDistance = documentTerms.Sections[0].PageSetup.FooterDistance;
SectionCollection terms_sec = documentTerms.Sections;
for (int n = 0; n < terms_sec.Count; n++)
{
Paragraph termspara = document.Sections[lastSection - 1].AddParagraph();
Section newSection = termspara.InsertSectionBreak(SectionBreakType.NewPage);
newSection.PageSetup.Margins.Top = docTop;
newSection.PageSetup.Margins.Bottom = docBottom;
newSection.PageSetup.Margins.Left = docLeft;
newSection.PageSetup.Margins.Right = docRight;
newSection.PageSetup.FooterDistance = footerDistance;
DocumentObjectCollection childObjects = newSection.Body.ChildObjects;
newSection.HeadersFooters.LinkToPrevious = false;
newSection.HeadersFooters.Header.ChildObjects.Clear();
HeaderFooter header = terms_sec[n].HeadersFooters.Header;
foreach (DocumentObject headerobj in header.ChildObjects)
{
newSection.HeadersFooters.Header.ChildObjects.Add(headerobj.Clone());
}
newSection.HeadersFooters.Footer.ChildObjects.Clear();
HeaderFooter footer = terms_sec[n].HeadersFooters.Footer;
foreach (DocumentObject footerobj in footer.ChildObjects)
{
newSection.HeadersFooters.Footer.ChildObjects.Add(footerobj.Clone());
}
DocumentObjectCollection obj = terms_sec[n].Body.ChildObjects;
for (int i1 = 0; i1 < obj.Count; i1++)
{
childObjects.Add(obj[i1].Clone());
}
}
documentTerms.Dispose();
ms.Dispose();
}
}
Login to view the files attached to this post.