Search code examples
c#pdfhl7nhapi

Extract PDF from HL7


I am writing a program in C# that will parse the HL7 message and will write the data to a Text File. The HL7 also contains an embedded PDF in base64 format. I have to decode the base64 encoded pdf and it too.

        string fileLocation = @"hl7file.hl7";

        var message = File.ReadAllText(fileLocation);

        PipeParser parser = new PipeParser();

        IMessage m = parser.Parse(message);//str message will contain your HL7 Message
        ADT_A03 adtA02 = m as ADT_A03;

        ORU_R01 oru = m as ORU_R01;
        ORU_R01_PATIENT patient = oru.GetPATIENT_RESULT().PATIENT;

        ORU_R01_ORDER_OBSERVATION orderObservation = oru.GetPATIENT_RESULT().GetORDER_OBSERVATION();
        OBR obr = orderObservation.OBR;

        PID pid = patient.PID;

        string PatientId = pid.GetPatientIdentifierList(0).IDNumber.ToString();

        ORU_R01_OBSERVATION observation = orderObservation.GetOBSERVATION(0);
        OBX obx = observation.OBX;


        var pdfFile = obx.GetObservationValue(0).data;

        Dictionary<string, string> hl7Data = new Dictionary<string, string>();

        hl7Data.Add("Patient ID", PatientId = pid.GetPatientIdentifierList(0).IDNumber.ToString());
        hl7Data.Add("Last_Name", pid.GetPatientName(0).FamilyName.Surname.Value);
        hl7Data.Add("First_Name", pid.GetPatientName(0).GivenName.Value);
        hl7Data.Add("DOB", pid.DateTimeOfBirth.Time.ToString());
        hl7Data.Add("Sex", pid.AdministrativeSex.Value);
        hl7Data.Add("Address", pid.GetPatientAddress(0).StreetAddress.StreetOrMailingAddress.Value);
        hl7Data.Add("City", pid.GetPatientAddress(0).City.Value);
        hl7Data.Add("State", pid.GetPatientAddress(0).StateOrProvince.Value);
        hl7Data.Add("Zip_Code", pid.GetPatientAddress(0).ZipOrPostalCode.Value);
        hl7Data.Add("Signature_Required", obr.PriorityOBR.Value);
        hl7Data.Add("Referring_Physician_Last_Name", obr.GetOrderingProvider(0).FamilyName.Surname.Value);
        hl7Data.Add("Referring_Physician_First_Name", obr.GetOrderingProvider(0).GivenName.Value);
        hl7Data.Add("NPI", obr.GetOrderingProvider(0).IdentifierTypeCode.Value);
        hl7Data.Add("Observation_Date", obr.ObservationDateTime.Time.ToString());
        hl7Data.Add("File_Type", obr.UniversalServiceIdentifier.Identifier.Value);
        hl7Data.Add("File_Description", obr.UniversalServiceIdentifier.Text.Value);

        using (StreamWriter file = new StreamWriter(@"C:\Users\samin.khan\Desktop\myfile.txt"))
        {
            foreach (var entry in hl7Data)
            {
                file.WriteLine("{0}: {1}", entry.Key, entry.Value);
            }
        }

So the line var pdfFile = obx.GetObservationValue(0).data shows the PDF in base64 format, but I am not able to access the data. I can clearly see the pdf value inside the pdffile object but can't access it. Please check the image attachment.

How do I access the PDF data?

Using:

 using NHapi.Base.Parser;
 using NHapi.Model.V25.Datatype;
 using NHapi.Model.V25.Message;
 using NHapi.Model.V25.Group;
 using NHapi.Model.V25.Segment;
 using NHapi.Base.Model;

Image: Can't access Data item in object

After casting it as ED (Encapsulated Data) object:After casting it as ED object


Solution

  • Just going off the original screenshot, you should be able to do the following:

    var ed = axc.Data as ED;
    if (ed != null)
    {
        var bytes = Convert.FromBase64String(ed.Data);
        File.WriteAllBytes("SomeFileName.pdf", bytes);
    }