コード例 #1
0
        // ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document())
            {
                // step 2
                PdfWriter.GetInstance(document, stream);
                // step 3
                document.Open();
                // step 4
                Director director;
                // creates line separators
                Chunk CONNECT = new Chunk(
                    new LineSeparator(0.5f, 95, BaseColor.BLUE, Element.ALIGN_CENTER, 3.5f)
                    );
                LineSeparator UNDERLINE = new LineSeparator(
                    1, 100, null, Element.ALIGN_CENTER, -2
                    );
                // creates tabs
                Chunk tab1 = new Chunk(new VerticalPositionMark(), 200, true);
                Chunk tab2 = new Chunk(new VerticalPositionMark(), 350, true);
                Chunk tab3 = new Chunk(new DottedLineSeparator(), 450, true);

                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())
                        {
                            // loops over the directors
                            while (r.Read())
                            {
                                // creates a paragraph with the director name
                                director = PojoFactory.GetDirector(r);
                                Paragraph p = new Paragraph(
                                    PojoToElementFactory.GetDirectorPhrase(director));
                                // adds a separator
                                p.Add(CONNECT);
                                // adds more info about the director
                                p.Add(string.Format("movies: {0}", Convert.ToInt32(r["c"])));
                                // adds a separator
                                p.Add(UNDERLINE);
                                // adds the paragraph to the document
                                document.Add(p);
                                // gets all the movies of the current director
                                var director_id = Convert.ToInt32(r["id"]);
                                // LINQ allows us to sort on any movie object property inline;
                                // let's sort by Movie.Year, Movie.Title
                                var by_year = from m in PojoFactory.GetMovies(director_id)
                                              orderby m.Year, m.Title
                                select m;
                                // loops over the movies
                                foreach (Movie movie in by_year)
                                {
                                    // create a Paragraph with the movie title
                                    p = new Paragraph(movie.MovieTitle);
                                    // insert a tab
                                    p.Add(new Chunk(tab1));
                                    // add the original title
                                    var mt = movie.OriginalTitle;
                                    if (mt != null)
                                    {
                                        p.Add(new Chunk(mt));
                                    }
                                    // insert a tab
                                    p.Add(new Chunk(tab2));
                                    // add the run length of the movie
                                    p.Add(new Chunk(string.Format("{0} minutes", movie.Duration)));
                                    // insert a tab
                                    p.Add(new Chunk(tab3));
                                    // add the production year of the movie
                                    p.Add(new Chunk(
                                              string.Format("{0}", movie.Year.ToString())
                                              ));
                                    // add the paragraph to the document
                                    document.Add(p);
                                }
                                document.Add(Chunk.NEWLINE);
                            }
                        }
                    }
                }
            }
        }