Free Support Forum - aspose.com

ASPOSE.Word Save method is taking time in CPU Utilization

I am using ASPOSE.Word for converting word to pdf but Aspose save method is taking more CPU utilization.Could you please help me on this, because we are facing so much problem
and we are in a hurry situation. Could you please help us.

@santoshp1989,

Thanks for your inquiry. Have you tried the latest version of Aspose.Words for .NET i.e. 19.11 on your end? In case the problem still remains, please ZIP and upload your input Word document you are getting this problem with here for testing. We will then investigate the issue on our end and provide you more information.

Thank you Hafeez. But we are using Aspose.Word 18.5.0.0 and I think it is similar to 19.11.
Then also it is taking same CPU utilization like earlier.

Code:-

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
newDocument1.Range.Replace("<<" + column + “>>”, replacetext, findReplaceOptions);
newDocument1.AcceptAllRevisions();
newDocument1.Save(Path.Combine(outpath, outfilename + “_0.pdf”), SaveFormat.Pdf); // This line is taking more CPU
}
}

Could you please help why Save method is taking more CPU?

I am using ASPOSE.Word for converting word to pdf But Aspose save method is taking more CPU utilization.Could u please help me on this, because we are facing so much problem in this.And we are hurrying in situation please help us.

@santoshp1989

Thanks for your inquiry. We will appreciate it if you please confirm which API version are your using 1.1 or 4.0. Please also share your sample source document here, it will help us to investigate the issue and guide you.

If you are using v1.1, then we strongly recommend to use new improved API Version V4 in terms of memory management and API structure. The SDKs > 19.2 are using API Version V4, you can check release tab for the changes in later SDK versions.

Thank you Ahmad for your response. We are using Aspose.Word 18.5.0.0.

Code:-

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
newDocument1.Range.Replace("<<" + column + “>>”, replacetext, findReplaceOptions);
newDocument1.AcceptAllRevisions();
newDocument1.Save(Path.Combine(outpath, outfilename + “_0.pdf”), SaveFormat.Pdf); // This line is not taking more CPU
}
}

Could you please help us to find why Save method taking more CPU.

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
newDocument1.Range.Replace("<<" + column + “>>”, replacetext, findReplaceOptions);
newDocument1.AcceptAllRevisions();
newDocument1.Save(Path.Combine(outpath, outfilename + “_0.pdf”), SaveFormat.Pdf); // This line is taking more CPU
}
}
gravatar.png (52.0 KB)

Please check check the latest code spinet

@santoshp1989,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document you are getting this problem with
  • Please create a standalone simple console application (source code without compilation errors) that helps us to reproduce your 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 issue and provide you more information. Thanks for your cooperation.

public void MailMergeDocument(Doc ObjMain, DataTable dt, string Templatefilepath, string outpath, string outfilename, int RFSno = 0)
{
ClsLogging.LogMsg(message: “MailMergeDocument Method Started”, “”, “”, sessionid, null);

        bool ishavingmorepackages = false;
        Document newDocument1 = null;
        List<string> lstInserts = new List<string>();
        int Preprocessor = Convert.ToInt32(Convert.ToString(ConfigurationManager.AppSettings["Preprocessor"]));

        try
        {
            DataSet mmMappingDS = new DataSet();
            DataTable dataTable = new DataTable();
            DataView dataView;
            bool isOnboarding = false;
            string column = string.Empty;
            string replacetext = string.Empty;
            string tempfile = string.Empty;
            TextAnnotation textAnnotation = null;
            string fontfamily = "";
            double fontsize = 0;
            var findReplaceOptions = new FindReplaceOptions();

            try
            {
                if (RFSno > 0)
                {
                    ClsLogging.LogMsg(message: "GetMappingDataFields Started", "", "", sessionid, null);

                    mmMappingDS = GetMappingDataFields(RFSno);

                    ClsLogging.LogMsg(message: "GetMappingDataFields Ended", "", "", sessionid, null);

                    dataTable = mmMappingDS.Tables[0];

                    if (dataTable != null && dataTable.Rows.Count > 0)
                    {
                        ClsLogging.LogMsg(message: "This is an Onboarding Job", "", "", sessionid, null);
                        isOnboarding = true;
                    }

                    string tempMsg = "Checking it is two package job";
                    ClsLogging.LogMsg(message: tempMsg, "", "", sessionid, null);

                    if (twopackagerecurringfoldernosls.Contains(RFSno.ToString()))
                    {
                        ishavingmorepackages = true;
                    }
                    else
                    {
                        ishavingmorepackages = false;
                    }
                }
                else
                {
                    ishavingmorepackages = false;
                }
            }
            catch (Exception ex)
            {
                ClsLogging.LogMsg(message: "This is not Onboarding Job - Exception Details: ", Assembly.GetExecutingAssembly().ToString(), MethodInfo.GetCurrentMethod().ToString(), sessionid, ex);
            }

            ClsLogging.LogMsg(message: "Total no. of Package -> " + dt.Rows.Count, "", "", sessionid, null);

            ClsLogging.LogMsg(message: "Looping through Total no. of Packages Started -> " + dt.Rows.Count, "", "", sessionid, null);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lstInserts.Clear();

                ClsLogging.LogMsg(message: Path.Combine(outpath, "processlog.txt") + " Mail Merge Creation is Processing on Package No :' " + i.ToString() + " With Template :" + outfilename + "'  On: {0}", "", "", sessionid, null);

                tempfile = Path.GetFileNameWithoutExtension(Templatefilepath) + "_0" + Path.GetExtension(Templatefilepath);

                ClsLogging.LogMsg(message: "Reading Template File '" + tempfile + "' for mail merge - Started", "", "", sessionid, null);

                newDocument1 = new Document(outpath + @"\" + tempfile);

                ClsLogging.LogMsg(message: "Reading Template File '" + tempfile + "' for mail merge - Ended", "", "", sessionid, null);

                if ((i == 0) && (Preprocessor == 1))
                {
                    ////ClsLogging.LogMsg("DEMO:PREPROCESSOR START - " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

                    GetMostUsedFont(newDocument1, fontfamily, fontsize, findReplaceOptions);

                    ////ClsLogging.LogMsg("DEMO:PREPROCESSOR END - " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                }

                ClsLogging.LogMsg(message: "Reading Template File '" + tempfile + "' for mail merge - Ended", "", "", sessionid, null);

                ClsLogging.LogMsg(message: "Looping through Total no. of Columns for row no. -> " + i + " Started -> " + dt.Rows.Count, "", "", sessionid, null);

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

                    if (insertcolumnsls.Contains(column.ToLower()))
                    {
                        if (!string.IsNullOrEmpty(replacetext))
                        {
                            lstInserts.Add(Path.Combine(outpath, replacetext));
                        }
                    }
                    else
                    {
                        if (isOnboarding)
                        {
                            try
                            {
                                dataView = dataTable.DefaultView;

                                if (!string.IsNullOrEmpty(column))
                                {
                                    dataView.RowFilter = "Destination = '" + column + "'";
                                }

                                foreach (DataRowView rw in dataView)
                                {
                                    if (!string.IsNullOrEmpty(Convert.ToString(rw["MMDestination"])))
                                    {
                                        //var findReplaceOptions = new FindReplaceOptions();
                                        newDocument1.Range.Replace(Convert.ToString(rw["MMDestination"]).Trim(), replacetext, findReplaceOptions);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                //ClsLogging.LogMsg(message: "Exception Details: ", Assembly.GetExecutingAssembly().ToString(), MethodInfo.GetCurrentMethod().ToString(), sessionid, ex);
                            }
                        }
                        else
                        {
                            //FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
                            newDocument1.Range.Replace("<<" + column + ">>", replacetext, findReplaceOptions);
                        }
                    }
                }

                ClsLogging.LogMsg(message: "Looping through Total no. of Columns for row no. -> " + i + " Ended -> " + dt.Rows.Count, "", "", sessionid, null);

                ClsLogging.LogMsg(message: " AcceptAllRevisions Started -> " + dt.Rows.Count, "", "", sessionid, null);

                newDocument1.AcceptAllRevisions();
                
                ClsLogging.LogMsg(message: " AcceptAllRevisions Ended -> " + dt.Rows.Count, "", "", sessionid, null);

                ClsLogging.LogMsg(message: " Saving Template File - Started", "", "", sessionid, null);


                //if (File.Exists(Path.Combine(outpath, outfilename + "_0.pdf")))
                //{
                //    File.Delete(Path.Combine(outpath, outfilename + "_0.pdf"));
                //}

                //Aspose.Words.Saving.PdfSaveOptions saveOption =
                //    new Aspose.Words.Saving.PdfSaveOptions
                //    {

                //        SaveFormat = Aspose.Words.SaveFormat.Pdf,
                //        MemoryOptimization = true,

                //    };

                //Aspose.Words.Saving.SaveOptions objsaveop = Aspose.Words.Saving.SaveOptions.CreateSaveOptions(SaveFormat.Pdf);
                //objsaveop.MemoryOptimization = true;
                //objsaveop.UseHighQualityRendering = false;

                //if (!File.Exists(Path.Combine(outpath, outfilename + "_0.pdf")))
                //{
                //    newDocument1.Save(Path.Combine(outpath, outfilename + "_0.pdf"), SaveFormat.Pdf);
                //}

                newDocument1.Save(Path.Combine(outpath, outfilename + "_0.pdf"), SaveFormat.Pdf);// This line is talking more CPU


                ClsLogging.LogMsg(message: " Saving Template File - Ended", "", "", sessionid, null);

                ClsLogging.LogMsg(message: "using (Doc objTemp = new Doc()) -> Started", "", "", sessionid, null);

                using (Doc objTemp = new Doc())
                {
                    ClsLogging.LogMsg(message: " objTemp read - Started", "", "", sessionid, null);

                    objTemp.Read(Path.Combine(outpath, outfilename + "_0.pdf"));

                    ClsLogging.LogMsg(message: "  objTemp read - Ended", "", "", sessionid, null);

                    textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "Rec_Guid={" + (i + 1).ToString() + "}");

                    if (ishavingmorepackages && objTemp.PageCount > 1)
                    {
                        ClsLogging.LogMsg(message: "ishavingmorepackages Started", "", "", sessionid, null);

                        for (int ij = 1; ij <= objTemp.PageCount; ij++)
                        {
                            if (ij != 1)
                            {
                                objTemp.PageNumber = ij;
                                string pdfRawtext = objTemp.GetText(Page.TextType.Text, false);
                                if (pdfRawtext.Contains("|#|"))
                                {
                                    textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "Rec_Guid={" + (i + ij + 1).ToString() + "}");
                                }
                            }
                        }

                        ClsLogging.LogMsg(message: "ishavingmorepackages Ended", "", "", sessionid, null);
                    }

                    if (iscoverpageenable == "1" && conditionallogs == "2")
                    {
                        ClsLogging.LogMsg(message: "iscoverpageenable - iscoverpageenable == 1 && conditionallogs == 2 -> Started", "", "", sessionid, null);

                        if (dt.Columns.Contains(ClsAppSetting.LogoCondtionclmName.ToString()))
                        {
                            if (dt.Rows[i][ClsAppSetting.LogoCondtionclmName.ToString()].ToString().Trim() != "")
                            {
                                textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "coverpage={" + mailmergeoutputFilename + "_Coverpage_" + dt.Rows[i][ClsAppSetting.LogoCondtionclmName.ToString()].ToString() + ".pdf" + "}");
                            }
                            else
                            {
                                textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "coverpage={" + mailmergeoutputFilename + "_Coverpage.pdf" + "}");
                            }
                        }
                        else
                        {
                            textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "coverpage={" + mailmergeoutputFilename + "_Coverpage.pdf" + "}");
                        }

                        ClsLogging.LogMsg(message: "iscoverpageenable - iscoverpageenable == 1 && conditionallogs == 2 -> Ended", "", "", sessionid, null);
                    }
                    else if (iscoverpageenable == "1" && (conditionallogs == "0" || conditionallogs == "1"))
                    {
                        ClsLogging.LogMsg(message: "iscoverpageenable - (iscoverpageenable == 1 && (conditionallogs == 0 || conditionallogs == 1)) -> Started", "", "", sessionid, null);

                        textAnnotation = new TextAnnotation(objTemp, "0 0 0 0", "0 0 0 0", "coverpage={" + mailmergeoutputFilename + "_Coverpage.pdf" + "}");

                        ClsLogging.LogMsg(message: "iscoverpageenable - (iscoverpageenable == 1 && (conditionallogs == 0 || conditionallogs == 1)) -> Ended", "", "", sessionid, null);
                    }

                    if (plextype.ToUpper() == "D")
                    {
                        ClsLogging.LogMsg(message: "Handeling plextypes", "", "", sessionid, null);

                        ClsLogging.LogMsg(message: "ConverttoDuplex Started", "", "", sessionid, null);

                        ConverttoDuplex(objTemp);

                        ClsLogging.LogMsg(message: "ConverttoDuplex Ended", "", "", sessionid, null);
                    }

                    ClsLogging.LogMsg(message: "Append - ObjMain -> Started", "", "", sessionid, null);

                    ObjMain.Append(objTemp);

                    ClsLogging.LogMsg(message: "Append - ObjMain -> Ended", "", "", sessionid, null);

                    objTemp.Clear();

                    if (lstInserts == null || lstInserts.Count == 0)
                    {
                        ClsLogging.LogMsg(message: "You do n't have any inserts to add for this Order -> ", "", "", sessionid, null);
                    }

                    if (lstInserts != null && lstInserts.Count > 0)
                    {
                        ClsLogging.LogMsg(message: "Inserts Adding started", "", "", sessionid, null);

                        lstInserts.ForEach(a =>
                        {
                            if (System.IO.File.Exists(a))
                            {
                                string fileext = GetExtension(a);

                                if (fileext == "PDF")
                                {
                                    objTemp.Read(a);

                                    if (plextype.ToUpper() == "D")
                                    {
                                        ConverttoDuplex(objTemp);
                                    }

                                    ObjMain.Append(objTemp);
                                    objTemp.Clear();
                                }
                                else if (fileext == "JPEG" || fileext == "JPG" || fileext == "TIFF" || fileext == "TIF")
                                {
                                    if (File.Exists(a))
                                    {
                                        using (XImage theImg = new XImage())
                                        {
                                            theImg.SetFile(a);
                                            objTemp.Rect.Left = 50;
                                            objTemp.Rect.Bottom = 50;

                                            if (theImg.Height > 792.0F)
                                            {
                                                objTemp.Rect.Height = 500;
                                            }
                                            else
                                            {
                                                objTemp.Rect.Height = theImg.Height;
                                            }
                                            if (theImg.Width > 612.0F)
                                            {
                                                objTemp.Rect.Width = 500;
                                            }
                                            else
                                            {
                                                objTemp.Rect.Width = theImg.Width;
                                            }

                                            objTemp.AddImageObject(theImg, false);
                                        }

                                        if (plextype.ToUpper() == "D")
                                        {
                                            ConverttoDuplex(objTemp);
                                        }

                                        ObjMain.Append(objTemp);
                                        objTemp.Clear();
                                    }
                                }
                            }
                        });

                        ClsLogging.LogMsg(message: "Inserts Adding completed", "", "", sessionid, null);
                    }
                }

                ClsLogging.LogMsg(message: " using (Doc objTemp = new Doc()) -> Ended", "", "", sessionid, null);
            }

            ClsLogging.LogMsg(message: " Looping through Total no. of Packages Ended -> " + dt.Rows.Count, "", "", sessionid, null);

            ClsLogging.LogMsg(message: " MailMergeDocument Method Ended", "", "", sessionid, null);
        }
        catch (Exception ex)
        {
            ClsLogging.LogMsg(message: " Exception Details: ", Assembly.GetExecutingAssembly().ToString(), MethodInfo.GetCurrentMethod().ToString(), sessionid, ex);
            throw;
        }
    }

Thank you so much.
I am really very sorry but I can’t give entire souce code in Console application.
Please find the above method where you can find Save method that is taking time.

Please find attachment where you can find word file which I am trying.

1511201973725285-11.zip (316.2 KB)

Any update ASPOSE.
We are in trouble.
Please help us.

@santoshp1989,

But, we have also fixed many bugs and introduced so many improvements during transition from 18.5 version to the latest version of Aspose.Words i.e. 19.11. So, please upgrade to the latest version of Aspose.Words for .NET i.e. 19.11 and see how it goes on your end. Hope, this helps.

In case the problem still remains, please create a simplified standalone simple console application (source code without compilation errors) that helps us to reproduce your 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. Thanks for your cooperation.

Thanks for your suggestion Hafeez, today I tried with upgraded version of Aspose.Words for .NET 19.11 but same result as expected. It is still killing the CPU.

Could you please help us. We are in a Critical situation.

@santoshp1989,

Unfortunately, it is difficult to say what the problem is without code example and documents. Please share the simple console application (source code without compilation errors) that helps us to reproduce your problem on our end. We will investigate the issue and provide you more information on it. Thanks for your cooperation.

Thanks for your quick response Tahir.

But I tried to upload the code in a zip file but it is allowing to upload.
Please check the image for your reference.
image.png (2.6 KB)

But I able to attach the documents what I am using in the code. Below is the attachment:-
1511201973725285-111.zip (184.2 KB).

Code:- As I am unable to upload the zip Console application below is the code:-

class Program
{
static void Main(string[] args)
{
string tempfile = string.Empty;
string column = string.Empty;
string replacetext = string.Empty;

        string csvPath = @"D:\AnthemDCC_NEW\3_MAILMERGE\PROCESS\1511201973725285-111";
        string templatePath = @"D:\AnthemDCC_NEW\3_MAILMERGE\PROCESS\1511201973725285-111";

        string csvFilePath = @"D:\AnthemDCC_NEW\3_MAILMERGE\PROCESS\1511201973725285-111\data.csv";
        string templateFilePath = @"D:\AnthemDCC_NEW\3_MAILMERGE\PROCESS\1511201973725285-111\template.docx";

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

        var findReplaceOptions = new FindReplaceOptions();

        Document newDocument1 = null;

        tempfile = templateFilePath;

        Document StatnewDocument1 = new Document(templateFilePath);

        Doc ObjMain = new Doc();

        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(Path.Combine(templatePath, Path.GetFileNameWithoutExtension(templateFilePath) + ".pdf"), SaveFormat.Pdf);// This line is talking more CPU

            using (Doc objTemp = new Doc())
            {
                objTemp.Read(Path.Combine(templatePath, Path.GetFileNameWithoutExtension(templateFilePath) + ".pdf"));

                ObjMain.Append(objTemp);

                objTemp.Clear();
                
            }
        }
    }

    public static DataTable ConvertCSVtoDataTable(string strFilePath, string delimiter)
    {
        DataTable csvData = new DataTable();

        try
        {
            using (TextFieldParser csvReader = new TextFieldParser(strFilePath))
            {
                csvReader.SetDelimiters(new string[] { delimiter });
                csvReader.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader.ReadFields();

                foreach (string column in colFields)
                {
                    DataColumn datecolumn = new DataColumn(column);
                    datecolumn.AllowDBNull = true;
                    csvData.Columns.Add(datecolumn);
                }

                while (!csvReader.EndOfData)
                {
                    string[] fieldData = csvReader.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData.Length; i++)
                    {
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }
                    }
                    csvData.Rows.Add(fieldData);
                }
            }
        }
        catch (Exception ex)
        {
        }
        return csvData;
    }
}

Dll used:-

  1. Aspose
  2. AbcPdf
  3. Microsoft.VisualBasic

Could I expect the quick response as earlier for the above email.

Most expensive line of code:-

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

@santoshp1989,

The code you shared in your previous post is causing a few compile time errors on our end. For example, we are unable to resolve assembly reference of type or namespace TextFieldParser on our end. It would be great if you please create a simplified standalone simple console application (source code without compilation errors) that helps us to reproduce your 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. You can ZIP the project and attach it. You can also upload the ZIP file to Dropbox and share the Download link here for testing. Thanks for your cooperation.

Have you also tried the latest version of Aspose.Words for .NET i.e. 19.12 on your end. Instead of saving to PDF, please save the final output in DOCX format, ZIP it and share the DOCX file here for further testing.

Thank you Hafeez for your support.
For fixing TextFieldParser reference type or namespace build error, you need to download
Microsoft.VisualBasic dll from Nuget package manager and even you need to download AbcPdf as well to to fix build error.

I tried with the latest version of Aspose.Words for .NET 19.12 dll from my end but still it is killing the CPU.

Please find the below for final output in docx extension with ZIP file for your further testing.

template_Final.zip (390.4 KB)

Note:- We are in a Critical situation. Could you please help us ASPOSE.

@santoshp1989,

Please try running the following project on your end:

This project uses latest versions of Aspose.Words, AbcPdf and Microsoft.VisualBasic libraries and produces the following output on our end:

  • Total Time the whole project takes is 4598 milliseconds
  • Peak memory usage is 68.2 Mb