We are using iTexSharp in an ASP.NET MVC web site and we have noticed that
when two requests arrive that utilize the same iTextSharp functionality we
get an exception.

No apparent problem exist when testing in one request at a time.

Here is the exception:

 

System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> System.IO.EndOfStreamException: Attempted
to read past the end of the stream.

   at iTextSharp.text.pdf.RandomAccessFileOrArray.ReadFully(Byte[] b, Int32
off, Int32 len)

   at iTextSharp.text.pdf.TrueTypeFont.GetFullFont()

   at iTextSharp.text.pdf.TrueTypeFont.WriteFont(PdfWriter writer,
PdfIndirectReference piref, Object[] parms)

   at iTextSharp.text.pdf.FontDetails.WriteFont(PdfWriter writer)

   at iTextSharp.text.pdf.PdfWriter.AddSharedObjectsToBody()

   at iTextSharp.text.pdf.PdfWriter.Close()

   at iTextSharp.text.pdf.PdfDocument.Close()

   at iTextSharp.text.Document.Close()

   at PDFServices.PDFUtil.AddDocumentFields(Byte[] pdfDoc, ExtendedDocument
exDoc, DocumentFields docFields) in E:\... \PDFUtil.cs:line 753

 

This is the code. First a function that gets base fonts:

 

                 protected BaseFont GetBaseFont(bool embedded = true, bool
identity = false)
                 {
                          BaseFont bf;
                          if (embedded)
                          {
                                   string cp = false && identity ?
BaseFont.IDENTITY_H : "CP1253";
                                   bf = BaseFont.CreateFont(UnicodeFontPath,
cp, BaseFont.EMBEDDED);
                                   bf.Subset = false; // This will force the
embedding of the whole font file.
                                   bf.CompressionLevel = 9; //the
compression level (0 = best speed, 9 = best compression, -1 is default)
                          }
                          else
                          {
                                   bf = BaseFont.CreateFont(UnicodeFont,
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                          }
 
                          return bf;
                 }

 

 

And the actual code that throws the exception:

 

                          using (var reader = new PdfReader(pdfDoc))

                          using (var ms = new MemoryStream(pdfDoc.Length +
3000))

                          {

                                   var document = new
Document(reader.GetPageSizeWithRotation(1)/*PageSize.A4*/);

                                   var rotation = reader.GetPageRotation(1);

                                   BaseFont bf = GetBaseFont(true, true); //
Create a Unicode font to write in Greek...

                                   BaseFont bfGr = GetBaseFont(true, false);
// Create a Unicode font to write in Greek...

 

                                   if (…)
                                   {
                                            // Create new PDF.
                                            PdfWriter writer =
PdfWriter.GetInstance(document, ms);
                                            document.Open();
 
                                            var fontBigUnderline = new
Font(bf, 18.0f, Font.UNDERLINE);
                                            var font = new Font(bf, 14.0f);

 

                                            // Do stuff here………………

                                            writer.Flush();

                                            //reader.Close();

                                            document.Close(); // ß Here it
throws.

                                            writer.Close();

                                   }

                                   …

 

 

What I would like to ask is:

1)      Have you any idea what is causing this?

2)      Is there some sharing of font streams implemented in iText that
causes this? It seems to me that the two threads read from the same stream
and this happens.

3)      Is there any  way to avoid it? I could use a critical section (C#’s
“lock”) to prevent two thread entering a code block, but where should I put
this? I can’t lock the entire function (there will be a bottleneck then)!

 

I will download a copy of the code to have a look inside, but I post it here
too so that you know about this and perhaps help more people in the process.

 

Thanks a lot,

Kostas

------------------------------------------------------------------------------
Time is money. Stop wasting it! Get your web API in 5 minutes.
www.restlet.com/download
http://p.sf.net/sfu/restlet
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to