字节数组为pdf [英] byte array to pdf

查看:552
本文介绍了字节数组为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换成存储在SQL列到PDF文件的内容

I am trying to convert content of a file stored in a sql column to a pdf.

我用下面这段代码:

        byte[] bytes;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, fileContent);
        bytes = ms.ToArray();
        System.IO.File.WriteAllBytes("hello.pdf", bytes);

生成的PDF是腐败在这个意义上,当我在记事本中打开PDF ++,我看到了一些垃圾报头(它是相同不论fileContent的)。垃圾头是NUL SOH NUL NUL NUL ....

The pdf generated is corrupt in the sense that when I open the pdf in notepad++, I see some junk header (which is same irrespective of the fileContent). The junk header is NUL SOH NUL NUL NUL ....

推荐答案

您不应该使用的BinaryFormatter 此 - 这是序列化.NET类型为二进制文件,使他们能够再次为.NET类型回读

You shouldn't be using the BinaryFormatter for this - that's for serializing .Net types to a binary file so they can be read back again as .Net types.

如果它存储在数据库中,希望为 VARBINARY - 那么所有你需要做的就是从字节数组(这将取决于您的数据访问技术 - EF和LINQ to SQL的,例如,将创建一个映射,使得它琐碎得到一个字节数组),然后为你的代码的最后一行做到这一点写入文件。

If it's stored in the database, hopefully, as a varbinary - then all you need to do is get the byte array from that (that will depend on your data access technology - EF and Linq to Sql, for example, will create a mapping that makes it trivial to get a byte array) and then write it to the file as you do in your last line of code.

运气好的话 - 我希望 fileContent 这里是字节数组?在这种情况下,你可以做

With any luck - I'm hoping that fileContent here is the byte array? In which case you can just do

System.IO.File.WriteAllBytes("hello.pdf", fileContent);

这篇关于字节数组为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆