在c#中将字节数组转换为pdf [英] Convert a byte array to pdf in c#

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

问题描述

亲爱的所有人,





我需要一些帮助才能将字节数组来自DataBase转换为pdf。有人能给我一个C#中的例子吗?以下是我到目前为止使用System.IO的


Dear All,


I need some help in converting a byte array "Which Came From DataBase" to pdf. Could someone give me an example of how in C#? Here is what I have so far,

using System.IO;

string sFile = "c:\testpdf.pdf"; //Path
FileStream fs = File.Create(sFile);
BinaryWriter bw = new BinaryWriter(fs); 







我用这个将Pdf文件转换成字节数组:






And I have Used This To Convert The Pdf File Into Byte Array:

FileUpload1.SaveAs(filePathName);
byte[] picArray= System.IO.File.ReadAllBytes(filePathName);





提前感谢,



thanks in advance,

推荐答案

您似乎试图将数据库中的字节写入文件:为什么这会给您带来问题?如果您有字节,只需写下它们:

You seem to be trying to write a byte arry from a database into a file: why is this giving you problems? If you have the bytes, just write them:
File.WriteAllBytes(@"C:\testpdf.pdf", myArrayOfBytes);

(您应该知道这可能会失败 - 权限问题通常会阻止写入HDD的根目录 - 请尝试使用子目录)

(You should be aware that this is likely to fail - permissions problems often prevent writes to the root directory of a HDD - try using a subdirectory instead)


Response.Clear(); 
MemoryStream ms = new MemoryStream(pdfBytearray); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf"); 
Response.Buffer = true; 
ms.WriteTo(Response.OutputStream); 
Response.End(); 


如果是字节数组,可以将其写入磁盘,以便将其保存为* pdf文件。



要么,你可以将字节写入响应输出流,用户将提示下载并保存文件。



If it is a byte array, you can write it to disk so it becomes saved as *pdf file.
or
either, you can write the bytes to the response output stream and user will be prompt to download and save the file.

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] pdf;
Response.AddHeader("Content-Length", response.Length.ToString());
Response.BinaryWrite(pdf);
Response.End();





您能否看一下这段代码。我从下面给出的链接中得到了它。



http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/6810a67f-66d9-4ce4-87e5-06dbbb754730/ [ ^ ]



希望这会对你有帮助。



Can you please have a look at this code. I got it from the link given below.

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/6810a67f-66d9-4ce4-87e5-06dbbb754730/[^]

Hope this will help you.


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

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