none
PDF Encoding using StreamWrite and StreamReader

    Question

  • 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!

     

    Monday, February 05, 2007 3:30 PM

Answers

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

    Tuesday, February 06, 2007 8:05 AM

All replies

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

    Monday, February 05, 2007 5:08 PM
  • 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();

    Tuesday, February 06, 2007 8:05 AM
  • 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.

    Tuesday, February 06, 2007 1:20 PM
  • i need to convert .. excel file into pdf through .net
    plz help me
    Wednesday, May 16, 2007 9:20 AM