// --------------------------------------------------------------------------- /** * Creates a PDF document. * @param stationary byte array of the new PDF document */ public byte[] CreatePdf(byte[] stationary) { using (MemoryStream ms = new MemoryStream()) { // step 1 using (Document document = new Document(PageSize.A4, 36, 36, 72, 36)) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, ms); //writer.CloseStream = false; UseStationary(writer, stationary); // step 3 document.Open(); // step 4 string SQL = "SELECT country, id FROM film_country ORDER BY country"; 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()) { document.Add(new Paragraph( r["country"].ToString(), FilmFonts.BOLD )); document.Add(Chunk.NEWLINE); string id = r["id"].ToString(); foreach (Movie movie in PojoFactory.GetMovies(id, true)) { document.Add(new Paragraph( movie.MovieTitle, FilmFonts.BOLD )); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { document.Add(new Paragraph( movie.OriginalTitle, FilmFonts.ITALIC )); } document.Add(new Paragraph( string.Format( "Year: {0}; run length: {0} minutes", movie.Year, movie.Duration ), FilmFonts.NORMAL )); document.Add(PojoToElementFactory.GetDirectorList(movie)); } document.NewPage(); } } } } } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Creates a Phrase containing information about a movie. * @param movie the movie for which you want to create a Paragraph */ public Phrase CreateMovieInformation(Movie movie) { Phrase p = new Phrase(); p.Font = FilmFonts.NORMAL; p.Add(new Phrase("Title: ", FilmFonts.BOLDITALIC)); p.Add(PojoToElementFactory.GetMovieTitlePhrase(movie)); p.Add(" "); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { p.Add(new Phrase("Original title: ", FilmFonts.BOLDITALIC)); p.Add(PojoToElementFactory.GetOriginalTitlePhrase(movie)); p.Add(" "); } p.Add(new Phrase("Country: ", FilmFonts.BOLDITALIC)); foreach (Country country in movie.Countries) { p.Add(PojoToElementFactory.GetCountryPhrase(country)); p.Add(" "); } p.Add(new Phrase("Director: ", FilmFonts.BOLDITALIC)); foreach (Director director in movie.Directors) { p.Add(PojoToElementFactory.GetDirectorPhrase(director)); p.Add(" "); } p.Add(new Chunk("Year: ", FilmFonts.BOLDITALIC)); p.Add(new Chunk(movie.Year.ToString(), FilmFonts.NORMAL)); p.Add(new Chunk(" Duration: ", FilmFonts.BOLDITALIC)); p.Add(new Chunk(movie.Duration.ToString(), FilmFonts.NORMAL)); p.Add(new Chunk(" minutes", FilmFonts.NORMAL)); p.Add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2)); return(p); }
// --------------------------------------------------------------------------- /** * Creates a table with screenings. * @param day a film festival day * @return a table with screenings */ public PdfPTable GetTable(string day) { // Create a table with 7 columns PdfPTable table = new PdfPTable(new float[] { 2, 1, 2, 5, 1, 3, 2 }); table.WidthPercentage = 100f; table.DefaultCell.UseAscender = true; table.DefaultCell.UseDescender = true; // Add the first header row Font f = new Font(); f.Color = BaseColor.WHITE; PdfPCell cell = new PdfPCell(new Phrase(day, f)); cell.BackgroundColor = BaseColor.BLACK; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.Colspan = 7; table.AddCell(cell); // Add the second header row twice table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY; for (int i = 0; i < 2; i++) { table.AddCell("Location"); table.AddCell("Time"); table.AddCell("Run Length"); table.AddCell("Title"); table.AddCell("Year"); table.AddCell("Directors"); table.AddCell("Countries"); } table.DefaultCell.BackgroundColor = null; // There are three special rows table.HeaderRows = 3; // One of them is a footer table.FooterRows = 1; // Now let's loop over the screenings List <Screening> screenings = PojoFactory.GetScreenings(day); Movie movie; foreach (Screening screening in screenings) { movie = screening.movie; table.AddCell(screening.Location); table.AddCell(screening.Time.Substring(0, 5)); table.AddCell(movie.Duration.ToString() + " '"); table.AddCell(movie.MovieTitle); table.AddCell(movie.Year.ToString()); cell = new PdfPCell(); cell.UseAscender = true; cell.UseDescender = true; cell.AddElement(PojoToElementFactory.GetDirectorList(movie)); table.AddCell(cell); cell = new PdfPCell(); cell.UseAscender = true; cell.UseDescender = true; cell.AddElement(PojoToElementFactory.GetCountryList(movie)); table.AddCell(cell); } return(table); }
// --------------------------------------------------------------------------- /** * Creates a Paragraph containing information about a movie. * @param movie the movie for which you want to create a Paragraph */ public Paragraph CreateMovieInformation(Movie movie) { Paragraph p = new Paragraph(); p.Font = FilmFonts.NORMAL; p.Add(new Phrase("Title: ", FilmFonts.BOLDITALIC)); p.Add(PojoToElementFactory.GetMovieTitlePhrase(movie)); p.Add(" "); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { p.Add(new Phrase("Original title: ", FilmFonts.BOLDITALIC)); p.Add(PojoToElementFactory.GetOriginalTitlePhrase(movie)); p.Add(" "); } p.Add(new Phrase("Country: ", FilmFonts.BOLDITALIC)); foreach (Country country in movie.Countries) { p.Add(PojoToElementFactory.GetCountryPhrase(country)); p.Add(" "); } p.Add(new Phrase("Director: ", FilmFonts.BOLDITALIC)); foreach (Director director in movie.Directors) { p.Add(PojoToElementFactory.GetDirectorPhrase(director)); p.Add(" "); } p.Add(CreateYearAndDuration(movie)); return(p); }
// --------------------------------------------------------------------------- /** * Creates a PDF document. * @param stream Stream for the new PDF document */ public void CreatePdf(Stream stream) { string RESOURCE = Utility.ResourcePosters; // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 Phrase phrase; Chunk chunk; PdfAnnotation annotation; foreach (Movie movie in PojoFactory.GetMovies()) { phrase = new Phrase(movie.MovieTitle); chunk = new Chunk("\u00a0\u00a0"); annotation = PdfAnnotation.CreateFileAttachment( writer, null, movie.MovieTitle, null, Path.Combine(RESOURCE, movie.Imdb + ".jpg"), string.Format("img_{0}.jpg", movie.Imdb) ); annotation.Put(PdfName.NAME, new PdfString("Paperclip")); chunk.SetAnnotation(annotation); phrase.Add(chunk); document.Add(phrase); document.Add(PojoToElementFactory.GetDirectorList(movie)); document.Add(PojoToElementFactory.GetCountryList(movie)); } } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 int epoch = -1; int currentYear = 0; Paragraph title = null; Chapter chapter = null; Section section = null; Section subsection = null; IEnumerable <Movie> movies = PojoFactory.GetMovies(true); // loop over the movies foreach (Movie movie in movies) { int yr = movie.Year; // add the chapter if we're in a new epoch if (epoch < (yr - 1940) / 10) { epoch = (yr - 1940) / 10; if (chapter != null) { document.Add(chapter); } title = new Paragraph(EPOCH[epoch], FONT[0]); chapter = new Chapter(title, epoch + 1); } // switch to a new year if (currentYear < yr) { currentYear = yr; title = new Paragraph( string.Format("The year {0}", yr), FONT[1] ); section = chapter.AddSection(title); section.BookmarkTitle = yr.ToString(); section.Indentation = 30; section.BookmarkOpen = false; section.NumberStyle = Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT; section.Add(new Paragraph( string.Format("Movies from the year {0}:", yr) )); } title = new Paragraph(movie.Title, FONT[2]); subsection = section.AddSection(title); subsection.IndentationLeft = 20; subsection.NumberDepth = 1; subsection.Add(new Paragraph("Duration: " + movie.Duration.ToString(), FONT[3])); subsection.Add(new Paragraph("Director(s):", FONT[3])); subsection.Add(PojoToElementFactory.GetDirectorList(movie)); subsection.Add(new Paragraph("Countries:", FONT[3])); subsection.Add(PojoToElementFactory.GetCountryList(movie)); } document.Add(chapter); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 Phrase phrase; Chunk chunk; foreach (Movie movie in PojoFactory.GetMovies()) { phrase = new Phrase(movie.MovieTitle); chunk = new Chunk("\u00a0"); chunk.SetAnnotation(PdfAnnotation.CreateText( writer, null, movie.MovieTitle, string.Format(INFO, movie.Year, movie.Duration), false, "Comment" )); phrase.Add(chunk); document.Add(phrase); document.Add(PojoToElementFactory.GetDirectorList(movie)); document.Add(PojoToElementFactory.GetCountryList(movie)); } } }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 document.Add(new Paragraph("Movies:")); IEnumerable <Movie> movies = PojoFactory.GetMovies(); foreach (Movie movie in movies) { PdfPTable table = new PdfPTable(2); table.SetWidths(new int[] { 1, 4 }); PdfPCell cell; cell = new PdfPCell(new Phrase(movie.Title, FilmFonts.BOLD)); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.Colspan = 2; table.AddCell(cell); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { cell = new PdfPCell(PojoToElementFactory.GetOriginalTitlePhrase(movie)); cell.Colspan = 2; cell.HorizontalAlignment = Element.ALIGN_RIGHT; table.AddCell(cell); } List <Director> directors = movie.Directors; cell = new PdfPCell(new Phrase("Directors:")); cell.Rowspan = directors.Count; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); int count = 0; foreach (Director pojo in directors) { cell = new PdfPCell(PojoToElementFactory.GetDirectorPhrase(pojo)); cell.Indent = (10 * count++); table.AddCell(cell); } table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT; table.AddCell("Year:"); table.AddCell(movie.Year.ToString()); table.AddCell("Run length:"); table.AddCell(movie.Duration.ToString()); List <Country> countries = movie.Countries; cell = new PdfPCell(new Phrase("Countries:")); cell.Rowspan = countries.Count; cell.VerticalAlignment = Element.ALIGN_BOTTOM; table.AddCell(cell); table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; foreach (Country country in countries) { table.AddCell(country.Name); } document.Add(table); } } }
public virtual void Write(Stream stream) { // step 1 using (Document document = new Document(PageSize.A4, 36, 36, 54, 36)) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); TableHeader tevent = new TableHeader(); writer.PageEvent = tevent; // step 3 document.Open(); // step 4 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()) { tevent.Header = r["country"].ToString(); // 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( r["id"].ToString() ) orderby m.Year, m.Title select m ; foreach (Movie movie in by_year) { document.Add(new Paragraph( movie.MovieTitle, FilmFonts.BOLD )); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { document.Add( new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC) ); } document.Add(new Paragraph( String.Format("Year: {0}; run length: {1} minutes", movie.Year, movie.Duration ), FilmFonts.NORMAL )); document.Add(PojoToElementFactory.GetDirectorList(movie)); } document.NewPage(); } } } } } }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, ms); // step 3 document.Open(); // step 4 PdfOutline root = writer.RootOutline; PdfOutline movieBookmark; PdfOutline link; String title; IEnumerable <Movie> movies = PojoFactory.GetMovies(); foreach (Movie movie in movies) { title = movie.MovieTitle; if ("3-Iron".Equals(title)) { title = "\ube48\uc9d1"; } movieBookmark = new PdfOutline(root, new PdfDestination( PdfDestination.FITH, writer.GetVerticalPosition(true) ), title, true ); movieBookmark.Style = Font.BOLD; link = new PdfOutline(movieBookmark, new PdfAction(String.Format(RESOURCE, movie.Imdb)), "link to IMDB" ); link.Color = BaseColor.BLUE; new PdfOutline(movieBookmark, PdfAction.JavaScript( String.Format(INFO, movie.Year, movie.Duration), writer ), "instant info" ); document.Add(new Paragraph(movie.MovieTitle)); document.Add(PojoToElementFactory.GetDirectorList(movie)); document.Add(PojoToElementFactory.GetCountryList(movie)); } } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- public override void Write(Stream stream) { // step 1 using (Document document = new Document(PageSize.A4, 36, 36, 54, 36)) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); TableHeader tevent = new TableHeader(); writer.PageEvent = tevent; writer.PageEvent = new Watermark(); // step 3 document.Open(); // step 4 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()) { tevent.Header = r["country"].ToString(); foreach (Movie movie in PojoFactory.GetMovies(r["id"].ToString(), true)) { document.Add(new Paragraph( movie.MovieTitle, FilmFonts.BOLD )); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { document.Add( new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC) ); } document.Add(new Paragraph( String.Format("Year: {0}; run length: {1} minutes", movie.Year, movie.Duration ), FilmFonts.NORMAL )); document.Add(PojoToElementFactory.GetDirectorList(movie)); } document.NewPage(); } } } } } }
// --------------------------------------------------------------------------- /** * Add content to a ColumnText object. * @param ct the ColumnText object * @param movie a Movie object * @param img the poster of the image */ public void AddContent(ColumnText ct, Movie movie, Image img) { ct.AddElement(img); ct.AddElement(new Paragraph(movie.Title, FilmFonts.BOLD)); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { ct.AddElement(new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC)); } ct.AddElement(PojoToElementFactory.GetDirectorList(movie)); ct.AddElement(PojoToElementFactory.GetYearPhrase(movie)); ct.AddElement(PojoToElementFactory.GetDurationPhrase(movie)); ct.AddElement(PojoToElementFactory.GetCountryList(movie)); ct.AddElement(Chunk.NEWLINE); }
// --------------------------------------------------------------------------- /** * Create a table with information about a movie. * @param screening a Screening * @return a table */ private PdfPTable GetTable(Screening screening) { // Create a table with 4 columns PdfPTable table = new PdfPTable(4); table.SetWidths(new int[] { 1, 5, 10, 10 }); // Get the movie Movie movie = screening.movie; // A cell with the title as a nested table spanning the complete row PdfPCell cell = new PdfPCell(); // nesting is done with addElement() in this example cell.AddElement(FullTitle(screening)); cell.Colspan = 4; cell.Border = PdfPCell.NO_BORDER; BaseColor color = WebColors.GetRGBColor( "#" + movie.entry.category.color ); cell.BackgroundColor = color; table.AddCell(cell); // empty cell cell = new PdfPCell(); cell.Border = PdfPCell.NO_BORDER; cell.UseAscender = true; cell.UseDescender = true; table.AddCell(cell); // cell with the movie poster cell = new PdfPCell(GetImage(movie.Imdb)); cell.Border = PdfPCell.NO_BORDER; table.AddCell(cell); // cell with the list of directors cell = new PdfPCell(); cell.AddElement(PojoToElementFactory.GetDirectorList(movie)); cell.Border = PdfPCell.NO_BORDER; cell.UseAscender = true; cell.UseDescender = true; table.AddCell(cell); // cell with the list of countries cell = new PdfPCell(); cell.AddElement(PojoToElementFactory.GetCountryList(movie)); cell.Border = PdfPCell.NO_BORDER; cell.UseAscender = true; cell.UseDescender = true; table.AddCell(cell); return(table); }
// =========================================================================== public void Write(Stream stream) { string RESOURCE = Utility.ResourcePosters; // step 1 using (Document document = new Document()) { // step 2 var writer = PdfWriter.GetInstance(document, stream); writer.StrictImageSequence = true; writer.InitialLeading = 18; // step 3 document.Open(); Rectangle rect = new Rectangle(0, 806, 36, 842); rect.BackgroundColor = BaseColor.RED; document.Add(rect); // step 4 IEnumerable <Movie> movies = PojoFactory.GetMovies(); foreach (Movie movie in movies) { // Create an image Image img = Image.GetInstance( Path.Combine(RESOURCE, movie.Imdb + ".jpg") ); img.Alignment = Image.ALIGN_LEFT | Image.TEXTWRAP; img.Border = Image.BOX; img.BorderWidth = 10; img.BorderColor = BaseColor.WHITE; img.ScaleToFit(1000, 72); document.Add(img); // Create text elements document.Add(new Paragraph(movie.MovieTitle, FilmFonts.BOLD)); document.Add(PojoToElementFactory.GetCountryList(movie)); document.Add(new Paragraph( String.Format("Year: {0}", movie.Year) )); document.Add(new Paragraph( String.Format("Duration: {0} minutes", movie.Duration)) ); document.Add(new Paragraph("Directors:")); document.Add(PojoToElementFactory.GetDirectorList(movie)); document.Add(Chunk.NEWLINE); } } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 foreach (Movie movie in PojoFactory.GetMovies()) { document.Add(new Phrase(movie.MovieTitle)); document.Add(new Annotation( movie.Title, string.Format(INFO, movie.Year, movie.Duration) )); document.Add(PojoToElementFactory.GetDirectorList(movie)); document.Add(PojoToElementFactory.GetCountryList(movie)); } } }
// --------------------------------------------------------------------------- /** * Add content to a ColumnText object. * @param ct the ColumnText object * @param movie a Movie object * @param img the poster of the image */ public void AddContent(ColumnText ct, Movie movie) { Paragraph p; p = new Paragraph(new Paragraph(movie.Title, FilmFonts.BOLD)); p.Alignment = Element.ALIGN_CENTER; p.SpacingBefore = 16; ct.AddElement(p); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { p = new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC); p.Alignment = Element.ALIGN_RIGHT; ct.AddElement(p); } p = new Paragraph(); p.Add(PojoToElementFactory.GetYearPhrase(movie)); p.Add(" "); p.Add(PojoToElementFactory.GetDurationPhrase(movie)); p.Alignment = Element.ALIGN_JUSTIFIED_ALL; ct.AddElement(p); p = new Paragraph(new Chunk(new StarSeparator())); p.SpacingAfter = 12; ct.AddElement(p); }
// --------------------------------------------------------------------------- /** * 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 int epoch = -1; int currentYear = 0; Paragraph title = null; iTextSharp.text.Chapter chapter = null; Section section = null; bool sortByYear = true; foreach (Movie movie in PojoFactory.GetMovies(sortByYear)) { // add the chapter if we're in a new epoch if (epoch < (movie.Year - 1940) / 10) { epoch = (movie.Year - 1940) / 10; if (chapter != null) { document.Add(chapter); } title = new Paragraph(EPOCH[epoch], FONT[0]); chapter = new iTextSharp.text.Chapter(title, epoch + 1); chapter.BookmarkTitle = EPOCH[epoch]; } // switch to a new year if (currentYear < movie.Year) { currentYear = movie.Year; title = new Paragraph( String.Format("The year {0}", movie.Year), FONT[1] ); section = chapter.AddSection(title); section.BookmarkTitle = movie.Year.ToString(); section.Indentation = 30; section.BookmarkOpen = false; section.NumberStyle = Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT; section.Add(new Paragraph( String.Format("Movies from the year {0}:", movie.Year) )); } title = new Paragraph(movie.MovieTitle, FONT[2]); section.Add(title); section.Add(new Paragraph( "Duration: " + movie.Duration.ToString(), FONT[3] )); section.Add(new Paragraph("Director(s):", FONT[3])); section.Add(PojoToElementFactory.GetDirectorList(movie)); section.Add(new Paragraph("Countries:", FONT[3])); section.Add(PojoToElementFactory.GetCountryList(movie)); } document.Add(chapter); } return(ms.ToArray()); } }
// =========================================================================== public override void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 IEnumerable <Movie> movies = PojoFactory.GetMovies(); foreach (Movie movie in movies) { // Create a paragraph with the title Paragraph title = new Paragraph( PojoToElementFactory.GetMovieTitlePhrase(movie) ); title.Alignment = Element.ALIGN_LEFT; document.Add(title); // Add the original title next to it using a dirty hack if (!string.IsNullOrEmpty(movie.OriginalTitle)) { Paragraph dummy = new Paragraph("\u00a0", FilmFonts.NORMAL); dummy.Leading = -18; document.Add(dummy); Paragraph originalTitle = new Paragraph( PojoToElementFactory.GetOriginalTitlePhrase(movie) ); originalTitle.Alignment = Element.ALIGN_RIGHT; document.Add(originalTitle); } // Info about the director float indent = 20; // Loop over the directors foreach (Director pojo in movie.Directors) { Paragraph director = new Paragraph( PojoToElementFactory.GetDirectorPhrase(pojo) ); director.IndentationLeft = indent; document.Add(director); indent += 20; } // Info about the country indent = 20; // Loop over the countries foreach (Country pojo in movie.Countries) { Paragraph country = new Paragraph( PojoToElementFactory.GetCountryPhrase(pojo) ); country.Alignment = Element.ALIGN_RIGHT; country.IndentationRight = indent; document.Add(country); indent += 20; } // Extra info about the movie Paragraph info = CreateYearAndDuration(movie); info.Alignment = Element.ALIGN_CENTER; info.SpacingAfter = 36; document.Add(info); } } }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 document.Add(new Paragraph("Movies:")); IEnumerable <Movie> movies = PojoFactory.GetMovies(); List list; PdfPCell cell; string RESOURCE = Utility.ResourcePosters; foreach (Movie movie in movies) { // a table with two columns PdfPTable table = new PdfPTable(new float[] { 1, 7 }); table.WidthPercentage = 100; table.SpacingBefore = 5; // a cell with an image cell = new PdfPCell( Image.GetInstance(Path.Combine(RESOURCE, movie.Imdb + ".jpg")), true ); cell.Border = PdfPCell.NO_BORDER; table.AddCell(cell); cell = new PdfPCell(); // a cell with paragraphs and lists Paragraph p = new Paragraph(movie.Title, FilmFonts.BOLD); p.Alignment = Element.ALIGN_CENTER; p.SpacingBefore = 5; p.SpacingAfter = 5; cell.AddElement(p); cell.Border = PdfPCell.NO_BORDER; if (!string.IsNullOrEmpty(movie.OriginalTitle)) { p = new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC); p.Alignment = Element.ALIGN_RIGHT; cell.AddElement(p); } list = PojoToElementFactory.GetDirectorList(movie); list.IndentationLeft = 30; cell.AddElement(list); p = new Paragraph( string.Format("Year: {0}", movie.Year), FilmFonts.NORMAL ); p.IndentationLeft = 15; p.Leading = 24; cell.AddElement(p); p = new Paragraph( string.Format("Run length: {0}", movie.Duration), FilmFonts.NORMAL ); p.Leading = 14; p.IndentationLeft = 30; cell.AddElement(p); list = PojoToElementFactory.GetCountryList(movie); list.IndentationLeft = 40; cell.AddElement(list); table.AddCell(cell); // every movie corresponds with one table document.Add(table); // but the result looks like one big table } } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // IMPORTANT: set linear page mode! writer.SetLinearPageMode(); ChapterSectionTOC tevent = new ChapterSectionTOC(new MovieHistory1()); writer.PageEvent = tevent; // step 3 document.Open(); // step 4 int epoch = -1; int currentYear = 0; Paragraph title = null; iTextSharp.text.Chapter chapter = null; Section section = null; Section subsection = null; // add the chapters, sort by year foreach (Movie movie in PojoFactory.GetMovies(true)) { int year = movie.Year; if (epoch < (year - 1940) / 10) { epoch = (year - 1940) / 10; if (chapter != null) { document.Add(chapter); } title = new Paragraph(EPOCH[epoch], FONT[0]); chapter = new iTextSharp.text.Chapter(title, epoch + 1); } if (currentYear < year) { currentYear = year; title = new Paragraph( String.Format("The year {0}", year), FONT[1] ); section = chapter.AddSection(title); section.BookmarkTitle = year.ToString(); section.Indentation = 30; section.BookmarkOpen = false; section.NumberStyle = Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT; section.Add(new Paragraph( String.Format("Movies from the year {0}:", year)) ); } title = new Paragraph(movie.MovieTitle, FONT[2]); subsection = section.AddSection(title); subsection.IndentationLeft = 20; subsection.NumberDepth = 1; subsection.Add(new Paragraph( "Duration: " + movie.Duration.ToString(), FONT[3] )); subsection.Add(new Paragraph("Director(s):", FONT[3])); subsection.Add(PojoToElementFactory.GetDirectorList(movie)); subsection.Add(new Paragraph("Countries:", FONT[3])); subsection.Add(PojoToElementFactory.GetCountryList(movie)); } document.Add(chapter); // add the TOC starting on the next page document.NewPage(); int toc = writer.PageNumber; foreach (Paragraph p in tevent.titles) { document.Add(p); } // always go to a new page before reordering pages. document.NewPage(); // get the total number of pages that needs to be reordered int total = writer.ReorderPages(null); // change the order int[] order = new int[total]; for (int i = 0; i < total; i++) { order[i] = i + toc; if (order[i] > total) { order[i] -= total; } } // apply the new order writer.ReorderPages(order); } }
// --------------------------------------------------------------------------- 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); } } } } } }
// --------------------------------------------------------------------------- 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; 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()) { // create a paragraph for the director director = PojoFactory.GetDirector(r); Paragraph p = new Paragraph( PojoToElementFactory.GetDirectorPhrase(director)); // add a dotted line separator p.Add(new Chunk(new DottedLineSeparator())); // adds the number of movies of this director p.Add(string.Format("movies: {0}", Convert.ToInt32(r["c"]))); document.Add(p); // Creates a list List list = new List(List.ORDERED); list.IndentationLeft = 36; list.IndentationRight = 36; // Gets the movies of the current director var director_id = Convert.ToInt32(r["id"]); ListItem movieitem; // LINQ allows us to on sort 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) { // creates a list item with a movie title movieitem = new ListItem(movie.MovieTitle); // adds a vertical position mark as a separator movieitem.Add(new Chunk(new VerticalPositionMark())); var yr = movie.Year; // adds the year the movie was produced movieitem.Add(new Chunk(yr.ToString())); // add an arrow to the right if the movie dates from 2000 or later if (yr > 1999) { movieitem.Add(PositionedArrow.RIGHT); } // add the list item to the list list.Add(movieitem); } // add the list to the document document.Add(list); } } } } } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { string SQL = "SELECT country, id FROM film_country ORDER BY country"; using (ZipFile zip = new ZipFile()) { using (var ms = new MemoryStream()) { // FIRST PASS, CREATE THE PDF WITHOUT HEADER // step 1 using (Document document = new Document(PageSize.A4, 36, 36, 54, 36)) { // step 2 PdfWriter.GetInstance(document, ms); // step 3 document.Open(); // step 4 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()) { document.Add(new Paragraph( r["country"].ToString(), FilmFonts.BOLD )); document.Add(Chunk.NEWLINE); string id = r["id"].ToString(); foreach (Movie movie in PojoFactory.GetMovies(id, true)) { document.Add(new Paragraph( movie.MovieTitle, FilmFonts.BOLD )); if (!string.IsNullOrEmpty(movie.OriginalTitle)) { document.Add(new Paragraph( movie.OriginalTitle, FilmFonts.ITALIC )); } document.Add(new Paragraph(string.Format( "Year: {0}; run length: {0} minutes", movie.Year, movie.Duration ), FilmFonts.NORMAL )); document.Add(PojoToElementFactory.GetDirectorList(movie)); } document.NewPage(); } } } } } byte[] firstPass = ms.ToArray(); zip.AddEntry("first-pass.pdf", firstPass); // SECOND PASS, ADD THE HEADER // Create a reader PdfReader reader = new PdfReader(firstPass); using (MemoryStream ms2 = new MemoryStream()) { // Create a stamper using (PdfStamper stamper = new PdfStamper(reader, ms2)) { // Loop over the pages and add a header to each page int n = reader.NumberOfPages; for (int i = 1; i <= n; i++) { GetHeaderTable(i, n).WriteSelectedRows( 0, -1, 34, 803, stamper.GetOverContent(i) ); } } zip.AddEntry(RESULT, ms2.ToArray()); } } zip.Save(stream); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document(PageSize.A4, 36, 36, 54, 54)) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); HeaderFooter tevent = new HeaderFooter(); writer.SetBoxSize("art", new Rectangle(36, 54, 559, 788)); writer.PageEvent = tevent; // step 3 document.Open(); // step 4 int epoch = -1; int currentYear = 0; Paragraph title = null; iTextSharp.text.Chapter chapter = null; Section section = null; Section subsection = null; // add the chapters, sort by year foreach (Movie movie in PojoFactory.GetMovies(true)) { int year = movie.Year; if (epoch < (year - 1940) / 10) { epoch = (year - 1940) / 10; if (chapter != null) { document.Add(chapter); } title = new Paragraph(EPOCH[epoch], FONT[0]); chapter = new iTextSharp.text.Chapter(title, epoch + 1); } if (currentYear < year) { currentYear = year; title = new Paragraph( String.Format("The year {0}", year), FONT[1] ); section = chapter.AddSection(title); section.BookmarkTitle = year.ToString(); section.Indentation = 30; section.BookmarkOpen = false; section.NumberStyle = Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT; section.Add(new Paragraph( String.Format("Movies from the year {0}:", year)) ); } title = new Paragraph(movie.MovieTitle, FONT[2]); subsection = section.AddSection(title); subsection.IndentationLeft = 20; subsection.NumberDepth = 1; subsection.Add(new Paragraph( "Duration: " + movie.Duration.ToString(), FONT[3] )); subsection.Add(new Paragraph("Director(s):", FONT[3])); subsection.Add(PojoToElementFactory.GetDirectorList(movie)); subsection.Add(new Paragraph("Countries:", FONT[3])); subsection.Add(PojoToElementFactory.GetCountryList(movie)); } document.Add(chapter); } }
// --------------------------------------------------------------------------- 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; // creating separators LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2); Paragraph stars = new Paragraph(20); stars.Add(new Chunk(StarSeparator.LINE)); stars.SpacingAfter = 30; 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()) { // get the director object and use it in a Paragraph director = PojoFactory.GetDirector(r); Paragraph p = new Paragraph( PojoToElementFactory.GetDirectorPhrase(director)); // if there are more than 2 movies for this director // an arrow is added to the left var count = Convert.ToInt32(r["c"]); if (count > 2) { p.Add(PositionedArrow.LEFT); } p.Add(line); // add the paragraph with the arrow to the document document.Add(p); // Get the movies of the 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; foreach (Movie movie in by_year) { p = new Paragraph(movie.MovieTitle); p.Add(": "); p.Add(new Chunk(movie.Year.ToString())); if (movie.Year > 1999) { p.Add(PositionedArrow.RIGHT); } document.Add(p); } // add a star separator after the director info is added document.Add(stars); } } } } } }
/** * Creates a PDF with a table * @param writer the writer to our report file * @param filename the PDF that will be created * @throws IOException * @throws DocumentException * @throws SQLException */ private void CreatePdfWithPdfPTable(StreamWriter writer, Stream doc) { // Create a connection to the database //DatabaseConnection connection = new HsqldbConnection("filmfestival"); // step 1 Document document = new Document(); // step 2 PdfWriter.GetInstance(document, doc); // step 3 document.Open(); // step 4 // Create a table with 2 columns PdfPTable table = new PdfPTable(new float[] { 1, 7 }); // Mark the table as not complete if (test) { table.Complete = false; } table.WidthPercentage = 100; IEnumerable <Movie> movies = PojoFactory.GetMovies(); List list; PdfPCell cell; int count = 0; // add information about a movie foreach (Movie movie in movies) { table.SpacingBefore = 5; // add a movie poster cell = new PdfPCell(Image.GetInstance(String.Format(RESOURCE, movie.Imdb)), true); cell.Border = PdfPCell.NO_BORDER; table.AddCell(cell); // add movie information cell = new PdfPCell(); Paragraph p = new Paragraph(movie.Title, FilmFonts.BOLD); p.Alignment = Element.ALIGN_CENTER; p.SpacingBefore = 5; p.SpacingAfter = 5; cell.AddElement(p); cell.Border = PdfPCell.NO_BORDER; if (movie.OriginalTitle != null) { p = new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC); p.Alignment = Element.ALIGN_RIGHT; cell.AddElement(p); } list = PojoToElementFactory.GetDirectorList(movie); list.IndentationLeft = 30; cell.AddElement(list); p = new Paragraph(String.Format("Year: %d", movie.Year), FilmFonts.NORMAL); p.IndentationLeft = 15; p.Leading = 24; cell.AddElement(p); p = new Paragraph(String.Format("Run length: %d", movie.Duration), FilmFonts.NORMAL); p.Leading = 14; p.IndentationLeft = 30; cell.AddElement(p); list = PojoToElementFactory.GetCountryList(movie); list.IndentationLeft = 40; cell.AddElement(list); table.AddCell(cell); // insert a checkpoint every 10 movies if (count++ % 10 == 0) { // add the incomplete table to the document if (test) { document.Add(table); } Checkpoint(writer); } } // Mark the table as complete if (test) { table.Complete = true; } // add the table to the document document.Add(table); // insert a last checkpoint Checkpoint(writer); // step 5 document.Close(); }