Exemplo n.º 1
0
        // ---------------------------------------------------------------------------

        /**
         * Creates a PDF document.
         */
        public byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // step 1
                using (Document document = new Document())
                {
                    // step 2
                    PdfWriter.GetInstance(document, ms);
                    // step 3
                    document.Open();
                    // step 4
                    // Add text with a local destination
                    Paragraph p   = new Paragraph();
                    Chunk     top = new Chunk("Country List", FilmFonts.BOLD);
                    top.SetLocalDestination("top");
                    p.Add(top);
                    document.Add(p);
                    // Add text with a link to an external URL
                    Chunk imdb = new Chunk("Internet Movie Database", FilmFonts.ITALIC);
                    imdb.SetAction(new PdfAction(new Uri("http://www.imdb.com/")));
                    p = new Paragraph(
                        @"Click on a country, and you'll get a list of movies, 
            containing links to the "
                        );
                    p.Add(imdb);
                    p.Add(".");
                    document.Add(p);
                    // Add text with a remote goto
                    p = new Paragraph("This list can be found in a ");
                    Chunk page1 = new Chunk("separate document");
                    page1.SetAction(new PdfAction(RESULT1, 1));

                    p.Add(page1);
                    p.Add(".");
                    document.Add(p);
                    document.Add(Chunk.NEWLINE);
                    // Get a list with countries from the database
                    var SQL =
                        @"SELECT DISTINCT mc.country_id, c.country, count(*) AS c 
FROM film_country c, film_movie_country mc 
WHERE c.id = mc.country_id
GROUP BY mc.country_id, country 
ORDER BY c DESC";
                    using (var c = AdoDB.Provider.CreateConnection())
                    {
                        c.ConnectionString = AdoDB.CS;
                        using (DbCommand cmd = c.CreateCommand())
                        {
                            cmd.CommandText = SQL;
                            c.Open();
                            using (var r = cmd.ExecuteReader())
                            {
                                while (r.Read())
                                {
                                    Paragraph country = new Paragraph(r["country"].ToString());
                                    country.Add(": ");
                                    Chunk link = new Chunk(string.Format(
                                                               "{0} movies", r["c"].ToString()
                                                               ));
                                    link.SetAction(PdfAction.GotoRemotePage(
                                                       RESULT1,
                                                       r["country_id"].ToString(),
                                                       false,
                                                       true
                                                       ));
                                    country.Add(link);
                                    document.Add(country);
                                }
                            }
                        }
                    }
                    document.Add(Chunk.NEWLINE);
                    // Add text with a local goto
                    p   = new Paragraph("Go to ");
                    top = new Chunk("top");
                    top.SetAction(PdfAction.GotoLocalPage("top", false));
                    p.Add(top);
                    p.Add(".");
                    document.Add(p);
                }
                return(ms.ToArray());
            }
        }