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)