PDF Encoding using StreamWrite and StreamReader

Hi All,

I'm looking for the correct Encoding (or method) to read and write PDF files.

When I use something like the code below, the generated PDF (Destination.PDF) is not the same as the Source.PDF used.

StreamReader objSR = new StreamReader(@"c:\temp\Source.pdf", Encoding.ASCII);

StreamWriter objSW = new StreamWriter(@"c:\temp\Destination.PDF", false, Encoding.ASCII);

objSW.Write(objSR.ReadToEnd());

objSW.Close();

objSR.Close();

Thanks in advance!



Answer this question

PDF Encoding using StreamWrite and StreamReader

  • kymaita

    Since PDF can contain binary data and supports multiple encodings, I must once again warn against using anything other than binary access. Just because this is working with some files does not mean it will work with all, unless you can exercise strong control over the creation of these PDF's. Databases are quite capable of storing binary data, although it's generally not a great idea to store large files in databases anyway.



  • MiXen

    i need to convert .. excel file into pdf through .net
    plz help me


  • Quadrillion

    Hi,

    That may work, thanks - but I needed it in a string format as apposed to a byte format - because I save the string to database.

    I was using the incorrect Encoding. Default (also known as ANSI encoding) works:

    //Use ANSI encoding - also known as Encoding.GetEncoding(1252)

    Encoding objEncoding = Encoding.Default;

    StreamReader objSR = new StreamReader(@"c:\temp\Source.pdf", objEncoding);

    StreamWriter objSW = new StreamWriter(@"c:\temp\Destination.PDF", false, objEncoding);

    objSW.Write(objSR.ReadToEnd());

    objSW.Close();

    objSR.Close();


  • pinoyz

    PDF's can contain binary content, so rather use BinaryReader & Writer instead.



  • PDF Encoding using StreamWrite and StreamReader