Esempio n. 1
0
 /// <summary>
 /// Moves a page after another
 /// </summary>
 public void InsertAfter(HtmlPdfPage page, HtmlPdfPage after)
 {
     this._Pages.Remove(page);
     this._Pages.Insert(
         Math.Min(this._Pages.IndexOf(after) + 1, this._Pages.Count),
         page);
 }
Esempio n. 2
0
 /// <summary>
 /// Moves a page before another
 /// </summary>
 public void InsertBefore(HtmlPdfPage page, HtmlPdfPage before)
 {
     this._Pages.Remove(page);
     this._Pages.Insert(
         Math.Max(this._Pages.IndexOf(before), 0),
         page);
 }
Esempio n. 3
0
        /// <summary>
        /// Appends and returns a new page for this document http://aspnettutorialonline.blogspot.com/
        /// </summary>
        public HtmlPdfPage AddPage()
        {
            HtmlPdfPage page = new HtmlPdfPage();

            this._Pages.Add(page);
            return(page);
        }
Esempio n. 4
0
 /// <summary>
 /// Removes the page from the document http://aspnettutorialonline.blogspot.com/
 /// </summary>
 public void RemovePage(HtmlPdfPage page)
 {
     this._Pages.Remove(page);
 }
Esempio n. 5
0
        public DataResponse CreatePDFFromHTMLFile(string HtmlStream, string Path)
        {
            DataResponse dr = new DataResponse();

            try
            {
                string domain           = System.Configuration.ConfigurationManager.AppSettings["domain"];
                string fileName         = DateTime.UtcNow.ToString("yyyyMMddHHmmss") + ".pdf";
                object TargetFile       = ServiceContext.Server.MapPath(Path) + "/" + fileName;
                string ModifiedFileName = string.Empty;
                string FinalFileName    = string.Empty;

                // To add a Password to PDF -http://aspnettutorialonline.blogspot.com/
                HtmlToPdfBuilder builder = new HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
                HtmlPdfPage      first   = builder.AddPage();


                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(HtmlStream);

                var node = htmlDoc.DocumentNode.SelectSingleNode("//*[contains(@class,'print')]");
                if (node != null)
                {
                    node.ParentNode.RemoveChild(node, true);
                    HtmlStream = HtmlStream.Replace(node.InnerHtml, "");
                }

                // Set image full path
                HtmlStream = HtmlStream.Replace("<img src=\"/", "<img src=\"" + domain + "/");
                HtmlStream = HtmlStream.Replace("width=\"730\"", "");

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(HtmlStream);

                doc.DocumentNode.Descendants()
                .Where(n => n.Name == "script" || n.Name == "style")
                .ToList()
                .ForEach(n => n.Remove());

                first.AppendHtml(HtmlStream);
                byte[] file = builder.RenderPdf();
                File.WriteAllBytes(TargetFile.ToString(), file);

                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
                ModifiedFileName = TargetFile.ToString();
                ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

                string password = "******";
                iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, password, "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
                //http://aspnettutorialonline.blogspot.com/
                reader.Close();
                if (File.Exists(TargetFile.ToString()))
                {
                    File.Delete(TargetFile.ToString());
                }
                FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
                File.Copy(ModifiedFileName, FinalFileName);
                if (File.Exists(ModifiedFileName))
                {
                    File.Delete(ModifiedFileName);
                }

                dr.Status  = DataResponseStatus.OK;
                dr.Message = "PDF created successfully";
                dr.Content = Path + "/" + fileName;
            }
            catch (Exception ex)
            {
                dr.Status  = DataResponseStatus.InternalServerError;
                dr.Message = "Request failed";
                dr.Content = "";
                ex.Log();
                //throw ex;
            }
            return(dr);
        }