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.

Tue May 17, 2022 9:13 am

Hello, I am trying to clone the sections from one document to a new one like this:

Code: Select all
output.Sections.Add(source.Sections[0].Clone())

(output and source are both Spire.Doc Documents but output always returns with a count of 0)

And the same error is always returned:

System.NullReferenceException: Object reference not set to an instance of an object.
at Spire.Doc.Documents.Style.?(String A_0)
at Spire.Doc.Documents.Style.?(Document A_0, Document A_1)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Documents.Paragraph.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.TableCell.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.TableRow.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Table.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Section.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Collections.DocumentObjectCollection.OnInsert(Int32 index, DocumentObject entity)
at Spire.Doc.Collections.DocumentObjectCollection.Add(IDocumentObject entity)

From the stack trace this error was also displayed:
output.Sections.Add(source.Sections); error CS1503: Argument 1: cannot convert from 'Spire.Doc.Collections.SectionCollection' to 'Spire.Doc.Interface.ISection'


I appreciate any help you may provide and thank you as always.

Kind regards,

Adrian

adrianchan
 
Posts: 13
Joined: Fri Dec 10, 2021 2:08 am

Tue May 17, 2022 10:09 am

Hi Adrian,

Thanks for your inquiry.

To help us investigate this issue further, could you please provide the Doc file and test code related to this issue with us. You can upload them here or send them to us via email(support@e-iceblue.com). Thanks in advance for your assistance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue May 17, 2022 10:55 am

Hi Andy,

Below is my code:

Code: Select all
public static Tuple<string, Document> generateAutoLabel(Dictionary<string, string> model, Document source, Document output, string outputFileName)
        {
            try
            {
                source.CloneWebSettingsTo(output);
                source.CloneDefaultStyleTo(output);
                source.CloneThemesTo(output);
                source.CloneCompatibilityTo(output);
                Dictionary<string, string> wordsToReplace = dictionaryModel("", model);
                if (wordsToReplace.Count != 0)
                {
                    log.Info("source count in generateAutoLabel: " + source.Sections.Count);
                    output.Sections.Add(source.Sections[0].Clone());  <---- exception appears here

                    foreach (KeyValuePair<string, string> entry in wordsToReplace)
                    {
                        output.Replace(entry.Key, entry.Value, true, true);
                    }
                }
                else
                {
                    log.Info("replace fail ");
                }
                return Tuple.Create(outputFileName, output);
            }
            catch (Exception ex)
            {
                log.Info("replace fail at " + ex.ToString());
                return Tuple.Create(outputFileName, output);
            }
        }


Source is the word document I am trying to copy and Output is a newly declared Spire.Doc.Document(). The dictonaryModel just creates a dictionary of strings though I am unable to give you the code to the function. If you require anything else please let me know.

Kind regards,

Adrian

adrianchan
 
Posts: 13
Joined: Fri Dec 10, 2021 2:08 am

Wed May 18, 2022 2:31 am

Hi Adrian,

Thanks for your sharing.

I tried your code and the document but I did not encounter the error you mentioned. The result file can be generated successfully(see the screenshot).
ACDF8578-4CAF-458d-B5D8-52D4D3E914C4.png

Since the dictionaryModel cannot compile directly, and it did not seem to be related to the code where the exception occurred, I removed this part from the test code. Below is the modified test code.
Code: Select all
        public static Tuple<string, Document> generateAutoLabel( Document source, Document output, string outputFileName)
        {
            try
            {
                source.CloneWebSettingsTo(output);
                source.CloneDefaultStyleTo(output);
                source.CloneThemesTo(output);
                source.CloneCompatibilityTo(output);

                output.Sections.Add(source.Sections[0].Clone());

                return Tuple.Create(outputFileName, output);
            }
            catch (Exception ex)
            {
                return Tuple.Create(outputFileName, output);
            }
        }
        static void Main(string[] args)
        {
            setLicenseKey();
            Document doc = new Document();
            doc.LoadFromFile("serialserial.docx");
            Document doc2 = new Document();
            Tuple<string, Document>  s=generateAutoLabel(doc, doc2, "output.docx");
            s.Item2.SaveToFile(s.Item1,FileFormat.Docx);
            System.Diagnostics.Process.Start(s.Item1);
        }

Also, if the Spire.Doc you are currently using is not the latest 10.4.6 version, please update it from nuget and test again.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 18, 2022 11:04 am

Hi Andy,

Unfortunately I am still unable to identify the problem, also a new error it seems to have showed up at .CloneDefaultStyleTo(). By the way, I am using FreeSpire.Doc 10.2.5.0 so I am not sure if that is the issue.

Kind regards,

Adrian

adrianchan
 
Posts: 13
Joined: Fri Dec 10, 2021 2:08 am

Thu May 19, 2022 9:02 am

Hi Adrian,

Thanks for your feedback.

As the free version is updated less frequently, many fixes and optimizations may not be included. So I suggest you switch to Spire.Doc latest commercial version 10.4.6 for testing first.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Thu Jun 02, 2022 2:42 am

Dear Andy,

As per your last reply, we have switched to use the commercial version (now 10.5.9) but the issue still persists.

After using Close() and Dispose() to free the system memory, the NullReferenceException error occurs whenever we try to add the cloned existing document to the new document, and although the issue does not occur when we use .Close() and .Dispose(), all the RAM will be consumed as we attempt to generate 2000+ documents. Any other advice regarding memory management would be appreciated.

Kind regards,

Adrian

adrianchan
 
Posts: 13
Joined: Fri Dec 10, 2021 2:08 am

Thu Jun 02, 2022 10:04 am

Hi Adrian,

Thanks for your feedback.

Memory consumption is closely related to the size and number of documents processed. Therefore, you need to control the size and number of the documents.
Also, regarding the exception you're getting while adding the cloned existing document to the new document, could you please provide a simple test project so we can reproduce it? Thanks in advance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc