将PDF转换为bytes []和bytes []转换为字符串,反之亦然 [英] Convert PDF to bytes[] and bytes[] to string and vice versa

查看:65
本文介绍了将PDF转换为bytes []和bytes []转换为字符串,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要将PDF转换为bytes []和bytes []转换为字符串,反之亦然,我正在使用以下代码。



这对于html文档来说是完美的,但不适用于pdf文件。

Hi
I am in a need to convert PDF to bytes[] and bytes[] to string and vice versa and i am using the following code for this.

This works perfectlty for html documents but not for pdf's.

//convert pdf to bytes
FileStream fs;
fs = File.Open(fileName, FileMode.Open);
byte[] bytBytes1 = new byte[fs.Length];
fs.Read(bytBytes1, 0, bytBytes1.Length);
fs.Close();

//convert bytes to string
string strModified = Encoding.Unicode.GetString(bytBytes1);

//convert string to bytes
byte[] b =Encoding.Unicode.GetBytes(strModified);

//to open the attachment
Response.AddHeader("Content-disposition", "attachment;filename=filename.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(b);
Response.End();



任何人都可以帮我解决这个问题。



谢谢事先


can anyone help me in this issue.

Thanks in advance

推荐答案

你为什么这样做,只需使用;

Why are you doing that, just use;
Response.BinaryWrite(bytBytes1);



您不需要将其转换为字符串然后再转换为字节。


You do not need to convert it to a string then to a byte again.


请不要按'回答',除非你正在发布答案。编辑你的帖子以添加细节。



你的客户要求这个吗?然后他们是个白痴。 HTML是一个字符串。 PDF不是字符串。所以,它无法完成。你不应该与客户打交道,除非你对你所使用的技术有足够的了解,告诉你的客户他们想象的东西实际上是不可能的。
Please don't push 'Answer' unless you are posting an answer. Edit your post to add detail.

Your client is asking for this ? Then they are an idiot. HTML IS a string. A PDF is NOT a string. So, it can't be done. You should not be dealing with clients unless you know enough about the technology you're using to tell your client if the thing they are imagining is actually not possible.


没有我喜欢的情况我必须以字符串形式发送附件,在另一端我们的客户端将获取字符串并将其转换为附件。所以我必须将其转换为字符串。
No i have a situation like i have to send an attachment as a string and in the other end our client will get the string and convert it to attachments. so i have to convert it to string.


这篇关于将PDF转换为bytes []和bytes []转换为字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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