I have a report object which I can generate PDF by calling report.Publish(fileName, fileType) method and it works fine. However I need another method where I can generate stream bytes[] or the object. I tried using following code but it shows error because Report object is not serializable. My code:
//convert report object to bytesbyte[] bytes = ObjectToByteArray(report);
private static byte[] ObjectToByteArray(object obj) { if (obj == null) return null; BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { bf.Serialize(ms, obj);//ERROR comes here return ms.ToArray(); } }
any help is appreciated.
Thanks Miteva,
That helped.
Hello sarojanand@gmail.com,
The report Publish method has yet another overload, that takes two parameters: a stream and a file format.
In the sample project I have added the GetByteArrayFromStream method that uses the above approach and returns the byte array from a memory stream.
To get bytes from File.ReadAllBytes, I need to save the file first somewhere on disk and then capture it. I am looking for a method where I can just convert to bytes because I already got my Report object with all data and formatting. I dont want to save PDF file on user machine as there can be some permission issues.
You can simply use the File.ReadAllBytes method in the System.IO namespace and pass the path to the pdf file as parameter.
I have attached a small sample project that has an UltraGrid and two buttons on it. The “Export To PDF” button calls the ExportGridToPdf and exports the grid to a pdf file. The “Get Byte Array” button converts the previously created pdf file into a byte array.
You can check out the Microsoft documentation for more information on the above method on https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx .
Please note that Linux is not supported operating systems under which our libraries are tested. However, there might be other reasons behind not opening the exported PDF file under Linux.
Basically I am trying to send this report to a remote printer via API.
This printer is connected on a remote Linux server which accepts byte[]/streams and then it convert them to PDF and then print it. For this reason I am trying to convert it to steam bytes.
Another issue: when I generate PDF using publish method. It opens fine on my local PC but same PDF if I try to open on remote Linux server for printing, it not able to read it. Dont know what could be the reason.