Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
We are doing it since last 3 years to create pdf. but it take too much time ( approx 2 minutes) for 100 pages. some time we have to use 200 to 250 pages.

Steps are

1. Genrate image by using Get ASP.NET C# 2.0 Website Thumbnail Screenshot[^]


2. Read all all images and create PDF using
C#
PdfWriter.GetInstance
and
C#
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance('ImagePathOnDisk');



it is very slow because
Step 1. A. Get Bitmap Image (100 time loop for 100 images)
B. Write it on Server Disk (100 images)

Step 2. A. Read From Server local (100 times loop for 100 images)
B. Create PDF (1 File)
C. Write on Server Disk (i file)


I want to improve this.
Idea is that i want to all images in server memory (say array of memoryStream) use them to create PDF and finally release memory of memoryStream array.

I Implemented

1.
C#
MemoryStream[] arrBmp;


2. in loop while create image i am saving it in server memory
C#
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
arrBmp[k] = ms;



3. at the time of creating PDF using loop
C#
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(arrBmp[k]);


at this point there is an error "Index was outside the bounds of the array."
even i have 4 element in arrBmp[] but it throws error at k = 0 also.

NOTE:- step 2 and 3 are in different methods.

Q.1 why this error?
Q.2 Is it write way?
Q. what else i can do to increase performance?
Posted
Updated 28-Jul-15 23:40pm
v3
Comments
Santosh K. Tripathi 29-Jul-15 6:45am    
issue is solved by using .GetBuffer() Ref:-


Image image = Image.GetInstance(chartImage.GetBuffer());
http://www.codeproject.com/Articles/297677/Using-ASP-Net-Charting-with-Image-Map-in-MVC

Still its takes 40 seconds. i want to reduce it more. is MemoryStream[] arrBmp is good or should i use some other data structure?
Santosh K. Tripathi 29-Jul-15 12:49pm    
People who can't give any solution, they don't have right to down vote. if you are down voting, just give logic for that.
Bye the way They have there own rights to up or down vote. I know at the time of posting question few people will down vote.

if you are down voting, mean you have knowledge more than i have. But you are "A Miser" in knowledge shearing.

Share it.. :)
Frank Lindecke 3-Aug-15 2:58am    
Is it able to use a concurrent API?

You have the following steps:
1) Read image data into memory
2) Create an image
3) Create PDF from image
4) Write PDF to disk

Add a queue between each step and connect the steps via the queue.
Each step uses a separate worker taken from in-queue and writing result it to out-queue.

This might improve performance, because conversion from data to image and image to pdf can be made concurrent (if theses steps are the cpu hogs).

Steps 1 and 4 are io intensive, but when reading or writing data cpu is waiting for disk and these small waits can be used by other threads.

NOTE:
1) This might only work if creation of image and creation of pdf API can be used concurrently.
2) Use a cpu profile and check if the assumptions you made are correct (e.g. that conversion of image and conversion of pdf are the cpu hogs)
Santosh K. Tripathi 3-Aug-15 6:14am    
thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900