0

Below is the code i use to read a web page, i can read from an outer web page but i need to read the local html class.

Document document = new Document();
            try
            {
                PdfWriter.GetInstance(document, new FileStream("C:\\teklif.pdf", FileMode.OpenOrCreate));
                document.Open();
                WebClient wc = new WebClient();
                
                string HtmlText = wc.DownloadString("http://localhost:60972/Reports/Index");
                Response.Write(HtmlText);
                List<IElement> HtmlArrayList = HTMLWorker.ParseToList(new StringReader(HtmlText), null);
                for (int k = 0; k < HtmlArrayList.Count; k++)
                {
                    document.Add((IElement)HtmlArrayList[k]);
                }

                document.Close();
            }
            catch
            {
            }
            
            return View();

By this way, i can't already access the localhost because of the active directory restriction.

What i need is something like this.

string HtmlText = wc.DownloadString("Reports/Index.cshtml");

Or is the way sending the html object should be via AJAX call?

Tia.

Ensar Turkoglu
  • 124
  • 2
  • 14
  • It sounds like your real question is how to use `WebClient` with authentication, right? There's a couple of options, try [this](http://stackoverflow.com/a/1883697/231316) and [this](http://stackoverflow.com/a/11118861/231316) – Chris Haas Mar 28 '16 at 18:22
  • Actually what i ask is i do not want to use string HtmlText = wc.DownloadString("http://localhost:60972/Reports/Index"); i want to use sth like string HtmlText = wc.DownloadString("Reports/Index.cshtml"); no way for it? – Ensar Turkoglu Mar 30 '16 at 08:18
  • "Reports/Index.cshtml" - that does not sound like html. You should not expect any server side program code to be executed by the xmlworker. – mkl Mar 30 '16 at 08:19
  • @mkl i am using htmlworker actually. – Ensar Turkoglu Mar 30 '16 at 08:28
  • *htmlworker* - ah, the precursor of the xmlworker which has been declared deprecated. That does not help at all. – mkl Mar 30 '16 at 08:46
  • @mkl how can i help you? – Ensar Turkoglu Mar 30 '16 at 08:53
  • Simply said, it is not clear what you want. You say you cannot access your target page due to access restrictions. So the obvious choice would be to *give* your application access and then authenticate as @Chris shows. Alternatively you can remove the restrictions on the web server, e.g. allowing unauthenticated access from localhost. – mkl Mar 30 '16 at 09:37
  • Well, what i mean was not breaking the restriction. It was just the "even if want to use the link it gives me unauthorized exception" part. Just forget it. My main issue issue is as i specified in the title, getting the whole html code and converting it to pdf. Not the link. I wonder if this is possible. Hope it is clear now. – Ensar Turkoglu Mar 30 '16 at 10:53
  • Most likely your issue then will be the one I hinted at in my first comments: `Index.cshtml` seems to indicate that **the source is not html** but some html/code mix which usually is *executed in the webserver making use of specific webserver resources* to eventually produce html to show in a browser or convert tho pdf. ITextSharp does not run such code but instead likely will recognize it as non-html and reject the input. – mkl Mar 30 '16 at 11:10
  • Okay, let's assume i changed it to index.html. So how would that be? Like it's done [here](http://stackoverflow.com/questions/28923080/itextsharp-can-not-convert-all-html-to-pdf) – Ensar Turkoglu Mar 30 '16 at 11:19
  • 1
    If you just want to read a string from a file _and not execute any ASP.Net, MVC, Razor, JavaScript or whatever code_ then instead of a `WebClient` you can just use [`System.IO.File.ReadAllText()`](https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx) – Chris Haas Mar 30 '16 at 13:16

0 Answers0