编辑:这是解决的。谢谢杰里米·拉克曼(Jeremy Lakeman)的评论,这使我走上了正确的轨道。
我有一个PDF文件内容字符串,我试图用来产生PDF文件。
编辑:编码的字符串看起来像这样: image 或Google doc: https://docs.google.com/document/d/1i4utmaidyrtvcjd7005jnfp0gtoaauif0hrgbb3k6wk6w1s/edit?usp = sharing
问题的一部分当然没有得到任何编码的经验为此原谅我。
我正在尝试处理这样的文件的转换和编写:
var bytes = System.Text.Encoding.Unicode.GetBytes(FileContentString);
File.WriteAllBytes(filePath, bytes);
这确实会产生一个PDF文件,但是当我尝试打开它时,Edge PDF Viewer说:“我们无法打开此文件。”
除Unicode外,我还尝试将字符串作为UTF-8和UTF-32处理,结果多样。所有这些选项都会写入文件,但是该文件不能打开或显示空白页。
您知道这是什么样的编码吗?我肯定不会。或者,我只是在总体上处理这个错误吗?
EDIT: This is solved. Thank you Jeremy Lakeman for your comment, it put me on the right track.
I have a PDF file content string that I am trying to use to produce a PDF file.
EDIT: The encoded string looks like this: image or google doc: https://docs.google.com/document/d/1I4UtmAIDYRTvCjd7005jnfP0GTOAauiF0hRGB3K6w1s/edit?usp=sharing
Part of the problem is certainly that I have not dealt with any encoding so far in my experience, so please forgive me for that.
I'm trying to handle the conversion and writing of the file like this:
var bytes = System.Text.Encoding.Unicode.GetBytes(FileContentString);
File.WriteAllBytes(filePath, bytes);
This does produce a PDF file, but when I try to open it, Edge PDF Viewer says, "We can't open this file."
In addition to Unicode, I've tried handling the string as UTF-8 and UTF-32 with varied results. All of these options do write the file, but the file either does not open or displays a blank page instead.
Do you know what kind of encoding this is? I sure don't. Or, am I just handling this wrong in general?
发布评论
评论(1)
专家提示:切勿将文件的内容读为字符串,然后如果可以避免使用,则尝试将其转换回原始字符。在这种情况下,我的内容被读取了:
只需将其切换到:
var content.content.content.contasbytearrayasync();
,然后:
file> file.writeallbytes(filepath,content,content);
做到了。
Pro tip: never read a file's contents as a string and then try to convert it back to raw bytes if you can avoid it. In this case, my contents were being read like so:
Simply switching it to:
var content = response.Content.ReadAsByteArrayAsync();
And then:
File.WriteAllBytes(filePath, content);
Did the trick.