public void createPdf(String dest)
        {
            FileStream fs       = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            Document   document = new Document(PageSize.LETTER);
            PdfWriter  writer   = PdfWriter.GetInstance(document, fs);

            document.Open();
            TOCEvent evento = new TOCEvent();

            writer.PageEvent = evento;
            for (int i = 0; i < 10; i++)
            {
                String title = "This is title " + i;
                Chunk  c     = new Chunk(title, new Font());
                c.SetGenericTag(title);
                document.Add(new Paragraph(c));
                for (int j = 0; j < 50; j++)
                {
                    document.Add(new Paragraph("Line " + j + " of title " + i + " page: " + writer.PageNumber));
                }
            }
            document.NewPage();
            document.Add(new Paragraph("Table of Contents", new Font()));
            Chunk            dottedLine = new Chunk(new DottedLineSeparator());
            List <PageIndex> entries    = evento.getTOC();
            MultiColumnText  columns    = new MultiColumnText();

            columns.AddRegularColumns(72, 72 * 7.5f, 24, 2);
            Paragraph p;

            for (int i = 0; i < 10; i++)
            {
                foreach (PageIndex pageIndex in entries)
                {
                    Chunk chunk = new Chunk(pageIndex.Text);
                    chunk.SetAction(PdfAction.GotoLocalPage(pageIndex.Name, false));
                    p = new Paragraph(chunk);
                    p.Add(dottedLine);
                    chunk = new Chunk(pageIndex.Page.ToString());
                    chunk.SetAction(PdfAction.GotoLocalPage(pageIndex.Name, false));
                    p.Add(chunk);
                    columns.AddElement(p);
                }
            }
            document.Add(columns);
            document.Close();
        }
Exemplo n.º 2
0
        public PdfTestClass(string pdfFileName)
        {
            var config = new PdfConfig(PdfConfig.Application.Scorer)
            {
                MarginTop    = 1.5f * PdfHelper.cm2pt,
                MarginBottom = 1.5f * PdfHelper.cm2pt,

                HeaderLeft  = "Sun table",
                HeaderRight = loc,
                FooterLeft  = string.Format("Printed on {0:yyyy/MM/dd HH:mm}", DateTime.Now),

                TitleFont    = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD),
                SubtitleFont = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD),
                BoldFont     = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD),
                NormalFont   = new Font(Font.FontFamily.HELVETICA, 6.5f, Font.NORMAL),
                HeaderFont   = new Font(Font.FontFamily.HELVETICA, 6.5f, Font.NORMAL),
                FooterFont   = new Font(Font.FontFamily.HELVETICA, 6.5f, Font.NORMAL)
            };
            var helper   = new PdfHelper(pdfFileName, config);
            var document = helper.Document;

            //title
            //document.Add(new Paragraph("Sun table", config.TitleFont)
            //{
            //    Alignment = Element.ALIGN_LEFT,
            //    SpacingAfter = 10
            //});

            //line separator
            //document.Add(new Paragraph(new Chunk(new LineSeparator())) { SpacingBefore = -10, SpacingAfter = 10 });

            //table
            var headers   = new string[] { "Date", "Dawn", "Dusk", "Outdoor activity time" };
            var relWidths = new float[] { 8, 2, 2, 3 };
            var table     = helper.NewTable(headers, relWidths);
            //var table = helper.NewTable(headers, relWidths, "Sun table"); //with table title

            //table body

            //compute cells
            var sun   = new Sun(lat, lng, DateTime.Now - DateTime.UtcNow);
            var today = DateTime.Now;
            //var from = today - new TimeSpan(10, 0, 0, 0);
            //var to = today + new TimeSpan(30, 0, 0, 0);

            var from = new DateTime(today.Year, 1, 1);
            var to   = new DateTime(today.Year, 12, 31);

            for (var date = from; date <= to; date += new TimeSpan(1, 0, 0, 0))
            {
                var dawn = sun.Sunrise(date, Sun.ZenithTypes.Custom);
                var dusk = sun.Sunset(date, Sun.ZenithTypes.Custom);
                var span = dusk - dawn;

                //strings
                var strDate = date.ToString("D", DateTimeFormatInfo.InvariantInfo);
                var strDawn = dawn.ToString("HH:mm");
                var strDusk = dusk.ToString("HH:mm");
                var strSpan = span.ToString("hh\\:mm");

                //place cells in table
                table.AddCell(helper.NewLCell(strDate));
                table.AddCell(helper.NewRCell(strDawn));
                table.AddCell(helper.NewRCell(strDusk));
                table.AddCell(helper.NewRCell(strSpan));

                //var cell = helper.NewCell("Cell with colspan 2");
                //cell.Colspan = 2;
                //table.AddCell(cell);
            }

            //Place table on document

            ////normal layout
            //document.Add(table);

            ////page break
            //document.NewPage();

            //multicolumn layout
            MultiColumnText columns = new MultiColumnText();

            columns.AddRegularColumns(
                PdfHelper.cm2pt,                           //L-margin in pt
                document.PageSize.Width - PdfHelper.cm2pt, //R-margin in pt
                0.35f * PdfHelper.cm2pt,                   //separation in pt
                4);                                        //# of cols
            columns.AddElement(table);
            document.Add(columns);

            document.Close();
        }
Exemplo n.º 3
0
        private void SavePdf_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var fileName = Helper.SaveFile(".pdf files (*.pdf)|*.pdf");

            if (!string.IsNullOrEmpty(fileName))
            {
                var config = new PdfConfig(PdfConfig.Application.Scorer)
                {
                    MarginTop    = 1.5f * PdfHelper.cm2pt,
                    MarginBottom = 1.5f * PdfHelper.cm2pt,

                    HeaderLeft = Competition,
                    FooterLeft = "Datum " + DatumName + ", zone " + UtmZone
                };
                var helper = new PdfHelper(fileName, config);

                //title
                helper.Document.Add(new Paragraph("OFFICIAL WAYPOINT LIST", config.TitleFont)
                {
                    Alignment    = Element.ALIGN_CENTER,
                    SpacingAfter = 10
                });

                //line separator
                //document.Add(new Paragraph(new Chunk(new LineSeparator())) { SpacingBefore = -10, SpacingAfter = 10 });

                //table
                var headers   = new string[] { "Name", "Coordinates", "Altitude" };
                var relWidths = new float[] { 1, 2, 1 };
                var table     = helper.NewTable(headers, relWidths);
                //var table = helper.NewTable(headers, relWidths, "Sun table"); //with table title

                //table body

                foreach (var wp in Waypoints.Select(p => p.ToPdfWaypoint(DatumName, UtmZone, AltitudeUnits.Meters)))
                {
                    //place cells in table
                    table.AddCell(helper.NewLCell(wp.Name));
                    table.AddCell(helper.NewCCell(wp.CompetitionCoords));
                    table.AddCell(helper.NewRCell(wp.Altitude));
                }

                //Place table on document

                ////normal layout
                //helper.Document.Add(table);

                ////page break
                //document.NewPage();

                //multicolumn layout
                MultiColumnText columns = new MultiColumnText();
                columns.AddRegularColumns(
                    PdfHelper.cm2pt,                                  //L-margin in pt
                    helper.Document.PageSize.Width - PdfHelper.cm2pt, //R-margin in pt
                    0.35f * PdfHelper.cm2pt,                          //separation in pt
                    int.Parse(Columns));                              //# of cols
                columns.AddElement(table);
                helper.Document.Add(columns);

                helper.Document.Close();

                helper.OpenPdf();
            }
        }