-1

Is it possible with itextsharp, to create several PDF's at the same time?

I have begun with this code:

pdfdoc = new Document(PageSize.A4);
PdfWriter writer;

string Pfad = @"\...." + Filename; //Filename is with a random number.

writer = PdfWriter.GetInstance(pdfdoc, new FileStream(Server.MapPath(Pfad), 
writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
TwoColumnHeaderFooter eventHandler = new TwoColumnHeaderFooter();
writer.PageEvent = eventHandler;

pdfdoc.Open();

A great thanks for a fast answer.

Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
  • Do you mean creating a PDF in different threads? What are you trying to do? – Sven Grosen Dec 09 '13 at 13:31
  • The user click the button and this one call the c# function, where create the pdf, and at the same time another user click in his Browser the same button. – user2834085 Dec 09 '13 at 13:41

2 Answers2

1

If you are planning on allowing multiple user to create PDFs on a published web site there is no problem with that

You only have to worry about creating unique filenames for each generated PDF file if you are going to generate them one the server (and then delete them after user has download it)

Or you could just create the PDFs in memory and return them as part of the response and let the user save it on its local machine

Any of these two solutions can be called by many users at the same time without any issues.

see this examples

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Above it's a code sample, it's that what you mean? – user2834085 Dec 09 '13 at 13:59
  • @user2834085 I have added links to examples that can help you – Mauricio Gracia Gutierrez Dec 09 '13 at 14:17
  • Thanks for the links. In my case, the function save the PDF-File (with a random number in the title) in a Folder on the Server, and then on the client-side the Javascript open the Saved PDF-File. That works for one File, but not, when an another User create his PDF while the other/first is actual creating. – user2834085 Dec 09 '13 at 14:28
  • @user2834085 have you checked if you are really getting different filenames for each request ? maybe you have problems initializing the seed of the random ? – Mauricio Gracia Gutierrez Dec 09 '13 at 14:29
  • Yes, the code above writes different files with random rumbers for each file in the folder. The Second File is correct, but a Message Box says the first one "has an error, it can't it open, it is open in the function". – user2834085 Dec 09 '13 at 14:55
  • if you are seeing 2 PDF files on the server then it seems to be an error with the way that your are displaying it on the javascript – Mauricio Gracia Gutierrez Dec 09 '13 at 14:59
  • The error is on the javascript and on the Adobe Reader, when i direct open the file in the Server Folder. – user2834085 Dec 09 '13 at 15:03
  • @user2834085 please post your javascript part of the logic maybe in a new question – Mauricio Gracia Gutierrez Dec 09 '13 at 15:04
  • Great thanks for the last answers, but i don't think its a javascript problem, it must be a problem with the c# code, then he writes correctly a simple PDF-File and open it in the browser, but don't several creating files at the same time. The browser can open only one File correct. The Javascript part is: this.location.href = 'path/pdftitle_' + CustomerID + '_' + randomNumber + '.pdf'; – user2834085 Dec 09 '13 at 15:12
  • @user2834085 no problem. feel free to mark this as answer and post what you consider to be your problem as a new question – Mauricio Gracia Gutierrez Dec 09 '13 at 15:15
1

I solved the Problem:

//This line works
Document pdfdoc = new Document(PageSize.A4);

//===================================================================================
     //I make the mistake, and declare the 
     Document pdfdoc;
     //global as 
     private static Document pdfdoc;
     // and this one works only for one creating PDF not for several at the same time.
//===================================================================================


PdfWriter writer;

string Pfad = @"\...." + Filename; //Filename is with a random number.

writer = PdfWriter.GetInstance(pdfdoc, new FileStream(Server.MapPath(Pfad), 
writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
TwoColumnHeaderFooter eventHandler = new TwoColumnHeaderFooter();
writer.PageEvent = eventHandler;

pdfdoc.Open();