ASPOSE.Word Save method is taking time in CPU Utilization

Thank you Hafeez for your confimation.
But the problem will come once after you increase the row count in the CSV file.

Could you please increase the row count to 1000 and check.

The issue is coming when the csv file having more rows count.

Probelm is, it is killing the CPU.

I am sure, the below only killing the CPU:-

newDocument1.Save(Path.Combine(templatePath, Path.GetFileNameWithoutExtension(templateFilePath) + “.pdf”), SaveFormat.Pdf);// This line is talking more CPU

Could you please help us.

@santoshp1989,

You could simply avoid this problem by taking this newDocument1.Save("19.11.pdf", SaveFormat.Pdf); line out of the for loop. Please check the following code:

Stopwatch sw = new Stopwatch();
sw.Reset();
sw.Start();

License lic = new License();
lic.SetLicense("Aspose.Total.Product.Family.lic");

string tempfile = string.Empty;
string column = string.Empty;
string replacetext = string.Empty;

string csvFilePath = "data.csv";
string templateFilePath = "template.docx";

DataTable dt = ConvertCSVtoDataTable(csvFilePath, ";");

var findReplaceOptions = new FindReplaceOptions();
Document newDocument1 = null;

tempfile = templateFilePath;

Document StatnewDocument1 = new Document(templateFilePath);

for (int i = 0; i < dt.Rows.Count; i++)
{
    newDocument1 = null;
    newDocument1 = StatnewDocument1.Clone();

    for (int j = 0; j < dt.Columns.Count; j++)
    {
        column = Convert.ToString(dt.Columns[j]).Trim();
        replacetext = Convert.ToString(dt.Rows[i][j]).Trim();

        newDocument1.Range.Replace("<<" + column + ">>", replacetext, findReplaceOptions);
    }

    newDocument1.AcceptAllRevisions();                                
}

newDocument1.Save("19.11.pdf", SaveFormat.Pdf);// This line is talking more CPU

sw.Stop();
Console.WriteLine(string.Format("Total time: {0} ms", sw.ElapsedMilliseconds));
Console.WriteLine(string.Format("Peak memory: {0} Mb", Process.GetCurrentProcess().PeakWorkingSet64 / (1024f * 1024f)));

Hope, this helps.

Thank you Hafeez. Let me check with the solution what you given above.
What newDocument1.AcceptAllRevisions(); code does?
Will it save also in the word document?

Could you please help us giving code for:-
How to use both Aspose.Word and Aspose.Pdf in same Console Application?

Please help us.

Is the below code is fine:-
Aspose.Words.License licence = new Aspose.Words.License();
licence.SetLicense(Convert.ToString(ConfigurationManager.AppSettings[“AsposeLic”]));

Aspose.Pdf.License licencepdf = new Aspose.Pdf.License();
licencepdf.SetLicense(Convert.ToString(ConfigurationManager.AppSettings[“AsposeLic”]));

Hafeez, We struggling to use both the Licenses(Aspose.Word and Aspose.Pdf) in same Console Application but our application is not working in Development Server. It is working fine in local system. When trying to move to Severs like Dev or QA then it is not working.

Could you please help us quickly on this.

Hafeez, We are having Aspose.Total 18.5 license.
Is it covering both Aspose.Word and Aspose.Pdf?

Please help us.

Any update Aspose?

@santoshp1989,

Please check Document.AcceptAllRevisions Method

Secondly, you need to set the license separately for each Aspose API that you are using. For example, if you are using Aspose.Words and Aspose.PDF, code for setting the license will look like this:

Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense(MyDir + @"Aspose.Total.Product.Family.lic");

Aspose.Pdf.License pdflic = new Aspose.Pdf.License();
pdflic.SetLicense(MyDir + @"Aspose.Total.Product.Family.lic");

You can simply install Aspose.Words and Aspose.PDF libraries in your console application via NuGet:

Hope, this helps.

Firstly, I have not tried yet newDocument1.AcceptAllRevisions();. I will try and let you know.

Secondly, We have done the same what you suggested above but that is also not working.

Please check the code below:-

Aspose.Pdf.License pdflic = new Aspose.Pdf.License(); pdflic.SetLicense(“Aspose.Total.lic”);

Aspose.Words.License licence = new License();
licence.SetLicense("“Aspose.Total.lic”");

Could you please suggest, how to solve this issue ASAP.

We are having 18.5 Aspose.Total.lic license.

Will that includes both Aspose.Word and Aspose.Pdf dll?

Could you please help us how to use both the above two dll in a same Console application.

If possible, could you please provide code sample for this?

Could you please give the solution as early as possible.
Our dead line is tomorrow.

Please help us.

@santoshp1989,

Yes, Aspose.Total.lic file should work with both Aspose.Words and Aspose.PDF libraries. Depending upon the expiry date of your license, please use the appropriate versions of Aspose.APIs.

Please download and build the following project to see how to set license for Aspose.Words and Aspose.PDF APIs in the same Console Application.

Hope, this helps.

Hi Hafeez,

We can not do like this. We can not move the Save to Pdf part code (newDocument1.Save) outside of the loop.

Because, after saving to pdf we need to have add some pdf files and annotations.
Those are our client requirement.

MailMerge Steps:-

Step 1:- Read the csv file and convert the csv file to DataTable. DataTable may have 100000 records.
Step 2:- For one record there will be one pdf(Insert) file and one annotation that need to add after MailMege in the word file.

As per the above steps, we can’t move the “Save” code outside of the loop.

The below line only taking more time.
newDocument1.Save(“19.11.pdf”, SaveFormat.Pdf);

Could you please help us to find any other solutions.

Thanks,
Santosh Kumar Panigrahi

@santoshp1989,

Each time you render Document to PDF format in loop, Aspose.Words will need to rebuild the layout of document which is relatively slower process. Roughly during rendering to PDF, Aspose.Words layouts 10 pages per second; so, the extra amount of time Aspose.Words may take to format document into pages depends on the number of pages your Word document has. Also, the process of converting Word document to fixed-page formats such as PDF/XPS or images is not linear; it may take a minute to render one page and may take a few seconds to render 100 pages (it depends upon the document complexity). I think, you can improve the total processing time by incorporating multithreading in your application. Hope, this helps.

Hi Hafeez,

Thank you for your quick response.

Definitely we will implement multi-threading and will check the processing time.

But I have a concern, before the loop only I am converting and Saving word to Pdf and trying to MailMerge the fields in the Pdf only.

It is working fine but Overflow issue is coming.

Let say <<FirstName>> field is there in Word file and the data is there in Excel file. If <<FirstName>> is the last word of the sentence and data is big “mshfjdsahfjdhjshfjdshsjhfvsdfvdskhf” then it is overflowing the document.

If this overflow problem is resolved then we got the solution for Processing time issue.

Could you please help on this issue.

Thanks,
Santosh Kumar Panigrahi

@santoshp1989,

For the ‘text overflowing the document’ problem, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 20.1 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word and attach it here for our reference
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.

Hi Hafeez,

Thank you for your quick response.

I am sorry but “text overflow” issue i am not getting in Word file. The problem is coming in Pdf file.

Actually we are facing Processing time issue in that loop right. So, before loop only we are saving the word file to Pdf file and appending the Pdf’s.

Earlier the MailMerge was happening in word right. Now, we are doing it in Pdf.
So, in Pdf we are getting “Text Overflow” issue not in the Word document.

Could you please help on how to do MailMerge in Pdf file without “Text Overflow” issue.

Thanks,
Santosh Kumar Panigrahi

@santoshp1989,

Sure, instead of output DOCX file, please provide the output PDF file showing the undesired behavior along with the resources requested in my previous post here for testing. Thanks for your cooperation.