471,107 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,107 developers and data experts.

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

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.
Aug 14 '09 #1
0 10185

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Brett | last post: by
8 posts views Thread by Chad A. Beckner | last post: by
reply views Thread by David C | last post: by
2 posts views Thread by deepukutty | last post: by
reply views Thread by manishoz | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.