以PDF格式保存二进制文件

时间:2014-07-25 09:44:06

标签: c# file-io binaryfiles

我正在尝试在PDF文件中保存PDF格式的Web API响应。

这是我正在做的事情:

FileStream fs = File.Create("API_response.pdf", 5120, FileOptions.None);
BinaryWriter F = new BinaryWriter(fs);
//ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba =  Encoding.Unicode.GetBytes(response.data);

F.Write(ba);
F.Close();
fs.Close();

但是当我试图打开API_response.pdf文件时,它说该文件已损坏且无法修复。

这是我从API获得的响应,我将其转储到API_response.pdf文件中,

%PDF-1.3
%âãÏÓ
1 0 obj<</Producer(htmldoc 1.8.27 Copyright 1997-2006 Easy Software Products, All Rights Reserved.)/CreationDate(D:20140725043937+0500)>>endobj
2 0 obj<</Type/Encoding/Differences[ 32/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quotesingle/parenleft/parenright/asterisk/plus/comma/hyphen/period/slash/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright/asciicircum/underscore/grave/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde 160/space/exclamdown/cent/sterling/currency/yen/brokenbar/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot/minus/registered/macron/degree/plusminus/twosuperior/threesuperior/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine/guillemotright/onequarter/onehalf/threequarters/questiondown/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis/yacute/thorn/ydieresis]>>endobj
3 0 obj<</Length1 1279/Length2 92942/Length3 544/Filter/FlateDecode/Length 93901     >>stream
xœ»s”dm³=X¶Ùå,WuÙ¶mÛ¶mÛ¶mÛ¶ÝÕe›]˜~¿û»ßoîÌ?³2WžóìˆØODœg­<¹’”P^‰FÀØÎÐDÔÎÖ™†–ž kacèâ$cg+M£hbæø²Â’*[8[›ü/+©ª‰£“…-çû 9š8ÿ„
œÿº+›»

请让我知道我错过了什么?

1 个答案:

答案 0 :(得分:1)

BinaryWriter用于二进制格式的序列化数据。它不是将任意数据不变地写入流中。事实上,流已经可以做到这一点。