Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public PdfPCell RenderingCell(CellAttributes attributes)
        {
            var pdfCell = new PdfPCell();
            var table   = new PdfGrid(1)
            {
                RunDirection = PdfWriter.RUN_DIRECTION_LTR
            };

            var photo = PdfImageHelper.GetITextSharpImageFromImageFile(System.IO.Path.Combine(AppPath.ApplicationPath, "Images\\" + _rnd.Next(1, 5).ToString("00") + ".png"));

            table.AddCell(new PdfPCell(photo)
            {
                Border = 0, MinimumHeight = photo.Height, VerticalAlignment = Element.ALIGN_BOTTOM
            });

            var name = attributes.RowData.TableRowData.GetSafeStringValueOf <User>(x => x.Name);

            table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(name))
            {
                Border = 0
            });

            var lastName = attributes.RowData.TableRowData.GetSafeStringValueOf <User>(x => x.LastName);

            table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(lastName))
            {
                Border = 0
            });

            pdfCell.AddElement(table);

            return(pdfCell);
        }
Ejemplo n.º 2
0
        public PdfPCell RenderingCell(CellAttributes attributes)
        {
            var pdfCell = new PdfPCell();
            var table   = new PdfGrid(1)
            {
                RunDirection = PdfWriter.RUN_DIRECTION_LTR
            };

            var filePath = System.IO.Path.Combine(TestUtils.GetBaseDir(), "Images", $"{_rnd.Next(1, 5):00}.png");
            var photo    = PdfImageHelper.GetITextSharpImageFromImageFile(filePath);

            table.AddCell(new PdfPCell(photo, fit: true)
            {
                Border              = 0,
                VerticalAlignment   = Element.ALIGN_BOTTOM,
                HorizontalAlignment = Element.ALIGN_CENTER
            });

            var name = attributes.RowData.TableRowData.GetSafeStringValueOf("User");

            table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(name))
            {
                Border = 0,
                HorizontalAlignment = Element.ALIGN_CENTER
            });

            pdfCell.AddElement(table);

            return(pdfCell);
        }
        public void AddChartToPage(PlotModel model,
                                   Document pdfDoc,
                                   int width, int height,
                                   float spacingBefore   = 50,
                                   float spacingAfter    = 10,
                                   float widthPercentage = 80)
        {
            using (var chartimage = new MemoryStream())
            {
                //BMP gives the best compression result
                var pngExporter = new PngExporter {
                    Width = width, Height = height, Background = OxyColors.White
                };
                pngExporter.Export(model, chartimage);

                var imageBytes      = chartimage.ToArray();
                var iTextSharpImage = PdfImageHelper.GetITextSharpImageFromByteArray(imageBytes);
                iTextSharpImage.Alignment = Element.ALIGN_CENTER;

                var table = new PdfGrid(1)
                {
                    WidthPercentage = widthPercentage,
                    SpacingBefore   = spacingBefore,
                    SpacingAfter    = spacingAfter
                };
                table.AddCell(iTextSharpImage);

                pdfDoc.Add(table);
            }
        }
Ejemplo n.º 4
0
        public void AddChartToPage(Document pdfDoc,
                                   int scalePercent      = 100,
                                   float spacingBefore   = 50,
                                   float spacingAfter    = 10,
                                   float widthPercentage = 80)
        {
            using (var chartimage = new MemoryStream())
            {
                _chart.SaveImage(chartimage, ChartImageFormat.Bmp); //BMP gives the best compression result

                var iTextSharpImage = PdfImageHelper.GetITextSharpImageFromByteArray(chartimage.ToArray());
                iTextSharpImage.ScalePercent(scalePercent);
                iTextSharpImage.Alignment = Element.ALIGN_CENTER;

                var table = new PdfGrid(1)
                {
                    WidthPercentage = widthPercentage,
                    SpacingBefore   = spacingBefore,
                    SpacingAfter    = spacingAfter
                };
                table.AddCell(iTextSharpImage);

                pdfDoc.Add(table);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public PdfPCell RenderingCell(CellAttributes attributes)
        {
            var pdfCell = new PdfPCell();
            var table   = new PdfGrid(1)
            {
                RunDirection = PdfWriter.RUN_DIRECTION_LTR
            };

            var fileName = $"{_rnd.Next(1, 5):00}.png";
            var photo    = PdfImageHelper.GetITextSharpImageFromImageFile(TestUtils.GetImagePath(fileName));

            table.AddCell(new PdfPCell(photo)
            {
                Border = 0, MinimumHeight = photo.Height, VerticalAlignment = Element.ALIGN_BOTTOM
            });

            var name = attributes.RowData.TableRowData.GetSafeStringValueOf <User>(x => x.Name);

            table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(name))
            {
                Border = 0
            });

            var lastName = attributes.RowData.TableRowData.GetSafeStringValueOf <User>(x => x.LastName);

            table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(lastName))
            {
                Border = 0
            });

            pdfCell.AddElement(table);

            return(pdfCell);
        }
Ejemplo n.º 6
0
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public PdfPCell RenderingCell(CellAttributes attributes)
            {
                PdfPCell pdfCell = new PdfPCell();

                PdfGrid table = new PdfGrid(1)
                {
                    RunDirection = PdfWriter.RUN_DIRECTION_LTR
                };

                iTextSharp.text.Image photo = PdfImageHelper.GetITextSharpImageFromByteArray(SharksLogo);

                photo.WidthPercentage = 60;

                table.AddCell(new PdfPCell(photo, true)
                {
                    Border = 0
                });


                string coachName  = attributes.RowData.TableRowData[0].PropertyValue.ToSafeString();
                string coachPhone = attributes.RowData.TableRowData[1].PropertyValue.ToSafeString();

                List <string> players = attributes.RowData.TableRowData[2].PropertyValue as List <string>;

                foreach (string p in players)
                {
                    table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(p))
                    {
                        Border = 0, Padding = 0
                    });
                }

                table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(""))
                {
                    Border = 0
                });
                table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(""))
                {
                    Border = 0
                });

                if (!string.IsNullOrWhiteSpace(coachName))
                {
                    table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process("Head Coach"))
                    {
                        Border = 0
                    });

                    table.AddCell(new PdfPCell(attributes.BasicProperties.PdfFont.FontSelector.Process(coachName + " " + coachPhone))
                    {
                        Border = 1
                    });
                }

                pdfCell.AddElement(table);

                return(pdfCell);
            }
 private static void addImageCell(string picturePath, PdfGrid mainTable)
 {
     mainTable.AddCell(new PdfPCell(PdfImageHelper.GetITextSharpImageFromImageFile(picturePath))
     {
         Border = 0,
         HorizontalAlignment = Element.ALIGN_CENTER,
         VerticalAlignment   = Element.ALIGN_MIDDLE
     });
 }
Ejemplo n.º 8
0
        public PdfGrid RenderingReportHeader(Document pdfDoc, PdfWriter pdfWriter, IList <SummaryCellData> summaryData)
        {
            if (_image == null) //cache is empty
            {
                var templatePath = System.IO.Path.Combine(AppPath.ApplicationPath, "data\\PdfHeaderTemplate.pdf");
                _image = PdfImageHelper.GetITextSharpImageFromPdfTemplate(pdfWriter, templatePath);
            }

            var table = new PdfGrid(1);
            var cell  = new PdfPCell(_image, true)
            {
                Border = 0
            };

            table.AddCell(cell);
            return(table);
        }
        public PdfGrid RenderingReportHeader(Document pdfDoc, PdfWriter pdfWriter, IList <SummaryCellData> summaryData)
        {
            if (_image == null) //cache is empty
            {
                var templatePath = System.IO.Path.Combine(TestUtils.GetBaseDir(), "data", "PdfHeaderTemplate.pdf");
                _image = PdfImageHelper.GetITextSharpImageFromPdfTemplate(pdfWriter, templatePath);
            }

            var table = new PdfGrid(1);
            var cell  = new PdfPCell(_image, true)
            {
                Border = 0
            };

            table.AddCell(cell);
            return(table);

            //Note: return null if you want to skip this callback and render nothing. Also in this case, you need to set the header.CacheHeader(cache: false) too.
        }
Ejemplo n.º 10
0
        public IPdfReportData CreatePdfReport()
        {
            return(new PdfReport().DocumentPreferences(doc =>
            {
                doc.RunDirection(PdfRunDirection.LeftToRight);
                doc.Orientation(PageOrientation.Portrait);
                doc.PageSize(PdfPageSize.A4);
                doc.DocumentMetadata(new DocumentMetadata {
                    Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "Test Rpt", Title = "Test"
                });
                doc.Compression(new CompressionSettings
                {
                    EnableCompression = true,
                    EnableFullCompression = true
                });
            })
                   .DefaultFonts(fonts =>
            {
                fonts.Path(System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\tahoma.ttf"),
                           System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
                fonts.Size(9);
                fonts.Color(System.Drawing.Color.Black);
            })
                   .PagesFooter(footer =>
            {
                footer.DefaultFooter(printDate: DateTime.Now.ToString("MM/dd/yyyy"));
            })
                   .PagesHeader(header =>
            {
                header.CacheHeader(cache: true);  // It's a default setting to improve the performance.
                header.DefaultHeader(defaultHeader =>
                {
                    defaultHeader.RunDirection(PdfRunDirection.LeftToRight);
                    defaultHeader.ImagePath(System.IO.Path.Combine(AppPath.ApplicationPath, "Images\\01.png"));
                    defaultHeader.Message("Our new rpt.");
                });
            })
                   .MainTableTemplate(template =>
            {
                template.BasicTemplate(BasicTemplate.SilverTemplate);
            })
                   .MainTablePreferences(table =>
            {
                table.ColumnsWidthsType(TableColumnWidthType.Relative);
            })
                   .MainTableDataSource(dataSource =>
            {
                var listOfRows = new List <Question>();
                for (int i = 1; i <= 20; i++)
                {
                    listOfRows.Add(new Question
                    {
                        Id = i,
                        QuestionText = "A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. " + i,
                        Answer1 = "A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. " + i,
                        Answer2 = "A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. " + i,
                        Answer3 = "A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. " + i,
                        Answer4 = "A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. A very long item. " + i,
                        PicturePath = System.IO.Path.Combine(AppPath.ApplicationPath, "Images\\01.png")
                    });
                }
                dataSource.StronglyTypedList(listOfRows);
            })
                   .MainTableEvents(events =>
            {
                events.DataSourceIsEmpty(message: "There is no data available to display.");
            })
                   .MainTableColumns(columns =>
            {
                columns.AddColumn(column =>
                {
                    column.PropertyName <Question>(x => x.Id);
                    column.HeaderCell(caption: "Questions");
                    column.Width(1);
                    column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                    column.IsVisible(true);
                    column.Order(1);
                    column.ColumnItemsTemplate(template =>
                    {
                        template.PdfTemplate(
                            pdfTemplatePath: System.IO.Path.Combine(AppPath.ApplicationPath, "data\\questionTemplate.pdf"),
                            onFillAcroForm: (data, form, pdfStamper) =>
                        {
                            var id = data.GetSafeStringValueOf <Question>(x => x.Id);
                            form.SetField("txtNumber", id);

                            var questionText = data.GetSafeStringValueOf <Question>(x => x.QuestionText);
                            form.SetField("txtQuestion", questionText);

                            var answer1 = data.GetSafeStringValueOf <Question>(x => x.Answer1);
                            form.SetField("txtItem1", answer1);

                            var answer2 = data.GetSafeStringValueOf <Question>(x => x.Answer2);
                            form.SetField("txtItem2", answer2);

                            var answer3 = data.GetSafeStringValueOf <Question>(x => x.Answer3);
                            form.SetField("txtItem3", answer3);

                            var answer4 = data.GetSafeStringValueOf <Question>(x => x.Answer4);
                            form.SetField("txtItem4", answer4);

                            //Create a standard iTextSharp image and scale it to the form field's dimensions and place it where the field is
                            var picturePath = data.GetSafeStringValueOf <Question>(x => x.PicturePath);
                            var rect = form.GetFieldPositions("txtImage")[0].position;
                            var img = PdfImageHelper.GetITextSharpImageFromImageFile(picturePath);
                            //Scale it
                            img.ScaleAbsolute(rect.Width, rect.Height);
                            //Position it
                            img.SetAbsolutePosition(rect.Left, rect.Bottom);
                            //Add it to page 1 of the document
                            pdfStamper.GetOverContent(1).AddImage(img);
                        });
                    });
                });
            })
                   .Generate(data => data.AsPdfFile(string.Format("{0}\\Pdf\\QuestionsAcroRpt-{1}.pdf", AppPath.ApplicationPath, Guid.NewGuid().ToString("N"))), debugMode: true));
        }