A Generic Font Class
Inheritance: IPdfFont
Ejemplo n.º 1
0
        public string CreatePdfReport()
        {
            var fonts = new GenericFontProvider(
                                Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\tahoma.TTF"),
                                Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));

            var finalFile = Path.Combine(AppPath.ApplicationPath, "Pdf\\PieChartPdfReport.pdf");

            using (var document = new Document(PageSize.A4))
            {
                var writer = PdfWriter.GetInstance(document, new FileStream(finalFile, FileMode.Create));
                document.Open();
                var canvas = writer.DirectContent;

                var img = new PieChart
                          {
                              Direction = PdfRunDirection.RightToLeft,
                              ContentByte = canvas,
                              PdfFont = fonts,
                              Segments = new List<PieChartSegment>
                              {
                                      new PieChartSegment(100, new BaseColor(130, 197, 91), "عنوان يك"),
                                      new PieChartSegment(80, new BaseColor(95, 182, 85), "عنوان دو"),
                                      new PieChartSegment(60, new BaseColor(88, 89, 91), "عنوان سه"),
                                      new PieChartSegment(50, new BaseColor(67, 66, 61), "عنوان چهار"),
                                      new PieChartSegment(50, new BaseColor(173, 216, 230), "عنوان پنچ")
                              }
                          }.Draw();
                document.Add(img);

                var img2 = new PieChart
                           {
                               ContentByte = canvas,
                               PdfFont = fonts,
                               Segments = new List<PieChartSegment>
                                         {
                                            new PieChartSegment{ Value=5, Color = new BaseColor(130, 197, 91), Label = "Title 1" },
                                            new PieChartSegment{ Value=60, Color = new BaseColor(95, 182, 85), Label = "Title 2" },
                                            new PieChartSegment{ Value=35, Color = new BaseColor(88, 89, 91), Label = "Title 3" },
                                            new PieChartSegment{ Value=0, Color = new BaseColor(67, 66, 61), Label = "Title 4" }
                                         }
                           }.Draw();
                img2.SetAbsolutePosition(310, 620);
                img2.ScalePercent(150);
                document.Add(img2);
            }

            return finalFile;
        }
Ejemplo n.º 2
0
        public string CreatePdfReport()
        {
            var fonts = new GenericFontProvider(
                                Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\tahoma.TTF"),
                                Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));

            var finalFile = Path.Combine(AppPath.ApplicationPath, "Pdf\\BarChartPdfReport.pdf");

            using (var document = new Document(PageSize.A4))
            {
                var writer = PdfWriter.GetInstance(document, new FileStream(finalFile, FileMode.Create));
                document.Open();
                var canvas = writer.DirectContent;

                var items = new List<BarChartItem>
                      {
                          new BarChartItem(10, "Item 1 caption",  new BaseColor(130, 197, 91)),
                          new BarChartItem(100, "Item 2 caption", new BaseColor(95, 182, 85)),
                          new BarChartItem(60, "Item 3 caption",  new BaseColor(130, 197, 91)),
                          new BarChartItem(70, "Item 4 caption",  new BaseColor(88, 89, 91)),
                          new BarChartItem(120, "Item 5 caption", new BaseColor(173, 216, 230)),
                          new BarChartItem(0, "Item 6 caption",  BaseColor.YELLOW),
                          new BarChartItem(210, "Item 7 caption", BaseColor.MAGENTA),
                          new BarChartItem(150, "Item 8 caption", BaseColor.ORANGE),
                          new BarChartItem(50, "Item 9 caption", BaseColor.PINK),
                          new BarChartItem(20, "Item 10 caption", BaseColor.CYAN),
                          new BarChartItem(100, "Item 11 caption", BaseColor.BLUE),
                          new BarChartItem(90, "عنوان آيتم 12", BaseColor.GREEN),
                      };

                var img = new VerticalBarChart
                {
                    PdfFont = fonts,
                    ContentByte = canvas,
                    Items = items
                }.Draw();
                document.Add(img);

                var img2 = new HorizontalBarChart
                {
                    PdfFont = fonts,
                    ContentByte = canvas,
                    Items = items
                }.Draw();
                document.Add(img2);
            }

            return finalFile;
        }
Ejemplo n.º 3
0
        public IPdfReportData CreatePdfReport()
        {
            var fontFamilyName = "Wingdings";
            var fontProvider = new GenericFontProvider(
                                System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\WINGDING.TTF"),
                                System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"))
                                {
                                    Size = 17,
                                    Color = new BaseColor(System.Drawing.Color.DarkRed.ToArgb())
                                };

            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\\arial.ttf"),
                                  System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
                fonts.Size(9);
                fonts.Color(System.Drawing.Color.Black);
            })
            .PagesFooter(footer =>
            {
                footer.DefaultFooter(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.Message("Characters Map");
                });
            })
            .MainTableTemplate(template =>
            {
                template.BasicTemplate(BasicTemplate.SilverTemplate);
            })
            .MainTablePreferences(table =>
            {
                table.ColumnsWidthsType(TableColumnWidthType.Relative);
                table.MainTableType(TableType.HorizontalStackPanel);
                table.HorizontalStackPanelPreferences(columnsPerRow: 10);
            })
            .MainTableDataSource(dataSource =>
            {
                // FontFamily is defined in System.Windows.Media of PresentationCore.dll assembly.
                var fontFamily = new FontFamily(fontFamilyName);

                var listOfRows = new List<CharacterInfo>();
                GlyphTypeface glyph = null;
                foreach (var typeface in fontFamily.GetTypefaces())
                {
                    if (typeface.TryGetGlyphTypeface(out glyph) && (glyph != null))
                        break;
                }

                if (glyph == null)
                    throw new InvalidOperationException("Couldn't find a GlyphTypeface.");

                foreach (var item in glyph.CharacterToGlyphMap)
                {
                    var index = item.Key;
                    listOfRows.Add(new CharacterInfo
                    {
                        Character = Convert.ToChar(index),
                        CharacterCode = string.Format("0x{0:X}", index)
                    });
                }

                dataSource.StronglyTypedList(listOfRows);
            })
            .MainTableColumns(columns =>
            {
                columns.AddColumn(column =>
                {
                    column.PropertyName("rowNo");
                    column.IsRowNumber(true);
                    column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                    column.IsVisible(true);
                    column.Order(0);
                    column.Width(1);
                    column.HeaderCell("#");
                });

                columns.AddColumn(column =>
                {
                    column.PropertyName<CharacterInfo>(x => x.Character);
                    column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                    column.IsVisible(true);
                    column.Order(1);
                    column.Width(1);
                    column.HeaderCell(fontFamilyName + " Characters", mergeHeaderCell: true);
                    column.ColumnItemsTemplate(itemsTemplate =>
                        {
                            itemsTemplate.CustomTemplate(new CellTemplate(fontProvider));
                        });
                });
            })
            .MainTableEvents(events =>
            {
                events.DataSourceIsEmpty(message: "There is no data available to display.");
            })
            .Generate(data => data.AsPdfFile(string.Format("{0}\\Pdf\\CharacterMapSample-{1}.pdf", AppPath.ApplicationPath, Guid.NewGuid().ToString("N"))));
        }
Ejemplo n.º 4
0
 private IPdfFont getWatermarkFont()
 {
     var watermarkFont = new GenericFontProvider(
                                 System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\arial.ttf"),
                                 System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
     watermarkFont.Color = new GrayColor(0.75f);
     watermarkFont.Size = 50;
     return watermarkFont;
 }