简体   繁体   中英

Saving a binary file in PDF format

I am trying to save a Web API response which is in PDF in a PDF file.

Here is what I am doing:

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();

But when I am trying to open the API_response.pdf file it says that file is damaged and could not be repaired.

This is the response I am getting from API and which I am dumping in API_response.pdf file,

%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ÿ„
œÿº+›»

Please let me know what I am missing?

BinaryWriter is for serialising data in a binary format. It's not for writing arbitrary data unchanged into a stream. In fact, a stream already can do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM