Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
AnswerRe: Polymorphism Pin
OriginalGriff13-Oct-16 19:07
mveOriginalGriff13-Oct-16 19:07 
GeneralRe: Polymorphism Pin
Peter_in_278013-Oct-16 19:37
professionalPeter_in_278013-Oct-16 19:37 
GeneralRe: Polymorphism Pin
OriginalGriff13-Oct-16 19:44
mveOriginalGriff13-Oct-16 19:44 
AnswerRe: Polymorphism Pin
Nathan Minier14-Oct-16 1:56
professionalNathan Minier14-Oct-16 1:56 
QuestionHow to integrate Facebook marketing API in window application using c# Pin
Member 1063574713-Oct-16 3:11
Member 1063574713-Oct-16 3:11 
AnswerRe: How to integrate Facebook marketing API in window application using c# Pin
Eddy Vluggen13-Oct-16 3:38
professionalEddy Vluggen13-Oct-16 3:38 
QuestionHTMLtoPDF Downloader Pin
Member 1279065812-Oct-16 18:29
Member 1279065812-Oct-16 18:29 
AnswerRe: HTMLtoPDF Downloader Pin
Member 1279065812-Oct-16 18:41
Member 1279065812-Oct-16 18:41 
i have used
C#
using iTextSharp.text;
using iTextSharp.text.pdf;


-------------//usage///
C#
 var baseUrl = SecurityContext.BaseUrl;

string urlString = string.Format("{1}Downloads/SalesOrderInvoice.aspx?id={0}", OrderId, baseUrl);

                   Uri url = new Uri(urlString);

                   IDownloader htmlToPDFDownloader = new HtmlToPDFDownloader(url);

                   string fileName = string.Format("Invoice for Order Referecne. {0}", OrderCode);

                   htmlToPDFDownloader.Download(fileName);


---------------------------------
Create a interface IDownloader.cs
-------------------------
C#
public interface IDownloader
    {
        object GetDownloadable();

        void Download(string fileName);
    }


-------------------------
HTMLToPDFDownloader.cs
---------------------------
C#
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.draw;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
namespace sampleproj.Client
{
    public class HtmlToPDFDownloader : IDownloader
    {
        private string htmlString;

        public HtmlToPDFDownloader(string htmlString)
        {
            this.htmlString = htmlString;
        }

        private string ImageToBase64(string url)
        {
            WebClient client = new WebClient();
            byte[] bytes ;
            if(url.StartsWith("http"))
            {
                bytes = client.DownloadData(url);
            }
            else
            {
                string physicalPath = HttpContext.Current.Server.MapPath(string.Format("~{0}", url));
                bytes = File.ReadAllBytes(physicalPath);                
            }

            string base64String = Convert.ToBase64String(bytes);

            return "data:image/png;base64," + base64String;
        }

        //private string GetDownloadedContent(Uri url)
        //{   
        //    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            
        //    webrequest.Method = "GET";

        //    webrequest.ContentType = "text/html";

        //    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

        //    Stream stream = webresponse.GetResponseStream();

        //    StreamReader r = new StreamReader(stream);

        //    return r.ReadToEnd();
        //}

        public HtmlToPDFDownloader(Uri url)
        {
            WebClient client = new WebClient();

            client.Headers.Add("_uidqqweb4324", ConfigurationManager.UserId);

            this.htmlString = client.DownloadString(url);

            //this.htmlString = GetDownloadedContent(url);

            foreach (Match m in Regex.Matches(htmlString, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase | RegexOptions.Multiline))
            {
                string src = m.Groups[1].Value;

                //string locaPath = new Uri(src).LocalPath;

                //string physicalPath = HttpContext.Current.Server.MapPath(string.Format("~{0}", locaPath));

                //htmlString = htmlString.Replace(src, physicalPath);

                //string src = m.Groups[1].Value;

                //string locaPath = new Uri(src).ToString();
                //locaPath = HttpContext.Current.Server.MapPath(locaPath);
                string physicalPath = ImageToBase64(src); //Server.MapPath(string.Format("~{0}",locaPath));

                htmlString = htmlString.Replace(src, physicalPath);
            }
        }


        private byte[] GetStream()
        {
            MemoryStream stream = new MemoryStream();

            var htmlContent = this.htmlString;

            var converter = new NReco.PdfGenerator.HtmlToPdfConverter();

            converter.Margins = new NReco.PdfGenerator.PageMargins
            {
                Bottom = 5,
                Left = 2.5f,
                Right = 2.5f,
                Top = 5
            };

            var pdfBytes = converter.GeneratePdf(htmlContent);

            return pdfBytes;
        }

        public object GetDownloadable()
        {
            return GetStream();
        }

        public void Download(string fileName)
        {
            var pdfBytes = GetStream();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf; file";
            HttpContext.Current.Response.AddHeader("content-length", pdfBytes.Length.ToString());

            fileName = fileName + "_" + DateTime.Now.ToString("ddMMMyyy") + ".pdf";

            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;" + "filename=" + fileName);
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            HttpContext.Current.Response.BinaryWrite(pdfBytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
        }
    }
}


-----------------------------
SecurityContext.cs
-----------------------------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnergyEngineers.Client
{
    public class SecurityContext
    {
        public int CurrentUserId
        {
            get
            {
                return 1;
            }
        }

        public static string BaseUrl
        {
            get
            {
                return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;
            }
        }
    }
}

AnswerRe: HTMLtoPDF Downloader Pin
Nathan Minier13-Oct-16 3:13
professionalNathan Minier13-Oct-16 3:13 
AnswerRe: HTMLtoPDF Downloader Pin
yellow flower18-Oct-16 1:39
yellow flower18-Oct-16 1:39 
QuestionC# Stopwatch - DateTime And Timer Tick - Is there a better way to run the Stopwatch Pin
Member 1275660312-Oct-16 12:05
Member 1275660312-Oct-16 12:05 
AnswerRe: C# Stopwatch - DateTime And Timer Tick - Is there a better way to run the Stopwatch Pin
Midi_Mick12-Oct-16 19:05
professionalMidi_Mick12-Oct-16 19:05 
AnswerRe: C# Stopwatch - DateTime And Timer Tick - Is there a better way to run the Stopwatch Pin
OriginalGriff12-Oct-16 22:47
mveOriginalGriff12-Oct-16 22:47 
AnswerRe: C# Stopwatch - DateTime And Timer Tick - Is there a better way to run the Stopwatch Pin
Bernhard Hiller13-Oct-16 22:08
Bernhard Hiller13-Oct-16 22:08 
AnswerRe: C# Stopwatch - DateTime And Timer Tick - Is there a better way to run the Stopwatch Pin
omeecode15-Oct-16 8:56
omeecode15-Oct-16 8:56 
QuestionHTML.Kendo.ComboBox [Error: Object doesn't support property or method 'requestData'] Pin
Atul Shriram rane11-Oct-16 17:57
Atul Shriram rane11-Oct-16 17:57 
AnswerRe: HTML.Kendo.ComboBox [Error: Object doesn't support property or method 'requestData'] Pin
NotPolitcallyCorrect12-Oct-16 4:53
NotPolitcallyCorrect12-Oct-16 4:53 
GeneralRe: HTML.Kendo.ComboBox [Error: Object doesn't support property or method 'requestData'] Pin
Pete O'Hanlon12-Oct-16 4:59
mvePete O'Hanlon12-Oct-16 4:59 
GeneralRe: HTML.Kendo.ComboBox [Error: Object doesn't support property or method 'requestData'] Pin
NotPolitcallyCorrect12-Oct-16 5:47
NotPolitcallyCorrect12-Oct-16 5:47 
QuestionI have a search button and datagrid in c# and SQL Pin
antonio_dsanchez11-Oct-16 11:16
antonio_dsanchez11-Oct-16 11:16 
AnswerRe: I have a search button and datagrid in c# and SQL Pin
Dave Kreskowiak11-Oct-16 11:23
mveDave Kreskowiak11-Oct-16 11:23 
AnswerRe: I have a search button and datagrid in c# and SQL Pin
antonio_dsanchez11-Oct-16 11:31
antonio_dsanchez11-Oct-16 11:31 
GeneralRe: I have a search button and datagrid in c# and SQL Pin
Swinkaran11-Oct-16 16:00
professionalSwinkaran11-Oct-16 16:00 
GeneralRe: I have a search button and datagrid in c# and SQL Pin
Pete O'Hanlon11-Oct-16 20:35
mvePete O'Hanlon11-Oct-16 20:35 
QuestionClosest standard value Pin
Pavlex410-Oct-16 10:44
Pavlex410-Oct-16 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.