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