예제 #1
0
        static void Main(string[] args)
        {
            //not working

            XSettings.Register();
            XSettings.InstallLicense("cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc");
            XSettings.InstallSystemLicense("cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc");
            XSettings.InstallRedistributionLicense("cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc");
            XSettings.InstallTrialLicense("cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc");
            XSettings.Register();
            Doc theDoc = new Doc();

            theDoc.FontSize = 72;
            theDoc.AddTextStyled("<b>Gallia</b> est omnis divisa in partes tres, quarum unam incolunt <b>Belgae</b>, aliam <b>Aquitani</b>, tertiam qui ipsorum lingua <b>Celtae</b>, nostra <b>Galli</b> appellantur.");
            theDoc.Save("../../testingC.pdf"); //need licence
            theDoc.Clear();

            Signature theSig = (Signature)theDoc.Form["Signature"];

            theSig.Location = "here";
            theSig.Reason   = "test";
            //pfx + password
            theSig.Sign("../../../test.pfx", "123456");
            theDoc.Save("../../testingC signed.pdf");
        }
예제 #2
0
        public static List <InvoiceAttachment> ConvertPdfToPng(string pathToDocument, string folderPath)
        {
            XSettings.InstallLicense(ConfigurationManager.AppSettings["ABCPdfKey"]);
            pathToDocument = LocalizePath(pathToDocument);

            var convertedFileName    = string.Empty;
            var convertedAttachments = new List <InvoiceAttachment>();
            var pdfToConvert         = new Doc();

            pdfToConvert.Read(pathToDocument);

            // loop through the pages

            var n = pdfToConvert.PageCount;

            for (var i = 1; i <= n; i++)
            {
                pdfToConvert.PageNumber  = i;
                pdfToConvert.Rect.String = pdfToConvert.CropBox.String;
                convertedFileName        = $"{folderPath}\\{Path.GetFileNameWithoutExtension(pathToDocument)}{i}.png";
                pdfToConvert.Rendering.Save(convertedFileName);
                var newDoc = new InvoiceAttachment
                {
                    DocumentPath = convertedFileName
                };
                convertedAttachments.Add(newDoc);
                newDoc = null;
            }
            pdfToConvert.Clear();
            return(convertedAttachments);
        }
예제 #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints => {
                endpoints.MapGet("/", async context => {
                    context.Response.Clear();
                    try {
                        // When running as an App Service we need to install the license in code otherwise you will
                        // get a licensing exception. ABCpdf automatically installs a trial license if no license is
                        // found.
                        // Your trial license may be found by uncommenting the line below WHEN RUNNING ON YOUR LOCAL
                        // COMPUTER. NB XSettings.Key returns an empty string for purchased licenses.
                        // throw new Exception(XSettings.Key);

                        // Paste either the ABCpdf 12 key provided at time of purchase, or the value obtained from
                        // uncommenting the line above. This must be set correctly prior to running on Azure.
                        XSettings.InstallLicense("PASTE YOUR LICENSE HERE");

                        using (Doc doc = new Doc()) {
                            doc.HtmlOptions.Engine = EngineType.WebKit;
                            doc.HtmlOptions.Media  = MediaType.Screen;
                            int id = doc.AddImageUrl("https://www.websupergoo.com");

                            // AddImageUrl only adds the first page. The following code adds any additional pages:
                            while (doc.Chainable(id))
                            {
                                doc.Page = doc.AddPage();
                                id       = doc.AddImageToChain(id);
                            }
                            // Compress output:
                            for (int i = 1; i <= doc.PageCount; i++)
                            {
                                doc.PageNumber = i;
                                doc.Flatten();
                            }
                            // This will ensure page is served in a web-efficient manner:
                            doc.SaveOptions.Linearize = true;
                            // Finally serve the page:
                            context.Response.ContentType = "Application/Pdf";
                            await context.Response.BodyWriter.WriteAsync(doc.GetData());
                        }
                    } catch (Exception ex) {
                        await context.Response.WriteAsync(ex.Message);
                    }
                    await context.Response.CompleteAsync();
                });
            });
        }
예제 #4
0
        // GET: Home
        public ActionResult Index()
        {
            if (!XSettings.InstallLicense("<license_key>"))
            {
                throw new Exception("License installation failed.");
            }

            byte[] pdfContent;

            using (var ms = new MemoryStream())
            {
                ExportHtmlToPdf(ms);
                pdfContent = ms.ToArray();
            }

            return(File(pdfContent, "application/pdf", "test.pdf"));
        }
예제 #5
0
        static void Main(string[] args)
        {
            if (!XSettings.InstallLicense("<license_key>"))
            {
                throw new Exception("License installation failed.");
            }

            Console.WriteLine("1. Export PDF to HTML. (default)");
            Console.WriteLine("2. Export HTML to PDF.");
            var result = Console.ReadLine();

            switch (result)
            {
            case "2":
                ExportHtmlToPdf();
                break;

            default:
                ExportPdfToHtml();
                break;
            }

            Console.ReadLine();
        }
예제 #6
0
파일: CreateFile.cs 프로젝트: khabt/MVCAjax
        public static ResponseVal ConvertHmlToPDF(string QuoteISN, string StaffISN)
        {
            Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: ----------Begin----------");
            Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: QuoteISN: " + QuoteISN);
            Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: StaffISN: " + StaffISN);
            ResponseVal resVal = new ResponseVal();
            var         result = -1;

            //bool rsCheck = XSettings.InstallLicense("X/VKS08wmMtAun4hGNvFONzmS/QQY7hZ9Z2488LHIg8X5nu5Qx7dYsZhez00hWZRXd5Xim0uoXp3ifxwDtAusQ0lPTnPXR1401Y=");

            //bool rsCheck = XSettings.InstallLicense("XeJREBodo/8B4nxZb63WaYOgeuQZPdtypqn27rLhKmkRz3CDGnvCaco3Dn5c5nQFBw==");
            //bool rsCheck = XSettings.InstallLicense("XeJREBodo/8B7XFQaf2CPdzyKuccPdtypszj/8HiXH0n+nieP1jmdZIuAHpU7kIFBw==");
            //bool rsCheck = XSettings.InstallLicense("X/VKS0cNn5FipytaG9r2LN6gO9YNAb8f0JfhndPDLmJ14X+2ABmFVcU9cz81hwBrU4M7olk+wz9WgdFFKeN5mjkhfR3iZgJkr1Y=");

            bool rsCheck = XSettings.InstallLicense(Functions.GetConfig_CS("AbcPdfLicense"));

            if (!rsCheck)
            {
                resVal.Code = -1;
                resVal.Msg  = "Invalid Websupergoo license.";
                return(resVal);
            }

            var ds = Globals.DB.ExecuteQuery(string.Format("select MemberISN from Vw_SolarQuote where QuoteISN={0}", Functions.ConvertObjectToInt(QuoteISN)));

            if (Functions.IsEmpty(ds))
            {
                resVal.Code = -1;
                resVal.Data = null;
                resVal.Msg  = "QuoteISN is missing";
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: Msg: " + resVal.Msg);
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: ----------End----------");
                return(resVal);
            }

            var    row       = ds.Tables[0].Rows[0];
            var    MemberISN = row["MemberISN"].ToString();
            string url       = string.Format("{0}/Proposal.aspx?isn={1}&staffisn={2}",
                                             Functions.GetConfig_CS("AdminURL").TrimEnd('/'), QuoteISN, StaffISN);

            Globals.WriteLog("[ConvertHmlToPDF_CreateFile] -- url: " + url);

            string Temp          = Functions.GetConfig_CS("TemporaryDir");
            string outputPath    = HttpContext.Current.Server.MapPath("~/OutputFiles/Pdf");
            string sFileName     = "";
            string sFullPathTemp = "";

            #region Create PDF
            try
            {
                Doc pages = new Doc();
                try
                {
                    Doc doc = new Doc();
                    //doc.Rect.Inset(30, 10);
                    //doc.HtmlOptions.Engine = EngineType.MSHtml;
                    //doc.HtmlOptions.Engine = EngineType.Chrome;
                    //doc.HtmlOptions.Engine = EngineType.MSHtml;
                    //doc.SetInfo(0, "RenderDelay", "500");
                    //doc.SetInfo(0, "OneStageRender", 0);

                    doc.HtmlOptions.FontEmbed      = true;
                    doc.HtmlOptions.FontSubstitute = false;
                    doc.HtmlOptions.FontProtection = false;
                    doc.HtmlOptions.BrowserWidth   = 1200;

                    doc.HtmlOptions.PageCacheClear();
                    doc.HtmlOptions.UseScript = true;
                    //doc.HtmlOptions.OnLoadScript = "(function(){window.ABCpdf_go = false; setTimeout(function(){window.ABCpdf_go = true;}, 3000);})();";


                    // Render after 3 seconds
                    //doc.HtmlOptions.OnLoadScript = " (function(){"
                    //  + " window.external.ABCpdf_RenderWait();"
                    //  + " setTimeout(function(){ "
                    //  + " window.external.ABCpdf_RenderComplete(); }, 5000);"
                    //  + "})();";
                    //doc.SetInfo(0, "RenderDelay", "5000");
                    //doc.SetInfo(0, "OneStageRender", 0);

                    //Render after 3 seconds
                    //doc.HtmlOptions.OnLoadScript = "(function(){"
                    //  + " window.ABCpdf_go = false;"
                    //  + " setTimeout(function(){ window.ABCpdf_go = true; }, 10000);"
                    //  + "})();";

                    doc.Page = doc.AddPage();
                    int theID;

                    theID = doc.AddImageUrl(url);

                    while (true)
                    {
                        doc.FrameRect(); // add a black border
                        if (!doc.Chainable(theID))
                        {
                            break;
                        }
                        doc.Page = doc.AddPage();
                        theID    = doc.AddImageToChain(theID);

                        System.Threading.Thread.Sleep(500);
                    }

                    //doc.Rect.String = "100 50 500 150";

                    //for (int i = 1; i <= doc.PageCount; i++)
                    //{
                    //    doc.PageNumber = i;
                    //    doc.AddText("Page " + i.ToString());
                    //    //doc.FrameRect();
                    //}

                    pages.Append(doc);
                }
                catch (Exception ex2)
                {
                    resVal.Code = -1;
                    resVal.Data = null;
                    resVal.Msg  = ex2.Message;
                    Globals.WriteLog("[ConvertHmlToPDF_CreateFile] - Exception: " + ex2.Message);
                    Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: ----------End----------");
                    return(resVal);
                }

                if (!System.IO.File.Exists(Temp))
                {
                    System.IO.Directory.CreateDirectory(Temp);
                }

                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] Temp: " + Temp);

                sFileName     = "Proposal_" + DateTime.Now.Ticks.ToString() + ".pdf";
                sFullPathTemp = Path.Combine(Temp, sFileName);

                pages.Save(sFullPathTemp);
                pages.Clear();

                string sDocName = "Proposal_" + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;

                result = (int)Globals.DB.ExecuteStoredProc("xp_debtext_documenttask_insupd",
                                                           new string[] { "DocumentISN", "docFileName", "docName", "docType", "docPublic", "MemberISN" },
                                                           new object[] { 0, sFileName, sDocName, "Proposal", 1, MemberISN });

                var sPathFileDoc = Path.Combine(Functions.GetConfig_CS("FilesDir"), GetDocumentsPath(result, null));

                if (!System.IO.File.Exists(sPathFileDoc))
                {
                    System.IO.Directory.CreateDirectory(sPathFileDoc);
                }

                var sFileSave = Path.Combine(sPathFileDoc, sFileName);
                File.Copy(sFullPathTemp, sFileSave);
                //File.Delete(sFullPathTemp);
                resVal.Code = 1;
                resVal.Data = result;
                resVal.Msg  = "Success";

                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] sFullPathTemp: " + sFullPathTemp);
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] sPathFileDoc: " + sFileSave);
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] sFileName: " + sFileName);
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] DocumentISN: " + result);
            }
            catch (Exception ex)
            {
                resVal.Code = -1;
                resVal.Data = null;
                resVal.Msg  = ex.Message;
                Globals.WriteLog("[ConvertHmlToPDF_CreateFile] -- Exception: " + ex.Message);
            }
            Globals.WriteLog("[ConvertHmlToPDF_CreateFile]: ----------End----------");
            #endregion

            #region old
            // string fileName = DateTime.Now.Ticks + ".pdf";
            // string pathFileName = Path.Combine(Functions.GetConfig_CS("TemporaryDir"), fileName);
            // string ContentHtml = ReadFromFile(pathFileHtml);

            // //StringBuilder sb = new StringBuilder();
            // //sb.Append(ContentHtml);

            // //var css = ContentHtml.Substring(ContentHtml.IndexOf("<style>") + 7, ContentHtml.IndexOf("</style>") - 7 - ContentHtml.IndexOf("<style>"));
            //// var js = ContentHtml.Substring(ContentHtml.IndexOf("<script>") + 8, ContentHtml.IndexOf("</script>") - 8 - ContentHtml.IndexOf("<script>"));

            // StringReader sr = new StringReader(ContentHtml);

            // //StringWriter sw = new StringWriter();

            // //HtmlTextWriter hw = new HtmlTextWriter(sw);
            // //hw.Write(ContentHtml);
            // Document pdfDoc = new Document(PageSize.A4);
            // //HTMLWorker htmlparser = new HTMLWorker(pdfDoc);


            // using (MemoryStream memoryStream = new MemoryStream())
            // {
            //     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            //     pdfDoc.Open();

            //     //htmlparser.Parse(new StringReader(sw.ToString()));
            //     ////HTMLWorker.ParseToList(sr, new StyleSheet());
            //     //pdfDoc.Close();

            //     //StyleSheet ss = new StyleSheet();

            //     //ArrayList list = HTMLWorker.ParseToList(new StringReader(ContentHtml), ss);
            //     //foreach (IElement e in list)
            //     //{
            //     //    pdfDoc.Add(e);
            //     //
            //     //using (TextReader sReader = new StringReader(ContentHtml.ToString()))
            //     //{
            //     //    ArrayList list = HTMLWorker.ParseToList(sReader, new StyleSheet());
            //     //    foreach (IElement elm in list)
            //     //    {
            //     //        pdfDoc.Add(elm);
            //     //    }
            //     //}
            //     //pdfDoc.HtmlStyleClass = ContentHtml;
            //     //pdfDoc.JavaScript_onLoad = ContentHtml;
            //     //htmlparser.Parse(sr);
            //     // step 5
            //     //Image img = Image.GetInstance(ContentHtml);
            //     //pdfDoc.Add(img);
            //     //writer.AddJavaScript(ContentHtml);
            //     iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);

            //     //using (var srHtml = new StringReader(ContentHtml))
            //     //{

            //     //    //Parse the HTML
            //     //    iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, srHtml);
            //     //}

            //     //using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
            //     //{
            //     //    using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(ContentHtml)))
            //     //    {

            //     //        Parse the HTML
            //     //        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, msHtml, msCss);
            //     //    }
            //     //}

            //     pdfDoc.Close();

            //     byte[] bytes = memoryStream.ToArray();

            //     //BinaryFormatter bf = new BinaryFormatter();
            //     //MemoryStream ms = new MemoryStream();
            //     //bf.Serialize(ms, ContentHtml);
            //     //bytes = ms.ToArray();

            //     pathFileName = pathFileName + ".pdf";
            //     System.IO.File.WriteAllBytes(pathFileName, bytes);
            //     memoryStream.Close();
            //     File.Copy(Path.Combine(Functions.GetConfig_CS("TemporaryDir"), fileName + ".pdf"), Path.Combine(Functions.GetConfig_CS("FilesConvertPdf"), fileName + ".pdf"), true);
            //     resVal.Code = 1;
            //     resVal.Data = pathFileName;
            //     resVal.Msg = "Success";
            // }
            #endregion

            return(resVal);
        }
예제 #7
0
파일: Functions.cs 프로젝트: khabt/MVCAjax
        public static string ConvertHtmlToPDF(string filePathHTML)
        {
            WriteLog("[ConvertHmlToPDF_CreateFile]: ----------Begin----------");
            WriteLog("[ConvertHmlToPDF_CreateFile] filePathHTML: " + filePathHTML);
            string result  = string.Empty;
            bool   rsCheck = XSettings.InstallLicense("X/VKS08wmMtAun4hGNvFONzmS/QQY7hZ9Z2488LHIg8X5nu5Qx7dYsZhez00hWZRXd5Xim0uoXp3ifxwDtAusQ0lPTnPXR1401Y=");

            if (!rsCheck)
            {
                result = "Invalid Websupergoo license.";
                WriteLog("[ConvertHmlToPDF_CreateFile] result:  " + result);
                return(null);
            }
            string FilesDir = Functions.GetAppConfigByKey("FileDir");

            if (!System.IO.File.Exists(FilesDir))
            {
                System.IO.Directory.CreateDirectory(FilesDir);
            }

            string fileName = "File_Convert_PDF" + DateTime.Now.Ticks.ToString() + ".pdf";

            string fullPath = Path.Combine(FilesDir, fileName);

            WriteLog("[ConvertHmlToPDF_CreateFile] FilesDir: " + FilesDir);
            WriteLog("[ConvertHmlToPDF_CreateFile] fileName: " + fileName);
            WriteLog("[ConvertHmlToPDF_CreateFile] fullPath: " + fullPath);

            #region Create PDF
            try
            {
                Doc pages = new Doc();
                try
                {
                    Doc doc = new Doc();
                    //doc.Rect.Inset(30, 10);
                    //doc.HtmlOptions.Engine = EngineType.MSHtml;
                    //doc.HtmlOptions.Engine = EngineType.Chrome;

                    doc.HtmlOptions.FontEmbed      = true;
                    doc.HtmlOptions.FontSubstitute = false;
                    doc.HtmlOptions.FontProtection = false;
                    doc.HtmlOptions.BrowserWidth   = 1200;

                    doc.HtmlOptions.PageCacheClear();
                    doc.HtmlOptions.UseScript = true;
                    //doc.HtmlOptions.OnLoadScript = "(function(){window.ABCpdf_go = false; setTimeout(function(){window.ABCpdf_go = true;}, 3000);})();";

                    // Render after 3 seconds
                    //doc.HtmlOptions.OnLoadScript = " (function(){"
                    //  + " window.external.ABCpdf_RenderWait();"
                    //  + " setTimeout(function(){ "
                    //  + " window.external.ABCpdf_RenderComplete(); }, 10000);"
                    //  + "})();";
                    //doc.SetInfo(0, "RenderDelay", "45000");
                    //doc.SetInfo(0, "OneStageRender", 0);

                    //Render after 3 seconds
                    //doc.HtmlOptions.OnLoadScript = "(function(){"
                    //  + " window.ABCpdf_go = false;"
                    //  + " setTimeout(function(){ window.ABCpdf_go = true; }, 10000);"
                    //  + "})();";

                    doc.Page = doc.AddPage();
                    int theID;

                    theID = doc.AddImageUrl(filePathHTML);

                    while (true)
                    {
                        doc.FrameRect(); // add a black border
                        if (!doc.Chainable(theID))
                        {
                            break;
                        }
                        doc.Page = doc.AddPage();
                        theID    = doc.AddImageToChain(theID);

                        System.Threading.Thread.Sleep(500);
                    }

                    //doc.Rect.String = "100 50 500 150";

                    //for (int i = 1; i <= doc.PageCount; i++)
                    //{
                    //    doc.PageNumber = i;
                    //    doc.AddText("Page " + i.ToString());
                    //    //doc.FrameRect();
                    //}

                    pages.Append(doc);
                }
                catch (Exception ex2)
                {
                    result = ex2.Message;
                    WriteLog("[ConvertHmlToPDF_CreateFile] - Exception: " + result);
                    return(null);
                }

                pages.Save(fullPath);
                pages.Clear();
                result = fullPath;

                WriteLog("[ConvertHmlToPDF_CreateFile]: ----------End----------");
                return(result);
            }
            catch (Exception ex)
            {
                result = ex.Message;
                WriteLog("[ConvertHmlToPDF_CreateFile] -- Exception: " + result);
                return(null);
            }
            #endregion
        }