Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Aug 2008
Location: Southern California
Posts: 1
#1   Aug 14 '09
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:
  2.  
  3. using System.Text;
  4. using System.IO;
  5.  
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();
  5.  
  6. //reads the response stream
  7. StreamReader sr = new StreamReader (reqStr,System.Text.Encoding.Unicode);
  8.  
  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.



Reply

Tags
httpwebresponse, read pdf url, streamreader, streamwriter