We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Speed up PDF report generation

Hi
I am using Aspose.Pdf for .NET
In my project I have template PDF (template1.pdf) and some controls on it.
I use this template to generate user report.
I have to open template file then generate report(user1.pdf) for user1, again I have to open template file and generate report (user2.pdf) for user2. I have to repeat it according of number of users.
Now I want to generate final report(AllUsers.pdf) by merging all user reports above generated.
My code is taking too much time to do all this process. It is taking around 30 mins. or more when report has only 65 pages.
Is there any optimized code so I can speed up my report generation process.

Thanks,
Pallavi.

@spallavi1

Thanks for contacting support.

Please note that the performance of API depends upon many factors like environment in which you are using API, application type and structure/complexity of the PDF documents. However, would you please share your sample PDF files with us along with sample code snippet, you are using to merge them. We will test the scenario in our environment and address it accordingly.

Please also share your environment details i.e. OS Version, Application Type, Target Framework Version, etc. Furthermore, in case your files are of more than 3.0MB, please upload them to some public file sharer like Dropbox or Google Drive and share the link with us.

In my project pdf generation is done in classic ASP code , so I created one class library in .NET to generate ASPOSE pdf.
This class library being call by ASP code using Interoperability.

I developed my class library using Visual Studio 2010.
I have 64 bit windows 10 operating system installed on my machine.
My code is following:
I call following functions (written in .NET class library) Method #1 to Method #5 from ASP code.
I call Method #1 and #5 only once for my report. But I have to call Method #2, #3 and #4 again and again.
(I have to open and fill template PDF again and again if I have multiple brokers, then merge with container PDF i.e. Main output PDF)
If there is need to add logo on my PDF report that time I call function PrintImage/ PrintImageLogo, AddLogo and SaveLogoPDF (I added logos on template PDF, which I attached here .)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Text;
using Aspose.Pdf;
using System.IO;
using System.Resources;
using System.Globalization;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;
using System.Web.Configuration;


[assembly: ApplicationName("AsposePDF_Library")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false,
    AccessChecksLevel = AccessChecksLevelOption.ApplicationComponent)]
namespace AsposePDF_Library
{
    interface IAsposeMethods
    {
       
        int OpenOutputFile(string outputFile);
        void OpenInputFile(string inputFile);
        void SetFormFieldData(string FieldName, string FieldValue, int FlagValue);
        int CopyForm(int StartPageNumber, int EndPageNumber);
        int CloseOutputfile(string outputFile);
        void SetFormFieldData_AutoID(string FieldName, string FieldValue, int FlagValue);
        void SetFlattenedFont(string FontType, int intFontSize, string strFieldName);
        void DeleteFormField(string FieldName);
        void AddLogo(string LogoFileName, bool IsLogoBackground);
        int PrintImage(string imageFileName, int lowerLeftX, int lowerLeftY, int upperRightX, int upperRightY, bool isImagePersist);
        int SaveLogoPDF(string fileToSave);
        int DeleteFile(string fileToDelete);
        void ClearLogosAndImages();
        void CloseInputFile();
        void ClearHeaderInfo();
        string getLicensePath();
        string getErrorLogFilePath();        
        string getCurrentAssemblyPath();
        int PrintImageLogo(string imageFileName, int lowerLeftX, int lowerLeftY, int upperRightX, int upperRightY, bool isImagePersist);

    }
    
    public class AsposeMethods:IAsposeMethods 
    {
        private Aspose.Pdf.Document outputPDF; //Output(container) PDF
        private Aspose.Pdf.Document templatePDF;  //to open Template PDF
        private Aspose.Pdf.Page objPage; //To get page from template PDF
        
        private Aspose.Pdf.Document pdfLogoDoc;  
        private Aspose.Pdf.Facades.Form objForm;
        private Aspose.Pdf.Facades.FormEditor objFormEditor;
        private Aspose.Pdf.Facades.FormFieldFacade  objFormFieldFacade;
        private Aspose.Pdf.Facades.PdfFileStamp objPDFFileStamp;
        private Aspose.Pdf.Facades.PdfContentEditor objPDFContentEditor;

        private static string genPDFPath;
        
      //Constructor
        public AsposeMethods()
        {

            try
            {

                objForm = new Aspose.Pdf.Facades.Form(); //Form object
                objFormEditor = new Aspose.Pdf.Facades.FormEditor();
                objFormFieldFacade = new Aspose.Pdf.Facades.FormFieldFacade();
                objPDFFileStamp = new Aspose.Pdf.Facades.PdfFileStamp();
                objPDFContentEditor = new Aspose.Pdf.Facades.PdfContentEditor();
                //// Create a PDF license object
                Aspose.Pdf.License objlicense = new Aspose.Pdf.License();
                objlicense.SetLicense(“D:/Aspose.Pdf.lic”);
            }
            catch (Exception ex)
            {
 
            }


        }

        //Method #1
//create object of container document(Main output PDF)
        public int OpenOutputFile(string outputFile)
        {

            try
            {
                outputPDF = new Aspose.Pdf.Document();
                genPDFPath = outputFile;
                return 1;
            }
            catch (Exception ex)
            {
 
            }
            
            
         }

        /// Method #2
 //Open template document 
        public void OpenInputFile(string inputFile)
        {
            try
            {
                templatePDF = new Aspose.Pdf.Document(inputFile);               
                objForm.BindPdf(templatePDF);
            }
            catch (Exception ex)
            {
            }
        }


        //Method #3
        public void SetFormFieldData(string FieldName, string FieldValue, int FlagValue)
        {
            
            try
            {

             
             objForm.FillField(FieldName, FieldValue);

             
                if (FlagValue == 0)                {
                   objFormEditor.BindPdf(templatePDF);
                               
                    objFormEditor.SetFieldAttribute(FieldName,Aspose.Pdf.Facades.PropertyFlag.ReadOnly);                    
           
                }
                if (FlagValue == -998) //Flatten Form field
                {

                    objForm.FlattenField(FieldName);                }
                if (FlagValue == -997)
                {
                    objForm.FlattenField(FieldName);                }
            }
            catch(Exception ex)
            {

            }
        }
	        //Method #4
		//Function copy pages from template PDF to Conatiner PDF;  according to page number value supplied to function. 
        public int CopyForm(int StartPageNumber,int EndPageNumber)
        {

            try
            {
                int templatePageCount, i;
                templatePageCount = templatePDF.Pages.Count;
                

                if(StartPageNumber==0 && EndPageNumber == 0) //copy all pages from template to container pdf
                   {
                    
                    for (i = 1; i <= templatePageCount; i++)
                    {
                        objPage = templatePDF.Pages[i];
                        outputPDF.Pages.Add(objPage);
                    }

                    }
                if (StartPageNumber != 0 && EndPageNumber != 0)  //copy template pages from start page number to end page number
                {
                    for (i = StartPageNumber; i <= EndPageNumber; i++)
                    {
                        objPage = templatePDF.Pages[i];
                        outputPDF.Pages.Add(objPage);
                    }
                }
                return 1;
            }
            catch(Exception ex)
            {
            }
        }

        //Method #5 
//Close container pdf (Save Main Output PDF)
        public int CloseOutputfile(string outputFile)
        {

          try
            {
                outputPDF.Save(outputFile);
                outputPDF.Dispose();
                objForm.Dispose();
                objFormEditor.Dispose();
                objPDFContentEditor.Dispose();
                return 1;
            }
            catch(Exception ex)
            {

            }
         }

//Image and logo related functions start here       
       
//Function is used to add Logo PDF on template PDF
        public void AddLogo(string LogoFileName, bool IsLogoBackground)
        {
            try
            {
                objPDFFileStamp.BindPdf(templatePDF);
                // Create stamp
                Aspose.Pdf.Facades.Stamp objStamp = new Aspose.Pdf.Facades.Stamp();
                objStamp.BindPdf(LogoFileName, 1);
                objStamp.IsBackground = IsLogoBackground;
                // Add stamp to PDF file
                objPDFFileStamp.AddStamp(objStamp);
            }
            catch(Exception ex)
            {
             
            }
       
   }

//following function used to add logo(.jpg/.bmp file) on pdf and set image size also. This logo PDF then used to add on template pdf using function AddLogo() above 
        public int PrintImage(string imageFileName, int lowerLeftX, int lowerLeftY, int upperRightX, int upperRightY, bool isImagePersist)
        {
            try
            {
            outputPDF.Pages.Add();
            Page page = outputPDF.Pages[1];

            ImageStamp imageStamp = new ImageStamp(imageFileName);
            imageStamp.Background = false;

            // Set the coordinate of the stamp
            imageStamp.XIndent = lowerLeftX;
            imageStamp.YIndent = lowerLeftY;

            page.AddStamp(imageStamp);
            return 1;
            }
            catch (Exception ex)
            {
             
            }
           
        }

//following function used to add logo(.jpg/.bmp file) on pdf and image size not set here . This logo PDF then used to add on template pdf using function AddLogo() above 
        public int PrintImageLogo(string imageFileName, int lowerLeftX, int lowerLeftY, int upperRightX, int upperRightY, bool isImagePersist)
        {
            try
            {
                // Get the page where image needs to be added
                outputPDF.Pages.Add();
                Page page = outputPDF.Pages[1];
                // Load image into stream
                FileStream imageStream = new FileStream(imageFileName, FileMode.Open);
                // Add image to Images collection of Page Resources
                page.Resources.Images.Add(imageStream);
                // Using GSave operator: this operator saves current graphics state
                page.Contents.Add(new Operator.GSave());
                //// Create Rectangle and Matrix objects
                Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
                Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
                //// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
                page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
                XImage ximage = page.Resources.Images[page.Resources.Images.Count];
                //// Using Do operator: this operator draws image
                page.Contents.Add(new Operator.Do(ximage.Name));
                //// Using GRestore operator: this operator restores graphics state
                page.Contents.Add(new Operator.GRestore());
                imageStream.Dispose();
                return 1;
            }
            catch (Exception ex)
            {
             
            }
        }

        public int SaveLogoPDF(string fileToSave)
        {
            try
            {
                pdfLogoDoc.Save(fileToSave);
                pdfLogoDoc.Dispose();
                return 1;
            }
            catch (Exception ex)
            {              

            }
        }

//Image and logo related functions end here

    }
}

PDF with 65 pages is getting near about 30 mins. to generate.
Please suggest some optimized solution so that I can speed up PDF generation.

Thanks,
Pallavi.

Brokerstatement.pdf (698.7 KB)

@spallavi1

Thanks for sharing the details.

We have tested the scenario by using your code snippet with Aspose.PDF for .NET 18.6 and were unable to notice delay in execution of the program. We have tested the code snippet in a console application targeting x64 mode of debugging, in VS 2017 Community and Windows 10 x64. Please check following screenshot of code and total time taken by the program.

PerformanceResults.png (41.0 KB)

Would you please share a sample console application, which is able to show the performance delay in our environment. We will again test the scenario in our environment and address it accordingly.

PS: In case you are using an older version of the API, please try using latest version as it comes with more improvements and fixes.

Hi,

I Have download latest version 18.5 of ASPOSE.Pdf.Dll.
However we are not able to use this in my application. We got error about mismatch between versions of ASPOSE.Pdf.Dll and License which we have purchased last year in Jun2017.

In our application , we generated PDF reports for around 100 pages. Aspose pdf implementation takes too much time to generate reports.
For few page reports, it generate quickly.

As suggested in ASPOSE forums to improve performance, we are trying to use latest version of ASPOSE pdf which seems not compatible with our license.
We are not able to generate PDFs and getting error when we use API versions greater than 17.6.
How to fill the gap between License version(Purchased on Jun2017) and latest API version?
Is there any way to make our license version and latest API version work together?

Please suggest.

Thanks & regards,
Pallavi.

@spallavi1

Thanks for your inquiry.

You can upgrade to any/latest release versions of API published before license subscription expiry. The license expiry date is specified in <SubscriptionExpiry>20170825</SubscriptionExpiry> tag, where 2017 is year, 08 is month and 25 is the date of month. However please note that the license file can be used for as long as you want (unless you are using it with API versions published before license expiry).

Would you please make sure about our above comments and in case you still face any issue, please share your license file in private message. We will test it in our environment and share our feedback with you.