Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu May 08, 2025 5:31 pm

I'm cloning a number of documents into a source document and i want the documents that I am cloning to restart the first numbered list once they have been added to the document.
newSection in the below code is the section which has been added to the source document.

Code: Select all
foreach (Paragraph para in newSection.Paragraphs)
{
    if (para.ListFormat.ListType != ListType.NoList)
    {
        para.ListFormat.IsRestartNumbering = true;
    }
}               


I can get to the list items, but using para.ListFormat.IsRestartNumbering = true; doesnt seem to do anything. The document i am cloning has quite a few list levels so i dont want to ave to recreate the list style if i dont have to.
I've seen this forum post - restart-numbering-on-a-list-t6220.html but i dont want to have to create a new list style if i can help it, can i edit the existing liststyle?

Thanks
Dan

danhigham
 
Posts: 4
Joined: Tue May 17, 2022 2:08 pm

Fri May 09, 2025 7:45 am

Hello,

Thanks for your inquiry.
Please refer to the following code to merge documents and confirm whether it meets your needs. If not, to help us investigate further, please provide us with your test files and screenshots to show the effect you want to achieve. You can upload them to the attachment or send them to this email [email protected]. Thanks in advance.
Code: Select all
static void Main(string[] args)
{
    List<String> filesFullPath = new  List<String>();
    //add files
    filesFullPath.Add("add1.docx");
    filesFullPath.Add("add2.docx");

    Document doc = new Document();
    doc.AddSection();
    //merge files
    mergeFiles(doc, filesFullPath);
    String fileNameout = "target.docx";
    doc.SaveToFile(fileNameout);
    doc.Dispose();
}
/*
 The method of merging documents
 */
public static Document mergeFiles(Document doc, List<String> filesFullPath)
{
    //The body of the last section(target document)
    DocumentObjectCollection childObjects = doc.LastSection.Body.ChildObjects;
    //merge files
    for (int i = 0; i < filesFullPath.Count; i++)
    {
        String mergeFileFullPath = filesFullPath[i];
        Document newDoc = new Document(mergeFileFullPath);
        newDoc.KeepSameFormat = true;
        SectionCollection newdocSections = newDoc.Sections;
        for (int n = 0; n < newdocSections.Count; n++)
        {
            DocumentObjectCollection obj = newdocSections[n].Body.ChildObjects;
            for (int i1 = 0; i1 < obj.Count; i1++)
            {
                childObjects.Add(obj[i1].Clone());
            }
        }
        newDoc.Dispose();
    }
    return doc;
}


Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Tue May 13, 2025 5:38 pm

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();
                        }
                    }


danhigham
 
Posts: 4
Joined: Tue May 17, 2022 2:08 pm

Wed May 14, 2025 5:48 am

Hello,

Thank you for your feedback.
To help us investigate your issue further,please send your document to email [email protected]. Thanks in advance.

Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Wed May 14, 2025 4:58 pm

I have emailed the file to you, thanks. Dan

danhigham
 
Posts: 4
Joined: Tue May 17, 2022 2:08 pm

Thu May 15, 2025 8:44 am

Hello,

Thank you for your feedback.
I have reproduced the issue you mentioned and logged it to our tracking system with the ticket SPIREDOC-11260. Our development team will further investigate and fix it. Once there is any progress, we will inform you in time. Sorry for the inconvenience caused to you.

Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Mon May 19, 2025 4:19 pm

If it is at all useful, the issue seems to be related to where styles are linked to a template file and set to automatically update. If i unselect automatically update, and then import the styles manually the issue goes away. Obviously, it would be useful to have the ability to use linked styles.

danhigham
 
Posts: 4
Joined: Tue May 17, 2022 2:08 pm

Tue May 20, 2025 3:32 am

Hello,

Thank you for your feedback.
As you mentioned, performing your previous obj.clone() method after importing styles from an external source works fine. I've reported this behavior to the development team. Thanks for your additional input.If you have any other questions later, please contact us in time. Thank you in advance.

Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Mon Jun 09, 2025 2:54 am

Hello Dan,

Glad to inform you that the reported bug SPIREDOC-11260 has been resolved and the new version (Spire.Doc Pack(hot fix) Version:13.5.11) is available. Please download it from the following link.
Websit download link:
https://www.e-iceblue.com/Download/down ... t-now.html
Nuget download link:
https://www.nuget.org/packages/Spire.Doc/13.5.11
https://www.nuget.org/packages/Spire.Do ... rd/13.5.11

Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Return to Spire.Doc