// --------------------------------------------------------------------------- /** * 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)); } } }
// --------------------------------------------------------------------------- /** * 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); }
// --------------------------------------------------------------------------- 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)); } } }
// --------------------------------------------------------------------------- /** * 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()); } }
// --------------------------------------------------------------------------- /** * 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)); } } }
// --------------------------------------------------------------------------- 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); } }
// --------------------------------------------------------------------------- /** * 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 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 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 } } }
/** * 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(); }