private void displayContentStream(DictionaryToken contentDictionaryToken) { var fonts = ((PdfContent)contentDictionaryToken.PdfObject !).Fonts; var bytes = bytesMemory.Span; sb.Clear(); var fontSb = new StringBuilder(); if (bytes.Length < 10) { for (int bytesIndex = 0; bytesIndex < bytes.Length - 1; bytesIndex++) { append(sb, bytes[bytesIndex]); } StreamTextBox.Text = sb.ToString(); sb.Clear(); return; } var contentState = contentStateEnum.parse; var startBTIndex = int.MinValue; char[]? fontEncoding = null; byte b0; var b1 = (byte)' ';//"add" space before buffer in case the very first 2 characters are like BI or BT var b2 = bytes[0]; var b3 = bytes[1]; append(sb, b2); append(sb, b3); for (int bytesIndex = 2; bytesIndex < bytes.Length; bytesIndex++) { b0 = b1; b1 = b2; b2 = b3; b3 = bytes[bytesIndex]; if (contentState == contentStateEnum.ID) { //checking if that ever occurs if ((b1 == '\n' || b1 == '\r') && (b2 != 'E' || b3 != 'I')) { System.Diagnostics.Debugger.Break(); } } if (contentState == contentStateEnum.BT) { //process T, i.e. strings one byte at a time if (b3 == '(') { //inside a string sb.Append('('); var encoding = fontEncoding ?? PdfEncodings.Standard; while (true) { b3 = bytes[++bytesIndex]; if (b3 == ')') { //sb.Append(')'); not needed, will b3 will be later added to sb break; } var c = encoding[b3]; if (c == 0xFFFF) { sb.Append('\'' + b3.ToString("x") + '\''); } else { sb.Append(c); } } } else if (b3 == 'T' && bytes[bytesIndex + 1] == 'f') { //font definition found int searchBackIndex; var isFontFound = false; for (searchBackIndex = bytesIndex - 1; searchBackIndex > startBTIndex; searchBackIndex--) { if (bytes[searchBackIndex] == '/') { fontSb.Clear(); searchBackIndex++; for (; searchBackIndex < bytesIndex; searchBackIndex++) { var b = bytes[searchBackIndex]; if (Tokeniser.IsWhiteSpace(b)) { var fontString = fontSb.ToString(); fontEncoding = fonts[fontString].Encoding8Bit; isFontFound = true; break; } fontSb.Append((char)b); } if (!isFontFound) { System.Diagnostics.Debugger.Break(); } break; } } if (!isFontFound) { System.Diagnostics.Debugger.Break(); } } else if (b3 == 'E' && bytes[bytesIndex + 1] == 'T') { startBTIndex = int.MinValue; fontEncoding = null; contentState = contentStateEnum.parse; } } else { //search for BI, DI, EI and BT if (Tokeniser.IsDelimiterByte(b0) && Tokeniser.IsDelimiterByte(b3)) { switch (contentState) { case contentStateEnum.parse: if (b1 == 'B' && b2 == 'I') { contentState = contentStateEnum.BI; } else if (b1 == 'B' && b2 == 'T') { contentState = contentStateEnum.BT; startBTIndex = bytesIndex; } break; case contentStateEnum.BI: if (b1 == 'I' && b2 == 'D') { contentState = contentStateEnum.ID; } break; case contentStateEnum.ID: if (b1 == 'E' && b2 == 'I') { contentState = contentStateEnum.parse; } break; default: throw new NotSupportedException(); } } } append(sb, b3); } StreamTextBox.Text = sb.ToString(); sb.Clear(); }