Announcement

Collapse
No announcement yet.

Extract & save PDF from DICOM message

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extract & save PDF from DICOM message

    Hi All,

    I have a DICOM message that contains a pdf and and patient information.

    My chanel config:

    Source: Dicom listener
    Destination: File writer

    I would like to get the content of the pdf:

    Code:
    var pdfData= msg['tag00420011'].toString();
    
    globalMap.put('pdfData',pdfData);
    But the created pdf is unreadable

    I have tried several solutions but nothing works.

    Do you have an idea?

    Thanks in advance

  • #2
    Firstly - I don't do DICOM.

    Secondly - you really should not use the globalMap for anything other than global variable(s) you want accessible by all channels. I might expect you to use the channel map variable in your use case if you even need to copy it to a local variable.

    What format is
    msg['tag00420011'].toString();
    ?

    Can you post a few lines from it? I would not expect it to be readable until written out to disk. What do you mean by the created pdf? Where is that code?
    Diridium Technologies, Inc.
    https://diridium.com

    Comment


    • #3
      Hi,

      Thanks for your response.

      Here a RAW Dicom data (There is not all the content of the message):

      Code:
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAABESUNNAgAAAFVMBADCAAAAAgABAE9CAA ACAAAAAAECAAIAVUkeADEuMi44
      NDAuMTAwMDguNS4xLjQuMS4xLjEwNC4xAAIAAwBVST4AMS4yLj UyOC4xLjEwMTQuMS4xLjEuMTAw
      MS4xLjEwMzkwNjkwODA3MjgxNC4yMDIyMDExMTE0NTkxMjQyMg ACABAAVUkUADEuMi44NDAuMTAw
      MDguMS4yLjEAAgASAFVJEAAxLjIuNDAuMC4xMy4xLjEAAgATAF NIDABkY200Y2hlLTIuMCAIAAUA
      Q1MKAElTT19JUiAxMDAIABIAREEIADIwMjIwMTExCAATAFRNBg AxNDU5MTIIABYAVUkeADEuMi44
      NDAuMTAwMDguNS4xLjQuMS4xLjEwNC4xAAgAGABVST4AMS4yLj UyOC4xLjEwMTQuMS4xLjEuMTAw........
      Transformed message (There is not all the content of the message):

      Code:
      <dicom>
      ...
      <tag00420010 len="14" tag="00420010" vr="ST">Testo Report</tag00420010>
      <tag00420011 len="50092" tag="00420011" vr="OB">25\50\44\46\2D\31\2E\34\0A\25\D3\F4\CC\E1\ 0A\31\20\30\23\F\DF\BE</tag00420011>
      <tag00420012 len="16" tag="00420012" vr="LO">application/pdf</tag00420012>
      ....
      </dicom>
      And tag 00420011 it's pdf content, but I think it is in a special format, or the content is serialized?

      Thanks in advance

      Comment


      • #4
        Taken from here & adapted to your needs:

        Code:
        /**
        Converts a hex string to a (base64-encoded) byte array
           @param hexString- The hex string representing the pdf document. non hex extra control characters like slashes will be ignored
           @param asBase64- flag that indicates if the pdf should be returned as byte array or base64-encoded string. (Default: as byte array)
           @return The pdf either as byte array or base64-encoded string
        */
        function hexStringToByteArray(hexString, asBase64) {
            
           if (asBase64 === undefined) {
               // by default, pdf is returned as byte array
               asBase64 = false;
           }
        
           // remove any non hex characters (like slashes) from the hex string
            hexString = hexString.replace(/[^A-Fa-f0-9]/g, "");
        
           // as a byte is represented by 2 characters, length must be even
           if (hexString.length % 2 !== 0) {
              throw "Must have an even number of hex digits to convert to bytes";
           }
           // determine resulting number of bytes
           var numBytes = hexString.length / 2;
           // initialize byte array of correct size
           var byteArray = new Uint8Array(numBytes);
           // add all bytes of pdf to the array
           for (var i=0; i<numBytes; i++) {
              // every byte is represented by 2 characters
              byteArray[i] = parseInt(hexString.substr(i*2, 2), 16);
           }
        
           return asBase64 ? FileUtil.encode(byteArray) : byteArray;
        }
        Call the code template like follows:

        Code:
        var pdfAsByteArray = hexStringToByteArray(msg['tag00420011'].toString());
        or
        Code:
        var pdfAsBase64 = hexStringToByteArray(msg['tag00420011'].toString(), true);

        Btw: The issue w/ posting code in this forum seems to be fixed. Great - thx to the admins!
        Last edited by odo; 01-13-2022, 02:32 AM. Reason: Fixed check for default value

        Comment


        • #5
          Hello,

          it's working with tihs solution:

          Code:
          var pdf_str = msg['tag00420011'].toString();
          var hexarray = pdf_str.split('\\');
          var len = hexarray.length;
          var bytearray = new Array(len);
          
          for (var ii = 0 ; ii < len ; ii++)
          {
              var sbyte = java.lang.Integer.parseInt(hexarray[ii], 16);
              if (sbyte > 127) {
                   sbyte -= 256;
              }
          }

          Comment

          Working...
          X