开发者

Converting byte[] of a PDF file original source to MemoryStream for loading into PDF viewer? (component one)

I'm working with a ComponentOne (C1) silverlight PDF viewer control. It has a "LoadDocument" method that accepts a "Stream".

I'm making an HTTP get call from my client app to get a PDF document. This document, on the server side, has been streamed in through File.ReadAllBytes(), then converted to a base64 string using Convert.ToBase64String(). This string is sent across the wire back to my silverlight app where it's then reversely converted back into a byte array with Convert.FromBase64String(val). Then I'm creating a MemoryStream with that byte array and passing "LoadDocument()" that memory stream.

The viewer is rendering nothing. It shows the toolbar and scrollbars, b开发者_运维百科ut the contents are blank and the save button is grayed out, suggesting that no document loaded. I know for certain the file made it across because the byte array size on the client matches teh byte array pre-conversion on the server side.

Here's my code: (in the interest of time/space, i've truncated, removing validation, etc.)

SERVERSIDE

string sendingToClient = Convert.ToBase64String(File.ReadAllBytes(filePath))

CLIENTSIDE

byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
docViewer.LoadDocument(stream);

edit As a potential workaround, I attempted to save the file into isolated storage with a ".pdf" extension. Then I use the IsolatedStorageFileStream to send to LoadDocument(). I've come to an actual error, it now says "PdfParserException was unhandled by user code: invalid file format (missing pdf header)"

Can anyone shed some light on this PDF header?


Here is an experiment I would conduct.

Add a button to your Xaml and on click use OpenFileDialog to get a FileInfo. From that FileInfo use its Open method to get a stream and pass that to docViewer.LoadDocument.

Now run it, click the button and select the same PDF document you are trying to send from the server.

If that succeeds you need to continue investigating your server streaming strategy. On the other hand if you still have the same problem, well it doesn't get more raw than that. Try other PDF files and start investigating the PDF component. Have you ever actually used it successfully, if so how does this current usage differ.


you should get the stream pointer back to 0 ,so this should do the trick

byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
stream.Position = 0;
docViewer.LoadDocument(stream);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜