procedure TForm1.cmdGenerateClick(Sender: TObject);
    // This is just an example program to show how to generate a HIPAA 837 4010 X096 EDI X12 file
    // in Delphi with Framework EDI component
    var
       oEdiDoc : IediDocument;
       oInterchange : IediInterchange;
       oGroup : IediGroup;
       oTransactionset : IediTransactionset;
       oSegment : IediDataSegment;
       oSchemas : IediSchemas;

       i : Integer;
       sInstance : String;
       sPath : String;
       sSefFile : String;
       sEdiFile : String;

       nInfoSources : Integer;
       nInfoSourceCounter : Integer;
       nSubscribers : Integer;
       nSubscriberCounter : Integer;

       nHlCounter : Integer;
       nHlSubscriberParent : Integer;

    begin
       cmdGenerate.Enabled := False;

       sPath := Trim(GetCurrentDir) + '\';
       sSefFile := '837_X096.SEF';
       sEdiFile := '837_X096_OUTPUT.X12';

       oEdiDoc := CoediDocument.Create();

       //CHANGE CURSOR TYPE TO FORWARDWRITE TO IMPROVE PERFORMANCE
       oEdiDoc.CursorType := 2;     //forwardWrite

       //DISABLE INTERNAL STADARD REFERENCE LIBRARY
       oSchemas := oEdiDoc.GetSchemas() as IediSchemas;
       oSchemas.EnableStandardReference := False;

       //LOAD SEF FILES
       oEdidoc.LoadSchema(sPath + sSefFile,0);

       //SET TERMINATORS
       oEdiDoc.SegmentTerminator := '~' + chr(13) + chr(10);
       oEdiDoc.ElementTerminator := '*';
       oEdiDoc.CompositeTerminator := ':';

       //CREATE INTERCHANGE
       oInterchange := oEdiDoc.CreateInterchange('X','004010') as IediInterchange;
       oSegment := oInterchange.GetDataSegmentHeader as IediDataSegment;
       oSegment.DataElementValue[1,0] := '00';
       oSegment.DataElementValue[3,0] := '00';
       oSegment.DataElementValue[5,0] := '12';
       oSegment.DataElementValue[6,0] := 'SENDERID';
       oSegment.DataElementValue[7,0] := '12';
       oSegment.DataElementValue[8,0] := 'RECEIVERID';
       oSegment.DataElementValue[9,0] := '020329';
       oSegment.DataElementValue[10,0] := '0435';
       oSegment.DataElementValue[11,0] := 'U';
       oSegment.DataElementValue[12,0] := '00401';
       oSegment.DataElementValue[13,0] := '000000020';
       oSegment.DataElementValue[14,0] := '0';
       oSegment.DataElementValue[15,0] := 'T';
       oSegment.DataElementValue[16,0] := ':';

       //CREATE GROUP
       oGroup := oInterchange.CreateGroup('004010X096') as IediGroup;
       oSegment := oGroup.GetDataSegmentHeader as IediDataSegment;
       oSegment.DataElementValue[1,0] := 'HC';
       oSegment.DataElementValue[2,0] := 'SENDERDEPT';
       oSegment.DataElementValue[3,0] := 'RECEIVERDEPT';
       oSegment.DataElementValue[4,0] := '20020329';
       oSegment.DataElementValue[5,0] := '0435';
       oSegment.DataElementValue[6,0] := '1';
       oSegment.DataElementValue[7,0] := 'X';
       oSegment.DataElementValue[8,0] := '004010X096';

       //TRANSACTION SET HEADER
       oTransactionset := oGroup.CreateTransactionset('837') as IediTransactionset;
       oSegment := oTransactionset.GetDataSegmentHeader as IediDataSegment;
       oSegment.DataElementValue[2,0] := '987654';

       //BHT BEGINNING OF HIERARCHICAL TRANSACTION
       oSegment := oTransactionset.CreateDataSegment('BHT') as IediDataSegment;
       oSegment.DataElementValue[1,0] := '0019';
       oSegment.DataElementValue[2,0] := '00';
       oSegment.DataElementValue[3,0] := '0123';
       oSegment.DataElementValue[4,0] := '20081201';
       oSegment.DataElementValue[5,0] := '0932';
       oSegment.DataElementValue[6,0] := 'CH';

       //REF TRANSMISSION TYPE IDENTIFICATION
       oSegment := oTransactionset.CreateDataSegment('REF') as IediDataSegment;
       oSegment.DataElementValue[1,0] := '87';
       oSegment.DataElementValue[2,0] := '004010X098';

       //NM1 SUBMITTER
       oSegment := oTransactionset.CreateDataSegment('NM1\NM1') as IediDataSegment;   //including the loop instance number 2 in NM1 is not necessary with cursor type forwardwrite
       oSegment.DataElementValue[1,0] := '41';
       oSegment.DataElementValue[2,0] := '2';
       oSegment.DataElementValue[3,0] := 'Jones Hospital';
       oSegment.DataElementValue[8,0] := '46';
       oSegment.DataElementValue[9,0] := '12345';

       //PER RECEIVER EDI CONTACT INFORMATION
       oSegment := oTransactionset.CreateDataSegment('NM1\PER') as IediDataSegment;
       oSegment.DataElementValue[1,0] := 'IC';
       oSegment.DataElementValue[2,0] := 'JANE DOE';
       oSegment.DataElementValue[3,0] := 'TE';
       oSegment.DataElementValue[4,0] := '9055555555';

       //NM1 RECEIVER NAME
       oSegment := oTransactionset.CreateDataSegment('NM1(2)\NM1') as IediDataSegment;
       oSegment.DataElementValue[1,0] := '40';
       oSegment.DataElementValue[2,0] := '2';
       oSegment.DataElementValue[3,0] := 'Medicare';
       oSegment.DataElementValue[8,0] := '46';
       oSegment.DataElementValue[9,0] := '00120';

       nInfoSources := 1;
       nInfoSourceCounter := 1;
       nSubscribers := 1;
       nSubscriberCounter := 1;
       nHlCounter := 0;

       //2000A BILLING / PAY-TO PROVIDER HL LOOP
       while nInfoSourceCounter <= nInfoSources do
       begin
            nHlCounter := nHlCounter + 1;
            nHlSubscriberParent := nHlCounter;

            //HL - BILLING PROVIDER
            oSegment := oTransactionset.CreateDataSegment('HL\HL') as IediDataSegment;
            oSegment.DataElementValue[1,0] := IntToStr(nHlCounter);
            oSegment.DataElementValue[3,0] := '20';
            oSegment.DataElementValue[4,0] := '1';

            //BILLING PROVIDER SPECIALTY INFORMATION
            oSegment := oTransactionset.CreateDataSegment('HL\PRV') as IediDataSegment;
            oSegment.DataElementValue[1,0] := 'BI';
            oSegment.DataElementValue[2,0] := 'ZZ';
            oSegment.DataElementValue[3,0] := '203BA0200N';

            //2010AA BILLING PROVIDER
            //NM1 BILLING PROVIDER NAME
            oSegment := oTransactionset.CreateDataSegment('HL\NM1\NM1') as IediDataSegment;
            oSegment.DataElementValue[1,0] := '85';
            oSegment.DataElementValue[2,0] := '2';
            oSegment.DataElementValue[3,0] := 'Jones Hospital';
            oSegment.DataElementValue[8,0] := 'XX';
            oSegment.DataElementValue[9,0] := '330127';

            //N3 BILLING PROVIDER ADDRESS
            oSegment := oTransactionset.CreateDataSegment('HL\NM1\N3') as IediDataSegment;
            oSegment.DataElementValue[1,0] := '225 Main Street Barkley Building';

            //NM1 BILLING CITY STATE ZIP
            oSegment := oTransactionset.CreateDataSegment('HL\NM1\N4') as IediDataSegment;
            oSegment.DataElementValue[1,0] := 'Smallville';
            oSegment.DataElementValue[2,0] := 'TX';
            oSegment.DataElementValue[3,0] := '22334';

            while nSubscriberCounter <= nSubscribers do
            begin
                    nHlCounter := nHlCounter + 1;

                    //2000B SUBSCRIBER HL LOOP
                    //HL - SUBSCRIBER
                    oSegment := oTransactionset.CreateDataSegment('HL\HL') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := IntToStr(nHlCounter);
                    oSegment.DataElementValue[2,0] := IntToStr(nHlSubscriberParent);
                    oSegment.DataElementValue[3,0] := '22';
                    oSegment.DataElementValue[4,0] := '0';

                    //SUBSCRIBER INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\SBR') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'P';
                    oSegment.DataElementValue[2,0] := '18';
                    oSegment.DataElementValue[9,0] := 'MB';

                    //NM1 SUBSCRIBER NAME
                    oSegment := oTransactionset.CreateDataSegment('HL\NM1\NM1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'IL';
                    oSegment.DataElementValue[2,0] := '1';
                    oSegment.DataElementValue[3,0] := 'DOE';
                    oSegment.DataElementValue[4,0] := 'JOHN';
                    oSegment.DataElementValue[5,0] := 'T';
                    oSegment.DataElementValue[8,0] := 'MI';
                    oSegment.DataElementValue[9,0] := '030005074A';

                    //SUBSCRIBER ADDRESS
                    oSegment := oTransactionset.CreateDataSegment('HL\NM1\N3') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '125 City Avenue';

                    //NM1 SUBSCRIBER CITY STATE ZIP
                    oSegment := oTransactionset.CreateDataSegment('HL\NM1\N4') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'Centerville';
                    oSegment.DataElementValue[2,0] := 'PA';
                    oSegment.DataElementValue[3,0] := '11711';

                    //NM1 SUBSCRIBER DEMOGRAPHIC INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\NM1\DMG') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'D8';
                    oSegment.DataElementValue[2,0] := '19261111';
                    oSegment.DataElementValue[3,0] := 'M';

                    //2010BB SUBSCRIBER/PAYER
                    //PAYER NAME
                    oSegment := oTransactionset.CreateDataSegment('HL\NM1(2)\NM1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'PR';
                    oSegment.DataElementValue[2,0] := '2';
                    oSegment.DataElementValue[3,0] := 'Medicare B';
                    oSegment.DataElementValue[8,0] := 'PI';
                    oSegment.DataElementValue[9,0] := '00435';


                    //2300 CLAIM
                    //CLM CLAIM LEVEL INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\CLM') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '00111222333';
                    oSegment.DataElementValue[2,0] := '89.93';
                    oSegment.DataElementValue[5,1] := '14';
                    oSegment.DataElementValue[5,2] := 'A';
                    oSegment.DataElementValue[5,3] := '1';
                    oSegment.DataElementValue[6,0] := 'Y';
                    oSegment.DataElementValue[7,0] := 'A';
                    oSegment.DataElementValue[8,0] := 'Y';
                    oSegment.DataElementValue[9,0] := 'Y';
                    oSegment.DataElementValue[18,0] := 'Y';

                    //DTP DATE OF STATEMENT
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\DTP') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '434';
                    oSegment.DataElementValue[2,0] := 'D8';
                    oSegment.DataElementValue[3,1] := '19960911';

                    //REF CLAIM CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\CL1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '3';
                    oSegment.DataElementValue[2,0] := '1';

                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BK';
                    oSegment.DataElementValue[1,2] := '366.9';

                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI(2)') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BF';
                    oSegment.DataElementValue[1,2] := '401.9';
                    oSegment.DataElementValue[2,1] := 'BF';
                    oSegment.DataElementValue[2,2] := '794.31';

                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI(3)') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BQ';
                    oSegment.DataElementValue[1,2] := '15.3';
                    oSegment.DataElementValue[1,3] := 'D8';
                    oSegment.DataElementValue[1,4] := '19960911';


                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI(4)') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BH';
                    oSegment.DataElementValue[1,2] := 'A1';
                    oSegment.DataElementValue[1,3] := 'D8';
                    oSegment.DataElementValue[1,4] := '19261111';
                    oSegment.DataElementValue[1,1] := 'BH';
                    oSegment.DataElementValue[1,2] := 'A2';
                    oSegment.DataElementValue[1,3] := 'D8';
                    oSegment.DataElementValue[1,4] := '19911101';
                    oSegment.DataElementValue[1,1] := 'BH';
                    oSegment.DataElementValue[1,2] := 'B1';
                    oSegment.DataElementValue[1,3] := 'D8';
                    oSegment.DataElementValue[1,4] := '19261111';
                    oSegment.DataElementValue[1,1] := 'BH';
                    oSegment.DataElementValue[1,2] := 'B2';
                    oSegment.DataElementValue[1,3] := 'D8';
                    oSegment.DataElementValue[1,4] := '19870101';

                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI(5)') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BE';
                    oSegment.DataElementValue[1,2] := 'A2';
                    oSegment.DataElementValue[1,5] := '15.31';

                    //HI HEALTH CARE DIAGNOSIS CODES
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\HI(6)') as IediDataSegment;
                    oSegment.DataElementValue[1,1] := 'BG';
                    oSegment.DataElementValue[1,2] := '09';

                    //NM1 ATTENDING PHYSICIAN
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\NM1\NM1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '71';
                    oSegment.DataElementValue[2,0] := '1';
                    oSegment.DataElementValue[3,0] := 'JONES';
                    oSegment.DataElementValue[4,0] := 'JOHN';
                    oSegment.DataElementValue[5,0] := 'J';
                    oSegment.DataElementValue[8,0] := 'XX';
                    oSegment.DataElementValue[9,0] := 'B99937';

                    //PRV RENDERING PROVIDER INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\NM1\PRV') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'AT';
                    oSegment.DataElementValue[2,0] := 'ZZ';
                    oSegment.DataElementValue[3,0] := '363LP0200N';

                    //SUBSCRIBER INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\SBR') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'S';
                    oSegment.DataElementValue[2,0] := '01';
                    oSegment.DataElementValue[3,0] := '351630';
                    oSegment.DataElementValue[4,0] := 'STATE TEACHERS';
                    oSegment.DataElementValue[9,0] := '09';

                    //DEMOGRAPHIC INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\DMG') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'D8';
                    oSegment.DataElementValue[2,0] := '19261111';
                    oSegment.DataElementValue[3,0] := 'F';

                    //OTHER HEALTH INSURANCE INFORMATION
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\OI') as IediDataSegment;
                    oSegment.DataElementValue[3,0] := 'Y';
                    oSegment.DataElementValue[6,0] := 'Y';

                    //NM1 SUBSCRIBER NAME
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\NM1\NM1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'IL';
                    oSegment.DataElementValue[2,0] := '1';
                    oSegment.DataElementValue[3,0] := 'DOE';
                    oSegment.DataElementValue[4,0] := 'JANE';
                    oSegment.DataElementValue[5,0] := 'S';
                    oSegment.DataElementValue[8,0] := 'MI';
                    oSegment.DataElementValue[9,0] := '222004433';

                    //SUBSCRIBER ADDRESS
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\NM1\N3') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := '125 City Avenue';

                    //SUBSCRIBER CITY STATE ZIP
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\NM1\N4') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'Centerville';
                    oSegment.DataElementValue[2,0] := 'PA';
                    oSegment.DataElementValue[3,0] := '17111';

                    //NM1 PAYER NAME
                    oSegment := oTransactionset.CreateDataSegment('HL\CLM\SBR\NM1(2)\NM1') as IediDataSegment;
                    oSegment.DataElementValue[1,0] := 'PR';
                    oSegment.DataElementValue[2,0] := '2';
                    oSegment.DataElementValue[3,0] := 'STATE TEACHERS';
                    oSegment.DataElementValue[8,0] := 'PI';
                    oSegment.DataElementValue[9,0] := '1135';

                    for i := 1 to 3 do begin

                            sInstance := trim(IntToStr(i));

                            //LX SERVICE LINE COUNTER
                            oSegment := oTransactionset.CreateDataSegment('HL\CLM\LX\LX') as IediDataSegment;
                            oSegment.DataElementValue[1,0] := sInstance;

                            //SV2 INSTITUTIONAL SERVICE
                            oSegment := oTransactionset.CreateDataSegment('HL\CLM\LX\SV2') as IediDataSegment;
                            oSegment.DataElementValue[1,0] := '305';
                            oSegment.DataElementValue[2,1] := 'HC';
                            oSegment.DataElementValue[2,2] := '85025';
                            oSegment.DataElementValue[3,0] := '13.39';
                            oSegment.DataElementValue[4,0] := 'UN';
                            oSegment.DataElementValue[5,0] := '1';

                            //DTP DATE - SERVICE DATE(S)
                            oSegment := oTransactionset.CreateDataSegment('HL\CLM\LX\DTP') as IediDataSegment;
                            oSegment.DataElementValue[1,0] := '472';
                            oSegment.DataElementValue[2,0] := 'D8';
                            oSegment.DataElementValue[3,0] := '19960911';
                    end;
                    nSubscriberCounter := nSubscriberCounter + 1;
            end;
            nInfoSourceCounter := nInfoSourceCounter + 1;
       end;

       //write EDI to file
       oEdiDoc.Save(sPath + sEdiFile,0);

       ShowMessage('Generate done. Output file = ' + sEdiFile );
       cmdGenerate.Enabled := true;

    end;

    Click here to download a trial version of the Framework EDI