title: Converted PDF to Base64 string representation not openable when converted back
body: <p>I need to have a PDF converted into a base 64 string such that I can send it in an XML file. I am using an excel spreadsheet to store all the data we need to go into the XML and then some VBA code that prints the XML to a file. The recipient of said XML file cannot open my PDFs. Adobe says it is either not a .pdf file or it is corrupt. I did some extra code to test my original conversion and I am getting the same problem also. The XSD specifies that data should be of type xsd:base64Binary</p>
  
  <p>The first function is to generate the string that gets sent off as XML. The second function is called from the first function and is purely for my testing purposes.</p>
  
  <p>I have found this code on other stackoverflow threads and pieced it together from a few sources, but it differs slightly in the different constant values used (e.g. "Base64Data", "b64" and "Base64" are all inputs I have seen for objXML.createElement). I also have a feeling I have gotten the text/binary and read/write functions in reverse order. A string is generated that looks very much like base64 binary, although there are some parts that look suspiciously repetative when I had a quick scan through the produced XML.</p>
  
  <pre><code>Function processBase64Binary(ByVal filePath As String) As String
  
  'define objects used
  Dim mstream As Stream
  Dim objXML As MSXML2.DOMDocument
  Dim objNode As MSXML2.IXMLDOMElement
  
  'load the data from the filepath provided
  Set mstream = New Stream
  mstream.Type = adTypeBinary
  mstream.Open
  mstream.LoadFromFile filePath
  
  'convert to base 64 binary and convert to a string to be used in XML
  Set objXML = New MSXML2.DOMDocument
  Set objNode = objXML.createElement("Base64Data")
  objNode.DataType = "bin.base64"
  objNode.nodeTypedValue = mstream.Read()
  myText = objNode.Text
  testDecode (myText)
  processBase64Binary = myText
  
  End Function
  
  Sub testDecode(ByVal strData As String)
  
  Dim mstream As Stream
  Dim objXML As MSXML2.DOMDocument
  Dim objNode As MSXML2.IXMLDOMElement
  
  Set objXML = New MSXML2.DOMDocument
  Set objNode = objXML.createElement("Base64Data")
  objNode.DataType = "bin.base64"
  objNode.Text = strData
  decodeBase64 = objNode.nodeTypedValue
  
  Set mstream = New Stream
  mstream.Type = adTypeText
  mstream.Open
  mstream.WriteText (strData)
  mstream.SaveToFile ("D:\tested5.pdf")
  
  End Sub
  </code></pre>