ParseDataStream(
            InputByteArray inputArray, SessionSettings sessionSettings,
            string DataStreamName, TypeTelnetDevice?TypeDevice = null)
        {
            var accumCmdList                   = new WorkstationCommandList();
            var responseItemList               = new ResponseItemList();
            TelnetCommandList   telList        = null;
            ResponseHeader      curRespHeader  = null;
            ResponseHeader      nextRespHeader = null;
            DataStreamHeader    dsh            = null;
            ControlFunctionList funcList       = null;

            bool didParse = true;

            while ((didParse == true) && (inputArray.RemainingLength > 0))
            {
                didParse       = false;
                curRespHeader  = nextRespHeader;
                nextRespHeader = null;

                if ((didParse == false) && (dsh == null))
                {
                    var telCmd = inputArray.NextTelnetCommand();
                    if (telCmd != null)
                    {
                        if (telList == null)
                        {
                            telList = new TelnetCommandList();
                        }
                        telList.Add(telCmd);
                        var s1 = telCmd.ToString();
                        didParse = true;
                    }
                }

                // parse the data stream header.
                if ((didParse == false) && (dsh == null))
                {
                    var code = inputArray.PeekDataStreamCode();
                    if (code != null)
                    {
                        var rv = DataStreamHeader.Factory(inputArray);
                        dsh      = rv.Item1;
                        didParse = true;

                        // update the type of device depending on data stream header stream
                        // code.
                        if ((TypeDevice == null) && (dsh != null) && (dsh.Errmsg == null))
                        {
                            TypeDevice = dsh.StreamCode.ToTypeTelnetDevice();
                        }
                    }
                }

                // parse as a sequence of workstation commands.
                if (didParse == false)
                {
                    if ((TypeDevice == null) || (TypeDevice.Value != TypeTelnetDevice.Printer))
                    {
                        var rv = Process5250.ParseWorkstationCommandList(
                            inputArray, sessionSettings);

                        var anotherDsh    = rv.Item1;
                        var wrkstnCmdList = rv.Item2;

                        if ((anotherDsh != null) && (anotherDsh.Errmsg == null))
                        {
                            didParse = true;
                            dsh      = anotherDsh;
                        }

                        // draw the fields and literals on the canvas.
                        if (wrkstnCmdList?.Count > 0)
                        {
                            accumCmdList.Append(wrkstnCmdList);
                            didParse = true;
                        }
                    }
                }

                // parse as sequence of SCS control functions. ( printer data stream ).
                if (didParse == false)
                {
                    if ((TypeDevice == null) || (TypeDevice.Value == TypeTelnetDevice.Printer))
                    {
                        var rv = ControlFunctionList.ParseDataStream(inputArray);
                        if (rv.Item1?.Count > 0)
                        {
                            didParse = true;
                            funcList = rv.Item1;
                        }
                    }
                }

                // parse as response stream header.
                if ((didParse == false) &&
                    (ResponseHeader.IsResponseHeader(inputArray).Item1 == true))
                {
                    curRespHeader = new ResponseHeader(inputArray);
                    didParse      = true;

                    responseItemList.Add(curRespHeader);
                    var rv = Response5250.ParseResponseStream(inputArray, curRespHeader);
                    responseItemList.Append(rv.Item1);
                }
            }

            return(new Tuple <WorkstationCommandList, ResponseItemList,
                              DataStreamHeader, TelnetCommandList, ControlFunctionList>(
                       accumCmdList, responseItemList, dsh, telList, funcList));
        }
        private OpenPdfDocument PrintToPdf(
            DataStreamHeader dsh, ControlFunctionList funcList,
            OpenPdfDocument OpenDoc = null)
        {
            OpenPdfDocument openDoc = OpenDoc;

            if (dsh is StartPrinterFileDataStreamHeader)
            {
                var docPath = "c:\\downloads\\printout.pdf";
                openDoc = new OpenPdfDocument(docPath);
            }

            else if (dsh is PrinterDataStreamHeader)
            {
                if (openDoc == null)
                {
                    var docPath = "c:\\downloads\\printout.pdf";
                    openDoc = new OpenPdfDocument(docPath);
                }

                if (funcList != null)
                {
                    foreach (var func in funcList)
                    {
                        if (func.ControlCode == ControlFunctionCode.CarriageReturn)
                        {
                            openDoc.ApplyCarriageReturn();
                        }
                        else if (func.ControlCode == ControlFunctionCode.NewLine)
                        {
                            openDoc.ApplyNewLine();
                        }
                        else if (func.ControlCode == ControlFunctionCode.PresentationPosition)
                        {
                            var presentPos = func as PresentationPositionControlFunction;
                            if (presentPos.Direction == PresentationPositionDirection.Horizontal)
                            {
                                openDoc.ApplyPos(presentPos.PositionValue - 1);
                            }
                        }
                        else if (func.ControlCode == ControlFunctionCode.Text)
                        {
                            var textFunc = func as TextControlFunction;
                            openDoc.ApplyText(textFunc.TextBytes.EbcdicBytesToString());
                        }

                        else if ((func.ControlCode == ControlFunctionCode.Undocumented2) ||
                                 (func.ControlCode == ControlFunctionCode.Undocumented3))
                        {
                            openDoc.ApplyText(" ");
                        }

                        else if (func.ControlCode == ControlFunctionCode.FormFeed)
                        {
                            openDoc.ApplyNewPage();
                        }
                    }

                    if (funcList.IsSingleNullList() == true)
                    {
                        openDoc.CloseDoc();
                        openDoc = null;
                    }
                }
            }
            return(openDoc);
        }