By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Reading a PDF from web page using URL (.NET)

P: 1
Hello all,

This isn't rocket science to most, but I had a hard time figuring out how to read a PDF via URL and saving it to disk. You can use the PDFBox dll for this, but for those who want to use pure .NET, here's some code:
Expand|Select|Wrap|Line Numbers
  1. The namespaces:
  3. using System.Text;
  4. using System.IO;
the script:
Expand|Select|Wrap|Line Numbers
  1. //sends the request, converts response to s stream
  2. HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(pdfURL);
  3. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  4. Stream reqStr = webResponse.GetResponseStream();
  6. //reads the response stream
  7. StreamReader sr = new StreamReader (reqStr,System.Text.Encoding.Unicode);
  9. //writes stream to a PDF file
  10. StreamWriter sw = new StreamWriter(savePathPDF, false, System.Text.Encoding.Unicode);
  11. sw.Write(sr.ReadToEnd());
  12. sw.Close();
Hopes this helps.
Aug 14 '09 #1
Share this Article
Share on Google+