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.

Wed Apr 20, 2022 8:39 am

Hi,

I have come across the method seen below but cannot find any documentation for how it behaves. Can you please provide some information on this method because it seems useful for me but I want to be able to understand it before I use it.

Code: Select all
document.deepClone()


Thanks

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Wed Apr 20, 2022 9:26 am

Hello,

Thank you for your inquiry.
Are you trying to clone a Word document? If yes, please refer to the code below. If not, please provide more details. Thanks in advance.
Code: Select all
        //Create Word document.
        Document document = new Document();
        //Load the file from disk.
        document.loadFromFile("inputFile");
        //Clone the word document.
        Document newDoc = document.deepClone();
        String result = "outputFile";
        //Save the file.
        newDoc.saveToFile(result, FileFormat.Docx_2013);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Wed Apr 20, 2022 6:20 pm

Hi,

Thanks for your reply.

I am currently using the method and am fully aware that it clones a document, therefore, the code snippet provided does not really help me. I would really appreciate it if there was some more in depth documentation explaining what this method actually does beneath the surface so that I can be aware of what to expect from it.

Thanks

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Thu Apr 21, 2022 3:21 am

Hi,

We are sorry that we don't have a depth documentation explaining what document.deepClone() method actually does beneath the surface. Could you please tell us what do you do next with this method? What are your concerns? We'll help you out.

Sincerely,
Amy
E-icelue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Apr 27, 2022 3:06 pm

Hi,

We've noticed that when creating many hundreds of documents it is more memory efficient to reuse a Document object by deep cloning it each time as opposed to instantiating a new Document object. Please see the below example to illustrate this. We are not sure if this is a known benefit of deep clone and if what we are doing is recommended when creating a large number of documents. This is why some detailed documentation would help us to understand the situation better.

Code: Select all
   document = new Document();
   InputStream inputStream = App.class.getClassLoader().getResourceAsStream("template.docx");
   document.loadFromStream(inputStream, FileFormat.Docx);

   for (int i = 0; i < 701; i++) {
       Document invoiceDoc = document.deepClone();

       //merge data

       try (OutputStream outputStream = new FileOutputStream(String.format("%s.pdf", i))) {
           invoiceDoc.saveToStream(outputStream, FileFormat.PDF);
       } catch (Exception exception) {
           exception.printStackTrace();
       }
   });


Thanks

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Thu Apr 28, 2022 7:32 am

Hello,

Thanks for your feedback.
Based on the code you provided, we found that it is not that the deep clone method is more memory efficient than instantiating a new Document object. In addition, there are some uncontrollable stylistic errors using the deep clone method, we do not recommend you to use the deep clone method. Therefore, please modify your code to the following code.
Code: Select all
for (int i = 0; i < 701; i++) {
Document document = new Document();
InputStream inputStream = App.class.getClassLoader().getResourceAsStream("template.docx");
document.loadFromStream(inputStream, FileFormat.Docx);

//merge data

 try (OutputStream outputStream = new FileOutputStream(String.format("%s.pdf", i))) {
invoiceDoc.saveToStream(outputStream, FileFormat.PDF);
} catch (Exception exception) {
     exception.printStackTrace();
     }
});

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Fri Apr 29, 2022 8:31 am

Hi,

We noticed the better memory performance when dealing with this issue: https://www.e-iceblue.com/forum/viewtopic.php?forum_uri=java-spire-doc-memory-settings&t=11032&start=. If the errors are only stylistic then we are fine with that since there is no other option for us at the moment until the aforementioned issue is fixed.

Thanks

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Fri Apr 29, 2022 9:34 am

Hello,

Thanks for your feedback.
For the multi-threading issue, I suggest you use thread pool to avoid memory overflow. If there is any question, please feel free to write back.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc