Example #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");
        }
        private Stream ConvertPdfToImage(string url, int marginLeft, int marginTop, int page)
        {
            Stream returnStream = null;

            byte[] buffer = new byte[4096];
            int    count  = 0;

            byte[] pdfBytes = null;
            XSettings.InstallRedistributionLicense(System.Web.Configuration.WebConfigurationManager.AppSettings[constLicenseKey]);


            using (MemoryStream memoryStream = new MemoryStream())
            {
                WebRequest  webRequest  = WebRequest.Create(url);
                WebResponse webResponse = webRequest.GetResponse();

                Stream stream = webResponse.GetResponseStream();
                do
                {
                    count = stream.Read(buffer, 0, buffer.Length);
                    memoryStream.Write(buffer, 0, count);
                } while (count != 0);

                pdfBytes = memoryStream.ToArray();
            }

            using (Doc theDoc = new Doc())
            {
                theDoc.HtmlOptions.UseScript        = true;
                theDoc.HtmlOptions.UseActiveX       = true; // Enalbe SVG
                theDoc.HtmlOptions.UseVideo         = true;
                theDoc.HtmlOptions.PageCacheEnabled = true; //Enable cache
                theDoc.HtmlOptions.Timeout          = 120000;
                theDoc.Rect.Inset(marginLeft, marginTop);
                theDoc.Read(pdfBytes);
                theDoc.PageNumber            = page;
                theDoc.Rendering.DotsPerInch = constDotPerInches;
                theDoc.Rendering.SaveQuality = constSaveQuality;
                theDoc.Flatten();

                //Each page of pdf will be returned as a byte array

                byte[] images = theDoc.Rendering.GetData("abc.png");
                returnStream = new MemoryStream(images);
                theDoc.Clear();
            }
            return(returnStream);
        }
        private Stream ConvertUrlToImage(string url, int marginLeft, int marginTop, int page)
        {
            //bool isLogged = Boolean.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings[constLog]);

            MemoryStream memoryStream = null;

            byte[] imageBytes = null;
            //return the coresponding image here for the first page
            XSettings.InstallRedistributionLicense(
                System.Web.Configuration.WebConfigurationManager.AppSettings[constLicenseKey]);

            //Create a new Doc
            using (Doc theDoc = new Doc())
            {
                theDoc.HtmlOptions.UseScript  = true; // Enable javascript at the startup time
                theDoc.HtmlOptions.UseActiveX = true; // Enable SVG
                theDoc.HtmlOptions.UseVideo   = true; // Enable Video
                theDoc.HtmlOptions.Timeout    = 120000;
                // Time out is 2 minutes for ABCPDF .NET Doc calls the url to get the html
                theDoc.HtmlOptions.PageCacheEnabled = true; // Enable ABCPDF .NET page cache
                theDoc.Rect.Inset(marginLeft, marginTop);   // Insert the margin left and margin top
                theDoc.Page = theDoc.AddPage();

                int theID = theDoc.AddImageUrl(url.ToString());
                // Now chain subsequent pages together. Stop when reach a page which wasn't truncated.
                while (true)
                {
                    if (!theDoc.Chainable(theID))
                    {
                        break;
                    }
                    theDoc.Page = theDoc.AddPage();
                    theDoc.Rendering.DotsPerInch = constDotPerInches; // set DPI = 150
                    theDoc.Rendering.SaveQuality = constSaveQuality;  // set Quality = 100
                    theID = theDoc.AddImageToChain(theID);
                }
                theDoc.PageNumber            = page;
                theDoc.Rendering.DotsPerInch = constDotPerInches;
                theDoc.Rendering.SaveQuality = constImageQuality;
                theDoc.Flatten();
                imageBytes = theDoc.Rendering.GetData("abc.png");
                theDoc.Clear();
            }
            memoryStream = new MemoryStream(imageBytes);

            return(memoryStream);
        }
        private int GetNumberOfPages(string url, string extension, int marginLeft, int marginTop)
        {
            this.margineLeft = marginLeft;
            this.margineTop  = marginTop;
            this.url         = url;
            int  PageCount = 0;
            bool isLogged  = Boolean.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings[constLog]);

            // This code line sets the license key to the Doc object
            XSettings.InstallRedistributionLicense(System.Web.Configuration.WebConfigurationManager.AppSettings[constLicenseKey]);

            //Create a new Doc

            switch (extension)
            {
            //If the url is a pdf file
            case PDF:
                byte[] buffer   = new byte[4096];
                int    count    = 0;
                byte[] pdfBytes = null;

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    WebRequest  webRequest  = WebRequest.Create(url);
                    WebResponse webResponse = webRequest.GetResponse();
                    Stream      stream      = webResponse.GetResponseStream();
                    do
                    {
                        count = stream.Read(buffer, 0, buffer.Length);
                        memoryStream.Write(buffer, 0, count);
                    } while (count != 0);

                    pdfBytes = memoryStream.ToArray();
                }
                using (Doc theDoc = new Doc())
                {
                    theDoc.HtmlOptions.UseScript        = true;
                    theDoc.HtmlOptions.UseActiveX       = true;
                    theDoc.HtmlOptions.UseVideo         = true;
                    theDoc.HtmlOptions.PageCacheEnabled = true;
                    theDoc.HtmlOptions.Timeout          = 120000;
                    theDoc.Rect.Inset(marginLeft, marginTop);
                    theDoc.Read(pdfBytes);
                    PageCount = theDoc.PageCount;
                }
                return(PageCount);

            //break;
            default:
                using (Doc htmlDoc = new Doc())
                {
                    //Start new thread to generate images when user call GetNumberOfPages method
                    htmlDoc.HtmlOptions.UseScript  = true;   // Enable javascript at the startup time
                    htmlDoc.HtmlOptions.UseActiveX = true;   // Enable SVG
                    htmlDoc.HtmlOptions.UseVideo   = true;   // Enable Video
                    htmlDoc.HtmlOptions.Timeout    = 120000;
                    // Time out is 2 minutes for ABCPDF .NET Doc calls the url to get the html
                    htmlDoc.HtmlOptions.PageCacheEnabled = true;   // Enable ABCPDF .NET page cache
                    htmlDoc.Rect.Inset(marginLeft, marginTop);     // Insert the margin left and margin top
                    htmlDoc.Page = htmlDoc.AddPage();

                    int theID = htmlDoc.AddImageUrl(url.ToString());
                    // Now chain subsequent pages together. Stop when reach a page which wasn't truncated.
                    while (true)
                    {
                        if (!htmlDoc.Chainable(theID))
                        {
                            break;
                        }
                        htmlDoc.Page = htmlDoc.AddPage();
                        htmlDoc.Rendering.DotsPerInch = constDotPerInches;    // set DPI = 150
                        htmlDoc.Rendering.SaveQuality = constSaveQuality;     // set Quality = 100
                        theID = htmlDoc.AddImageToChain(theID);
                    }
                    if (htmlDoc != null)
                    {
                        return(htmlDoc.PageCount);
                    }
                }
                break;
            }
            return(0);
        }
Example #5
0
        // Put license key to Azure setting.

        #region IPrintServiceV1 Members

        /// <summary>
        /// Convert the HTML code from the specified URL to a PDF document and send the
        /// document as an attachment to the browser
        /// </summary>
        /// <param name="sessionTokenId">SessionTokenId given by user to authorize</param>
        /// <param name="url">The URL of a page that will be rendered</param>
        /// <param name="marginLeft"></param>
        /// <param name="marginTop"></param>
        /// <returns>a byte array that rendered as a PDF, will be null if error</returns>
        Stream IPrintServiceV1.ConvertUrlToPdf(string sessionTokenId, string url, int marginLeft, int marginTop)
        {
            Stream returnStream = null;

            if (marginLeft < 10000 && marginTop < 10000)
            {
                try
                {
                    bool isQualified = IsQualifiedUrl(url.ToString());
                    if (isQualified)
                    {
                        // Create a Doc object
                        XSettings.InstallRedistributionLicense(licenseKey);

                        using (Doc theDoc = new Doc())
                        {
                            //theDoc.SetInfo(0, "RenderDelay", "1000");
                            theDoc.HtmlOptions.UseScript        = true;
                            theDoc.HtmlOptions.UseActiveX       = true;
                            theDoc.HtmlOptions.UseVideo         = true;
                            theDoc.HtmlOptions.PageCacheEnabled = true;
                            theDoc.HtmlOptions.Timeout          = 120000; // 120 seconds
                            theDoc.Rect.Inset(marginLeft, marginTop);     // add margin

                            // Add the first page of HTML. Save the returned ID as this will be used to add subsequent pages
                            theDoc.Page = theDoc.AddPage();

                            int theID = theDoc.AddImageUrl(url.ToString());

                            // Now chain subsequent pages together. Stop when reach a page which wasn't truncated.
                            while (true)
                            {
                                if (!theDoc.Chainable(theID))
                                {
                                    break;
                                }
                                theDoc.Page = theDoc.AddPage();
                                theDoc.Rendering.DotsPerInch = constDotPerInches; // DPI
                                theDoc.Rendering.SaveQuality = constSaveQuality;  // Quality
                                theID = theDoc.AddImageToChain(theID);
                            }

                            // After adding the pages we can flatten them. We can't do this until after the pages have been added
                            // because flattening will invalidate our previous ID and break the chain.
                            for (int i = 1; i <= theDoc.PageCount; i++)
                            {
                                theDoc.PageNumber = i;
                                theDoc.Flatten();
                            }
                            // Get pdf data from the Doc object
                            returnStream = new MemoryStream(theDoc.GetData());
                            //returnByte = theDoc.GetData();

                            theDoc.Clear();
                        }
                    }

                    //TO-DO: Add the HTTP Status Code 403 (Forbidden) when the url is not in supported list
                }
                catch (UriFormatException uriFormatException)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
                    LoggerHelperV1.Error(AssemblyName, AssemblyVersion, Environment.MachineName,
                                         uriFormatException.StackTrace,
                                         HttpUtility.UrlEncode(url.ToString()) + uriFormatException.Message +
                                         uriFormatException.StackTrace);
                    returnStream = null;
                }
                catch (WebException webException)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                    LoggerHelperV1.Error(AssemblyName, AssemblyVersion, Environment.MachineName, webException.StackTrace,
                                         HttpUtility.UrlEncode(url.ToString()) + webException.Message +
                                         webException.StackTrace);
                    returnStream = null;
                }
                catch (Exception ex)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
                    LoggerHelperV1.Error(AssemblyName, AssemblyVersion, Environment.MachineName, ex.StackTrace,
                                         HttpUtility.UrlEncode(url.ToString()) + ex.Message + ex.StackTrace);
                    returnStream = null;
                }
                if (WebOperationContext.Current != null)
                {
                    WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
                    HttpResponseHeader cacheHeader = HttpResponseHeader.CacheControl;
                    WebOperationContext.Current.OutgoingResponse.Headers.Add(cacheHeader,
                                                                             string.Format(CultureInfo.InvariantCulture,
                                                                                           "max-age={0}, must-revalidate",
                                                                                           CachingDuration));
                    //Add one day caching
                }
            }
            return(returnStream);
        }