Possible bug in CMapAwareDocumentFont.ProcessUni2Byte() (iTextSharp 5.4.3)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Possible bug in CMapAwareDocumentFont.ProcessUni2Byte() (iTextSharp 5.4.3)


I am working on a application which in general adds one line of text at the bottom of each page in PDF file. Before adding the text, application checks if there is enough space for the text. This is done by using TextMarginFinder. To simplify the code:
  PdfReaderContentParser parser = new PdfReaderContentParser(pdfInput);
  // where pdfInput is an instance of PdfReader
  textMarginFinder = parser.ProcessContent(i, new TextMarginFinder()); // 'i' being current page number
  bottomMarginSize = textMarginFinder.GetLly();

In some cases, exception occurs while calling parser.ProcessContent(...):
Index was outside the bounds of the array.

This is a stack trace:
   at iTextSharp.text.pdf.CMapAwareDocumentFont.ProcessUni2Byte() in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\CMapAwareDocumentFont.cs:line 196
   at iTextSharp.text.pdf.CMapAwareDocumentFont.InitFont() in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\CMapAwareDocumentFont.cs:line 101
   at iTextSharp.text.pdf.CMapAwareDocumentFont..ctor(PRIndirectReference refFont) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\CMapAwareDocumentFont.cs:line 94
   at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.GetFont(PRIndirectReference ind) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\parser\PdfContentStreamProcessor.cs:line 135
   at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.SetTextFont.Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List`1 operands) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\parser\PdfContentStreamProcessor.cs:line 584
   at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.InvokeOperator(PdfLiteral oper, List`1 operands) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\parser\PdfContentStreamProcessor.cs:line 251
   at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.ProcessContent(Byte[] contentBytes, PdfDictionary resources) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\parser\PdfContentStreamProcessor.cs:line 367
   at iTextSharp.text.pdf.parser.PdfReaderContentParser.ProcessContent[E](Int32 pageNumber, E renderListener) in D:\Projects\AddLabelToPDF\iTextSharp_src\core\iTextSharp\text\pdf\parser\PdfReaderContentParser.cs:line 76
   at Test.PdfClassification.DoClassification(String filePath, Boolean firstClassification, String classificationMark, Color markColor, String markChar) in D:\Projects\AddLabelToPDF\Test\PdfClassification.cs:line 210

I used the iTextSharp source code to debug the problem and found the possible bug in CMapAwareDocumentFont.ProcessUni2Byte() in the following code block:
            int[] e = byte2uni.ToOrderedKeys();
            if (e.Length == 0)
            cidbyte2uni = new char[256];    //NOTE: using cidbyte2uni = new char[e.Length]; solved my problem

            for (int k = 0; k < e.Length; ++k)
                int key = e[k];
                cidbyte2uni[key] = (char)byte2uni[key];

What happens is that key gets value 256 and cidbyte2uni[256] does not exist - since cidbyte2uni[255] is the last element, hence the exception.
I changed the array definition from new char[256] to new char[e.Length] and that solved my problem.

My concern is if this is a right way to fix the problem.
Also, where can I report this bug and hope that this would be solved in next iTextSharp version?