Embedded PDF stream to XML in D365FO X++
We can use either code 1 or code 2,
Code:1
Public System.Xml.XmlElement embed(
System.Xml.XmlElement _parentElement,
Name _elementName,
System.IO.Stream _pdfStream)
{
System.Xml.XmlDocument document;
System.Xml.XmlElement element;
System.Xml.XmlDocument ownerDocument;
System.Byte[] bytes;
System.IO.BinaryReader binaryReader;
System.IO.Stream baseStream;
System.IO.StreamReader streamReader;
System.Object streamObject;
System.Object bytesObject;
System.Xml.XmlCDataSection cdata;
int streamLength;
int bytesLength;
str base64data;
element = document.CreateElement(_elementName);
if (_pdfStream != null)
{
baseStream = _pdfStream;
streamObject = baseStream.get_Length();
streamLength = streamObject;
binaryReader = new System.IO.BinaryReader(baseStream);
bytes = binaryReader.ReadBytes(streamLength);
baseStream.Close();
ownerDocument = element.get_OwnerDocument();
bytesObject = bytes.get_Length();
bytesLength = bytesObject;
base64data = System.Convert::ToBase64String(bytes, 0, bytesLength);
cdata = ownerDocument.CreateCDataSection(base64data);
element.AppendChild(cdata);
}
_parentElement.AppendChild(element);
return element;
}
Code 2:
System.Xml.XmlElement embed(
System.Xml.XmlElement _parentElement,
Name _elementName,
System.IO.Stream _pdfStream)
{
element = document.CreateElement(_elementName);
if (_pdfStream != null)
{
using (System.IO.Stream fileStream = _pdfStream)
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
fileStream.CopyTo(memoryStream);
base64data = ERConversionUtils::StreamToBase64(memoryStream);
ownerDocument = element.get_OwnerDocument();
cdata = ownerDocument.CreateCDataSection(base64data);
element.AppendChild(cdata);
}
}
}
_parentElement.AppendChild(element);
return element;
}
No comments:
Post a Comment