user1550951
user1550951

Reputation: 379

convert PDF to byte

I have a WCF service which stores documents(PDF files) uploaded by a user to a file server.Now,I want to convert these PDF files to bytes so an iOS client can download them on iPad.I do not want to store the PDFs as BLOB in SQL,just convert into bytes and send them to iPad. Any links/sample code to achieve this is much appreciated.

Upvotes: 0

Views: 5705

Answers (2)

user1711092
user1711092

Reputation:

you can do it like below:

public byte[] ReadPDF(string filePath)
        {
            byte[] buffer;
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading
            }
            finally
            {
                fileStream.Close();
            }
            return buffer;
        }

Upvotes: 1

M4N
M4N

Reputation: 96571

You can simply read the PDF file into a byte array:

byte[] bytes = System.IO.File.ReadAllBytes(pathToFile);

Upvotes: 7

Related Questions