Пример #1
0
        public MemoryStream GeneratePDF()
        {
            var doc = new Document();
            var ms  = new MemoryStream();

            var writer = PdfWriter.GetInstance(doc, ms);

            doc.Open();

            PdfPTable table = new PdfPTable(1);
            PdfPCell  cell  = new PdfPCell();

            string htmlBeforeImage =
                "<h1 class=\"text-center\">Silicon Shores</h1>\n" +
                "<h3 class=\"col-md-4\">One " + ticket_types.ticket_name + " Ticket</h3>";

            string htmlAfterImage =
                "<p>Thank you for your ticket purchase! This ticket is redeemable any time at Silicon Shores Theme Park for a days admission</p>\n" +
                "<p>Requirements for redemption: " + ticket_types.ticket_restrictions + ".</p>\n" +
                File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Views/TicketPdf.cshtml"));

            var bootstrap = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/bootstrap.min.css"))
                            + File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/bootstrap-theme.min.css"));

            ElementList list = XMLWorkerHelper.ParseToElementList(htmlBeforeImage, bootstrap);

            foreach (var element in list)
            {
                cell.AddElement(element);
            }

            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(BarCodeImage(), BaseColor.WHITE);
            var scale = 115;

            img.ScalePercent(scale);
            cell.AddElement(img);

            list = XMLWorkerHelper.ParseToElementList(htmlAfterImage, bootstrap);
            foreach (var element in list)
            {
                cell.AddElement(element);
            }

            table.AddCell(cell);

            doc.Add(table);

            writer.CloseStream = false;
            doc.Close();
            ms.Position = 0;

            return(ms);
        }
Пример #2
0
        private void WriteCards(List <Card> cards, PdfPTable table)
        {
            foreach (var imageFile in cards)
            {
                if (File.Exists(imageFile.Path))
                {
                    System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageFile.Path);

                    if (imageFile.WhiteBorder)
                    {
                        image = WhiteBorderConverter.ToWhiteBorder(image);
                    }
                    else
                    {
                        image = WhiteBorderConverter.CreateNonIndexedImage(image);
                    }

                    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);

                    float scalePercent = 100.0f * 179.0f / (pic.Width);

                    //pic.ScalePercent(51.0f);
                    pic.ScalePercent(scalePercent);

                    PdfPCell cell = new PdfPCell(pic);
                    //cell.FixedHeight = pic.ScaledHeight;
                    cell.FollowingIndent = 0;
                    cell.Indent          = 0;
                    cell.Padding         = 0;
                    cell.SetLeading(0, 0);
                    cell.BorderColor = iTextSharp.text.BaseColor.WHITE;

                    table.AddCell(cell);
                }
            }
        }
Пример #3
0
        private void WriteCards(List <Card> cards, PdfPTable table, iTextSharp.text.Document document, KnownColor backColor)
        {
            foreach (var imageFile in cards)
            {
                if (File.Exists(imageFile.Path))
                {
                    System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(imageFile.Path);
                    System.Drawing.Bitmap image         = null;

                    if (imageFile.BorderColor != System.Drawing.KnownColor.Black)
                    {
                        image = WhiteBorderConverter.ToWhiteBorder(originalImage, imageFile.BorderColor);
                    }
                    else
                    {
                        image = WhiteBorderConverter.CreateNonIndexedImage(originalImage);
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    originalImage.Dispose();
                    originalImage = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (myCubeFeaturesCheckbox.IsChecked == true)
                    {
                        WhiteBorderConverter.FixCorners(ref image, imageFile.BorderColor);

                        // Do I need this for anything
                        //if (image.Width > image.Height)
                        //{
                        //    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        //}

                        if (ShowCardNumbers)
                        {
                            //image = AddCardNumber(ref image, imageFile);
                            AddCardNumber(ref image, imageFile);
                        }
                    }
                    //iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
                    //iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Gif);
                    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Bmp);


                    float scalePercent = 100.0f * 179.0f / (pic.Width);

                    // For cube, also only make sure that the heights are correct
                    if (true == this.myCubeFeaturesCheckbox.IsChecked)
                    {
                        scalePercent = 100.0f * 250.0f / (pic.Height);

                        /*
                         * float newHeight = pic.Height * scalePercent / 100.0f;
                         *
                         * // Limit height to 251 at the most
                         * if (newHeight > 251)
                         * {
                         *  scalePercent = 100.0f * 250.0f / (pic.Height);
                         * }
                         *
                         * int p = 0;*/
                    }

                    //pic.ScalePercent(51.0f);
                    pic.ScalePercent(scalePercent);

                    PdfPCell cell = new PdfPCell(pic);
                    //cell.FixedHeight = pic.ScaledHeight;
                    cell.FollowingIndent = 0;
                    cell.Indent          = 0;
                    cell.Padding         = 0;
                    cell.SetLeading(0, 0);
                    //cell.BorderColor = BaseColor.WHITE;
                    //cell.BorderColor = new iTextSharp.text.BaseColor(System.Drawing.Color.FromKnownColor(backColor));
                    //cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.FromKnownColor(backColor));
                    cell.BorderColor     = new iTextSharp.text.BaseColor(WhiteBorderConverter.GetCompatibleColor(backColor));
                    cell.BackgroundColor = new iTextSharp.text.BaseColor(WhiteBorderConverter.GetCompatibleColor(backColor));

                    table.AddCell(cell);

                    // Draw a rectangle at the right of the doc
                    //float vert = document.GetTop(0) - table.TotalHeight;
                    //iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(0, vert, 50, vert+30);
                    //rect.BackgroundColor = BaseColor.RED;
                    //document.Add(rect);

                    GC.Collect();
                }
            }
        }