Chapter 3: Adding content at absolute positions

This is a code example of iText PDF, discover more.

10th October 2015
admin-marketing

Switch code for this example

FestivalOpening.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Element;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Image;
  18. import com.itextpdf.text.PageSize;
  19. import com.itextpdf.text.Paragraph;
  20. import com.itextpdf.text.Font.FontFamily;
  21. import com.itextpdf.text.pdf.BaseFont;
  22. import com.itextpdf.text.pdf.PdfContentByte;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. public class FestivalOpening {
  26.  
  27.     /** The resulting PDF. */
  28.     public static final String RESULT
  29.         = "results/part1/chapter03/festival_opening.pdf";
  30.     /** The movie poster. */
  31.     public static final String RESOURCE = "resources/img/loa.jpg";
  32.  
  33.     /**
  34.      * Main method.
  35.      * @param    args    no arguments needed
  36.      * @throws DocumentException
  37.      * @throws IOException
  38.      */
  39.     public static void main(String[] args)
  40.         throws IOException, DocumentException {
  41.         // step 1
  42.         Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
  43.         // step 2
  44.         PdfWriter writer
  45.             = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         // Create and add a Paragraph
  50.         Paragraph p
  51.             = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
  52.         p.setAlignment(Element.ALIGN_CENTER);
  53.         document.add(p);
  54.         // Create and add an Image
  55.         Image img = Image.getInstance(RESOURCE);
  56.         img.setAbsolutePosition(
  57.             (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
  58.             (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
  59.         document.add(img);
  60.         // Now we go to the next page
  61.         document.newPage();
  62.         document.add(p);
  63.         document.add(img);
  64.         // Add text on top of the image
  65.         PdfContentByte over = writer.getDirectContent();
  66.         over.saveState();
  67.         float sinus = (float)Math.sin(Math.PI / 60);
  68.         float cosinus = (float)Math.cos(Math.PI / 60);
  69.         BaseFont bf = BaseFont.createFont();
  70.         over.beginText();
  71.         over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
  72.         over.setLineWidth(1.5f);
  73.         over.setRGBColorStroke(0xFF, 0x00, 0x00);
  74.         over.setRGBColorFill(0xFF, 0xFF, 0xFF);
  75.         over.setFontAndSize(bf, 36);
  76.         over.setTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324);
  77.         over.showText("SOLD OUT");
  78.         over.endText();
  79.         over.restoreState();
  80.         // Add a rectangle under the image
  81.         PdfContentByte under = writer.getDirectContentUnder();
  82.         under.saveState();
  83.         under.setRGBColorFill(0xFF, 0xD7, 0x00);
  84.         under.rectangle(5, 5,
  85.             PageSize.POSTCARD.getWidth() - 10, PageSize.POSTCARD.getHeight() - 10);
  86.         under.fill();
  87.         under.restoreState();
  88.         // step 5
  89.         document.close();
  90.     }
  91. }
GraphicsStateStack.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Rectangle;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class GraphicsStateStack {
  20.    
  21.     /** The resulting PDF. */
  22.     public static final String RESULT
  23.         = "results/part1/chapter03/graphics_state.pdf";
  24.  
  25.     /**
  26.      * Main method.
  27.      *
  28.      * @param    args    no arguments needed
  29.      * @throws DocumentException
  30.      * @throws IOException
  31.      */
  32.     public static void main(String[] args)
  33.         throws IOException, DocumentException {
  34.         // step 1
  35.         Document document = new Document(new Rectangle(200, 120));
  36.         // step 2
  37.         PdfWriter writer
  38.              = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         PdfContentByte canvas = writer.getDirectContent();
  43.         // state 1:
  44.         canvas.setRGBColorFill(0xFF, 0x45, 0x00);
  45.         // fill a rectangle in state 1
  46.         canvas.rectangle(10, 10, 60, 60);
  47.         canvas.fill();
  48.         canvas.saveState();
  49.         // state 2;
  50.         canvas.setLineWidth(3);
  51.         canvas.setRGBColorFill(0x8B, 0x00, 0x00);
  52.         // fill and stroke a rectangle in state 2
  53.         canvas.rectangle(40, 20, 60, 60);
  54.         canvas.fillStroke();
  55.         canvas.saveState();
  56.         // state 3:
  57.         canvas.concatCTM(1, 0, 0.1f, 1, 0, 0);
  58.         canvas.setRGBColorStroke(0xFF, 0x45, 0x00);
  59.         canvas.setRGBColorFill(0xFF, 0xD7, 0x00);
  60.         // fill and stroke a rectangle in state 3
  61.         canvas.rectangle(70, 30, 60, 60);
  62.         canvas.fillStroke();
  63.         canvas.restoreState();
  64.         // stroke a rectangle in state 2
  65.         canvas.rectangle(100, 40, 60, 60);
  66.         canvas.stroke();
  67.         canvas.restoreState();
  68.         // fill and stroke a rectangle in state 1
  69.         canvas.rectangle(130, 50, 60, 60);
  70.         canvas.fillStroke();
  71.         // step 5
  72.         document.close();
  73.     }
  74. }
MovieTimeTable.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.PageSize;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18. import com.itextpdf.text.BaseColor;
  19.  
  20. /**
  21.  * Draws a time table to the direct content using lines and simple shapes.
  22.  */
  23. public class MovieTimeTable {
  24.  
  25.     /** The resulting PDF. */
  26.     public static final String RESULT = "results/part1/chapter03/time_table.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file containing a time table for our filmfestival.
  30.      * @param    filename    the name of the PDF file
  31.      * @throws DocumentException
  32.      * @throws IOException
  33.      */
  34.     public void createPdf(String filename)
  35.         throws DocumentException, IOException {
  36.         // step 1
  37.         Document document = new Document(PageSize.A4.rotate());
  38.         // step 2
  39.         PdfWriter writer
  40.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         drawTimeTable(writer.getDirectContentUnder());
  45.         drawTimeSlots(writer.getDirectContent());
  46.         // step 5
  47.         document.close();
  48.     }
  49.  
  50.     /** The number of locations on our time table. */
  51.     public static final int LOCATIONS = 9;
  52.     /** The number of time slots on our time table. */
  53.     public static final int TIMESLOTS = 32;
  54.    
  55.     /** The offset to the left of our time table. */
  56.     public static final float OFFSET_LEFT = 76;
  57.     /** The width of our time table. */
  58.     public static final float WIDTH = 740;
  59.     /** The offset from the bottom of our time table. */
  60.     public static final float OFFSET_BOTTOM = 36;
  61.     /** The height of our time table */
  62.     public static final float HEIGHT = 504;
  63.    
  64.     /** The offset of the location bar next to our time table. */
  65.     public static final float OFFSET_LOCATION = 26;
  66.     /** The width of the location bar next to our time table. */
  67.     public static final float WIDTH_LOCATION = 48;
  68.    
  69.     /** The height of a bar showing the movies at one specific location. */
  70.     public static final float HEIGHT_LOCATION = HEIGHT / LOCATIONS;
  71.     /** The width of a time slot. */
  72.     public static final float WIDTH_TIMESLOT = WIDTH / TIMESLOTS;
  73.    
  74.     /**
  75.      * Draws the time table for a day at the film festival.
  76.      * @param directcontent a canvas to which the time table has to be drawn.
  77.      */
  78.     protected void drawTimeTable(PdfContentByte directcontent) {        
  79.         directcontent.saveState();
  80.        
  81.         directcontent.setLineWidth(1.2f);
  82.         float llx, lly, urx, ury;
  83.        
  84.         llx = OFFSET_LEFT;
  85.         lly = OFFSET_BOTTOM;
  86.         urx = OFFSET_LEFT + WIDTH;
  87.         ury = OFFSET_BOTTOM + HEIGHT;
  88.         directcontent.moveTo(llx, lly);
  89.         directcontent.lineTo(urx, lly);
  90.         directcontent.lineTo(urx, ury);
  91.         directcontent.lineTo(llx, ury);
  92.         directcontent.closePath();
  93.         directcontent.stroke();
  94.        
  95.         llx = OFFSET_LOCATION;
  96.         lly = OFFSET_BOTTOM;
  97.         urx = OFFSET_LOCATION + WIDTH_LOCATION;
  98.         ury = OFFSET_BOTTOM + HEIGHT;
  99.         directcontent.moveTo(llx, lly);
  100.         directcontent.lineTo(urx, lly);
  101.         directcontent.lineTo(urx, ury);
  102.         directcontent.lineTo(llx, ury);
  103.         directcontent.closePathStroke();
  104.        
  105.         directcontent.setLineWidth(1);
  106.         directcontent.moveTo(OFFSET_LOCATION + WIDTH_LOCATION / 2, OFFSET_BOTTOM);
  107.         directcontent.lineTo(OFFSET_LOCATION + WIDTH_LOCATION / 2, OFFSET_BOTTOM + HEIGHT);
  108.         float y;
  109.         for (int i = 1; i < LOCATIONS; i++) {
  110.             y = OFFSET_BOTTOM + (i * HEIGHT_LOCATION);
  111.             if (i == 2 || i == 6) {
  112.                 directcontent.moveTo(OFFSET_LOCATION, y);
  113.                 directcontent.lineTo(OFFSET_LOCATION + WIDTH_LOCATION, y);
  114.             }
  115.             else {
  116.                 directcontent.moveTo(OFFSET_LOCATION + WIDTH_LOCATION / 2, y);
  117.                 directcontent.lineTo(OFFSET_LOCATION + WIDTH_LOCATION, y);
  118.             }
  119.             directcontent.moveTo(OFFSET_LEFT, y);
  120.             directcontent.lineTo(OFFSET_LEFT + WIDTH, y);
  121.         }
  122.         directcontent.stroke();
  123.        
  124.         directcontent.restoreState();
  125.     }
  126.    
  127.     /**
  128.      * Draws the time slots for a day at the film festival.
  129.      * @param directcontent the canvas to which the time table has to be drawn.
  130.      */
  131.     protected void drawTimeSlots(PdfContentByte directcontent) {
  132.         directcontent.saveState();
  133.         float x;
  134.         for (int i = 1; i < TIMESLOTS; i++) {
  135.             x = OFFSET_LEFT + (i * WIDTH_TIMESLOT);
  136.             directcontent.moveTo(x, OFFSET_BOTTOM);
  137.             directcontent.lineTo(x, OFFSET_BOTTOM + HEIGHT);
  138.         }
  139.         directcontent.setLineWidth(0.3f);
  140.         directcontent.setColorStroke(BaseColor.GRAY);
  141.         directcontent.setLineDash(3, 1);
  142.         directcontent.stroke();
  143.         directcontent.restoreState();
  144.     }
  145.    
  146.     /**
  147.      * Main method creating the PDF.
  148.      * @param    args    no arguments needed
  149.      * @throws IOException
  150.      * @throws DocumentException
  151.      */
  152.     public static void main(String[] args)
  153.         throws DocumentException, IOException {
  154.         new MovieTimeTable().createPdf(RESULT);
  155.     }
  156. }
MovieTimeBlocks.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.sql.Date;
  14. import java.sql.Time;
  15. import java.util.List;
  16.  
  17. import com.lowagie.database.DatabaseConnection;
  18. import com.lowagie.database.HsqldbConnection;
  19. import com.lowagie.filmfestival.PojoFactory;
  20. import com.lowagie.filmfestival.Screening;
  21. import com.itextpdf.text.Document;
  22. import com.itextpdf.text.DocumentException;
  23. import com.itextpdf.text.PageSize;
  24. import com.itextpdf.text.Paragraph;
  25. import com.itextpdf.text.Rectangle;
  26. import com.itextpdf.text.html.WebColors;
  27. import com.itextpdf.text.pdf.PdfContentByte;
  28. import com.itextpdf.text.pdf.PdfWriter;
  29. import com.itextpdf.text.BaseColor;
  30.  
  31. /**
  32.  * Draws a time table to the direct content using lines and simple shapes,
  33.  * adding blocks representing a movies.
  34.  */
  35. public class MovieTimeBlocks extends MovieTimeTable {
  36.  
  37.     /** The resulting PDF. */
  38.     public static final String RESULT
  39.         = "results/part1/chapter03/time_blocks.pdf";
  40.  
  41.     /**
  42.      * Creates a PDF document.
  43.      * @param filename the path to the new PDF document
  44.      * @throws    DocumentException
  45.      * @throws    IOException
  46.      */
  47.     public void createPdf(String filename)
  48.         throws IOException, DocumentException {
  49.         // step 1
  50.         Document document = new Document(PageSize.A4.rotate());
  51.         // step 2
  52.         PdfWriter writer
  53.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  54.         // step 3
  55.         document.open();
  56.         // step 4
  57.         PdfContentByte over = writer.getDirectContent();
  58.         PdfContentByte under = writer.getDirectContentUnder();
  59.         try {
  60.             DatabaseConnection connection = new HsqldbConnection("filmfestival");
  61.             locations = PojoFactory.getLocations(connection);
  62.             List days = PojoFactory.getDays(connection);
  63.             List screenings;
  64.             for (Date day : days) {
  65.                 drawTimeTable(under);
  66.                 drawTimeSlots(over);
  67.                 screenings = PojoFactory.getScreenings(connection, day);
  68.                 for (Screening screening : screenings) {
  69.                     drawBlock(screening, under, over);
  70.                 }
  71.                 document.newPage();
  72.             }
  73.             connection.close();
  74.         }
  75.         catch(SQLException sqle) {
  76.             sqle.printStackTrace();
  77.             document.add(new Paragraph("Database error: " + sqle.getMessage()));
  78.         }
  79.         // step 5
  80.         document.close();
  81.     }
  82.    
  83.     /**
  84.      * Draws a colored block on the time table, corresponding with
  85.      * the screening of a specific movie.
  86.      * @param    screening    a screening POJO, contains a movie and a category
  87.      * @param    under    the canvas to which the block is drawn
  88.      */
  89.     protected void drawBlock(Screening screening, PdfContentByte under, PdfContentByte over) {
  90.         under.saveState();
  91.         BaseColor color = WebColors.getRGBColor(
  92.             "#" + screening.getMovie().getEntry().getCategory().getColor());
  93.         under.setColorFill(color);
  94.         Rectangle rect = getPosition(screening);
  95.         under.rectangle(
  96.                 rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
  97.         under.fill();
  98.         over.rectangle(
  99.             rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
  100.         over.stroke();
  101.         under.restoreState();
  102.     }
  103.    
  104.     /** The "offset time" for our calendar sheets. */
  105.     public static final long TIME930 = Time.valueOf("09:30:00").getTime();
  106.    
  107.     /** The width of one minute. */
  108.     public static final float MINUTE = WIDTH_TIMESLOT / 30f;
  109.    
  110.     /** A list containing all the locations. */
  111.     protected List locations;
  112.    
  113.     /**
  114.      * Calculates the position of a rectangle corresponding with a screening.
  115.      * @param    screening    a screening POJO, contains a movie
  116.      * @return    a Rectangle
  117.      */
  118.     protected Rectangle getPosition(Screening screening) {
  119.         float llx, lly, urx, ury;
  120.         long minutesAfter930 = (screening.getTime().getTime() - TIME930) / 60000l;
  121.         llx = OFFSET_LEFT + (MINUTE * minutesAfter930);
  122.         int location = locations.indexOf(screening.getLocation()) + 1;
  123.         lly = OFFSET_BOTTOM + (LOCATIONS - location) * HEIGHT_LOCATION;
  124.         urx = llx + MINUTE * screening.getMovie().getDuration();
  125.         ury = lly + HEIGHT_LOCATION;
  126.         Rectangle rect = new Rectangle(llx, lly, urx, ury);
  127.         return rect;
  128.     }
  129.    
  130.     /**
  131.      * Main method creating the PDF.
  132.      * @param    args    no arguments needed
  133.      * @throws DocumentException
  134.      * @throws IOException
  135.      */
  136.     public static void main(String[] args) throws IOException, DocumentException {
  137.         new MovieTimeBlocks().createPdf(RESULT);
  138.     }
  139. }
FoobarFilmFestival.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Element;
  17. import com.itextpdf.text.Font;
  18. import com.itextpdf.text.Paragraph;
  19. import com.itextpdf.text.Phrase;
  20. import com.itextpdf.text.Font.FontFamily;
  21. import com.itextpdf.text.pdf.BaseFont;
  22. import com.itextpdf.text.pdf.ColumnText;
  23. import com.itextpdf.text.pdf.PdfContentByte;
  24. import com.itextpdf.text.pdf.PdfWriter;
  25. import com.itextpdf.text.BaseColor;
  26.  
  27. public class FoobarFilmFestival {
  28.  
  29.     public static final String RESULT
  30.         = "results/part1/chapter03/foobar_film_festival.pdf";
  31.  
  32.     /**
  33.      * Main method.
  34.      *
  35.      * @param    args    no arguments needed
  36.      * @throws DocumentException
  37.      * @throws IOException
  38.      */
  39.     public static void main(String[] args)
  40.         throws IOException, DocumentException {
  41.         // step 1
  42.         Document document = new Document();
  43.         // step 2
  44.         PdfWriter writer
  45.             = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         Chunk c;
  50.         String foobar = "Foobar Film Festival";
  51.         // Measuring a String in Helvetica
  52.         Font helvetica = new Font(FontFamily.HELVETICA, 12);
  53.         BaseFont bf_helv = helvetica.getCalculatedBaseFont(false);
  54.         float width_helv = bf_helv.getWidthPoint(foobar, 12);
  55.         c = new Chunk(foobar + ": " + width_helv, helvetica);
  56.         document.add(new Paragraph(c));
  57.         document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
  58.         // Measuring a String in Times
  59.         BaseFont bf_times = BaseFont.createFont(
  60.             "c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
  61.         Font times = new Font(bf_times, 12);
  62.         float width_times = bf_times.getWidthPoint(foobar, 12);
  63.         c = new Chunk(foobar + ": " + width_times, times);
  64.         document.add(new Paragraph(c));
  65.         document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
  66.         document.add(Chunk.NEWLINE);
  67.         // Ascent and descent of the String
  68.         document.add(new Paragraph("Ascent Helvetica: "
  69.                 + bf_helv.getAscentPoint(foobar, 12)));
  70.         document.add(new Paragraph("Ascent Times: "
  71.                 + bf_times.getAscentPoint(foobar, 12)));
  72.         document.add(new Paragraph("Descent Helvetica: "
  73.                 + bf_helv.getDescentPoint(foobar, 12)));
  74.         document.add(new Paragraph("Descent Times: "
  75.                 + bf_times.getDescentPoint(foobar, 12)));
  76.         document.add(Chunk.NEWLINE);
  77.         // Kerned text
  78.         width_helv = bf_helv.getWidthPointKerned(foobar, 12);
  79.         c = new Chunk(foobar + ": " + width_helv, helvetica);
  80.         document.add(new Paragraph(c));
  81.         // Drawing lines to see where the text is added
  82.         PdfContentByte canvas = writer.getDirectContent();
  83.         canvas.saveState();
  84.         canvas.setLineWidth(0.05f);
  85.         canvas.moveTo(400, 806);
  86.         canvas.lineTo(400, 626);
  87.         canvas.moveTo(508.7f, 806);
  88.         canvas.lineTo(508.7f, 626);
  89.         canvas.moveTo(280, 788);
  90.         canvas.lineTo(520, 788);
  91.         canvas.moveTo(280, 752);
  92.         canvas.lineTo(520, 752);
  93.         canvas.moveTo(280, 716);
  94.         canvas.lineTo(520, 716);
  95.         canvas.moveTo(280, 680);
  96.         canvas.lineTo(520, 680);
  97.         canvas.moveTo(280, 644);
  98.         canvas.lineTo(520, 644);
  99.         canvas.stroke();
  100.         canvas.restoreState();
  101.         // Adding text with PdfContentByte.showTextAligned()
  102.         canvas.beginText();
  103.         canvas.setFontAndSize(bf_helv, 12);
  104.         canvas.showTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
  105.         canvas.showTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
  106.         canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
  107.         canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
  108.         canvas.showTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
  109.         canvas.endText();
  110.         // More lines to see where the text is added
  111.         canvas.saveState();
  112.         canvas.setLineWidth(0.05f);
  113.         canvas.moveTo(200, 590);
  114.         canvas.lineTo(200, 410);
  115.         canvas.moveTo(400, 590);
  116.         canvas.lineTo(400, 410);
  117.         canvas.moveTo(80, 572);
  118.         canvas.lineTo(520, 572);
  119.         canvas.moveTo(80, 536);
  120.         canvas.lineTo(520, 536);
  121.         canvas.moveTo(80, 500);
  122.         canvas.lineTo(520, 500);
  123.         canvas.moveTo(80, 464);
  124.         canvas.lineTo(520, 464);
  125.         canvas.moveTo(80, 428);
  126.         canvas.lineTo(520, 428);
  127.         canvas.stroke();
  128.         canvas.restoreState();
  129.         // Adding text with ColumnText.showTextAligned()
  130.         Phrase phrase = new Phrase(foobar, times);
  131.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
  132.         ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
  133.         ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
  134.         ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
  135.         ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
  136.         // Chunk attributes
  137.         c = new Chunk(foobar, times);
  138.         c.setHorizontalScaling(0.5f);
  139.         phrase = new Phrase(c);
  140.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
  141.         c = new Chunk(foobar, times);
  142.         c.setSkew(15, 15);
  143.         phrase = new Phrase(c);
  144.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
  145.         c = new Chunk(foobar, times);
  146.         c.setSkew(0, 25);
  147.         phrase = new Phrase(c);
  148.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
  149.         c = new Chunk(foobar, times);
  150.         c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
  151.         phrase = new Phrase(c);
  152.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
  153.         c = new Chunk(foobar, times);
  154.         c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
  155.         phrase = new Phrase(c);
  156.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);
  157.         // step 5
  158.         document.close();
  159.     }
  160. }
MovieTextInfo.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.sql.Date;
  14. import java.util.List;
  15.  
  16. import com.lowagie.database.DatabaseConnection;
  17. import com.lowagie.database.HsqldbConnection;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.lowagie.filmfestival.Screening;
  20. import com.itextpdf.text.Document;
  21. import com.itextpdf.text.DocumentException;
  22. import com.itextpdf.text.Element;
  23. import com.itextpdf.text.Font;
  24. import com.itextpdf.text.PageSize;
  25. import com.itextpdf.text.Paragraph;
  26. import com.itextpdf.text.Phrase;
  27. import com.itextpdf.text.Rectangle;
  28. import com.itextpdf.text.pdf.BaseFont;
  29. import com.itextpdf.text.pdf.ColumnText;
  30. import com.itextpdf.text.pdf.PdfContentByte;
  31. import com.itextpdf.text.pdf.PdfWriter;
  32. import com.itextpdf.text.BaseColor;
  33.  
  34. /**
  35.  * Draws a time table to the direct content using lines and simple shapes,
  36.  * adding blocks representing a movies.
  37.  */
  38. public class MovieTextInfo extends MovieTimeBlocks {
  39.  
  40.     /** The resulting PDF. */
  41.     public static final String RESULT = "results/part1/chapter03/calendar.pdf";
  42.  
  43.     /**
  44.      * Creates a PDF document.
  45.      * @param filename the path to the new PDF document
  46.      * @throws    DocumentException
  47.      * @throws    IOException
  48.      */
  49.     public void createPdf(String filename)
  50.         throws IOException, DocumentException {
  51.         // step 1
  52.         Document document = new Document(PageSize.A4.rotate());
  53.         // step 2
  54.         PdfWriter writer
  55.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  56.         // step 3
  57.         document.open();
  58.         // step 4
  59.         PdfContentByte over = writer.getDirectContent();
  60.         PdfContentByte under = writer.getDirectContentUnder();
  61.         try {
  62.             DatabaseConnection connection = new HsqldbConnection("filmfestival");
  63.             locations = PojoFactory.getLocations(connection);
  64.             List days = PojoFactory.getDays(connection);
  65.             List screenings;
  66.             int d = 1;
  67.             for (Date day : days) {
  68.                 drawTimeTable(under);
  69.                 drawTimeSlots(over);
  70.                 drawInfo(over);
  71.                 drawDateInfo(day, d++, over);
  72.                 screenings = PojoFactory.getScreenings(connection, day);
  73.                 for (Screening screening : screenings) {
  74.                     drawBlock(screening, under, over);
  75.                     drawMovieInfo(screening, over);
  76.                 }
  77.                 document.newPage();
  78.             }
  79.             connection.close();
  80.         }
  81.         catch(SQLException sqle) {
  82.             sqle.printStackTrace();
  83.             document.add(new Paragraph("Database error: " + sqle.getMessage()));
  84.         }
  85.         // step 5
  86.         document.close();
  87.     }
  88.    
  89.     /** The base font that will be used to write info on the calendar sheet. */
  90.     protected BaseFont bf;
  91.    
  92.     /** A phrase containing a white letter "P" (for Press) */
  93.     protected Phrase press;
  94.  
  95.     /** The different time slots. */
  96.     public static String[] TIME =
  97.         { "09:30", "10:00", "10:30", "11:00", "11:30", "12:00",
  98.           "00:30", "01:00", "01:30", "02:00", "02:30", "03:00",
  99.           "03:30", "04:00", "04:30", "05:00", "05:30", "06:00",
  100.           "06:30", "07:00", "07:30", "08:00", "08:30", "09:00",
  101.           "09:30", "10:00", "10:30", "11:00", "11:30", "12:00",
  102.           "00:30", "01:00" };
  103.    
  104.     /**
  105.      * Draws some text on every calendar sheet.
  106.      *
  107.      */
  108.     protected void drawInfo(PdfContentByte directcontent) {
  109.         directcontent.beginText();
  110.         directcontent.setFontAndSize(bf, 18);
  111.         float x, y;
  112.         x = (OFFSET_LEFT + WIDTH + OFFSET_LOCATION) / 2;
  113.         y = OFFSET_BOTTOM + HEIGHT + 24;
  114.         directcontent.showTextAligned(Element.ALIGN_CENTER,
  115.             "FOOBAR FILM FESTIVAL", x, y, 0);
  116.         x = OFFSET_LOCATION + WIDTH_LOCATION / 2f - 6;
  117.         y = OFFSET_BOTTOM + HEIGHT_LOCATION;
  118.         directcontent.showTextAligned(Element.ALIGN_CENTER,
  119.             "The Majestic", x, y, 90);
  120.         y = OFFSET_BOTTOM + HEIGHT_LOCATION * 4f;
  121.         directcontent.showTextAligned(Element.ALIGN_CENTER,
  122.             "Googolplex", x, y, 90);
  123.         y = OFFSET_BOTTOM + HEIGHT_LOCATION * 7.5f;
  124.         directcontent.showTextAligned(Element.ALIGN_CENTER,
  125.             "Cinema Paradiso", x, y, 90);
  126.         directcontent.setFontAndSize(bf, 12);
  127.         x = OFFSET_LOCATION + WIDTH_LOCATION - 6;
  128.         for (int i = 0; i < LOCATIONS; i++) {
  129.             y = OFFSET_BOTTOM + ((8.5f - i) * HEIGHT_LOCATION);
  130.             directcontent.showTextAligned(Element.ALIGN_CENTER,
  131.                 locations.get(i), x, y, 90);
  132.         }
  133.         directcontent.setFontAndSize(bf, 6);
  134.         y = OFFSET_BOTTOM + HEIGHT + 1;
  135.         for (int i = 0; i < TIMESLOTS; i++) {
  136.             x = OFFSET_LEFT + (i * WIDTH_TIMESLOT);
  137.             directcontent.showTextAligned(Element.ALIGN_LEFT,
  138.                 TIME[i], x, y, 45);
  139.         }
  140.         directcontent.endText();
  141.     }
  142.     /**
  143.      * Draws some text on every calendar sheet.
  144.      *
  145.      */
  146.     protected void drawDateInfo(Date day, int d, PdfContentByte directcontent) {
  147.         directcontent.beginText();
  148.         directcontent.setFontAndSize(bf, 18);
  149.         float x, y;
  150.         x = OFFSET_LOCATION;
  151.         y = OFFSET_BOTTOM + HEIGHT + 24;
  152.         directcontent.showTextAligned(Element.ALIGN_LEFT,
  153.                 "Day " + d, x, y, 0);
  154.         x = OFFSET_LEFT + WIDTH;
  155.         directcontent.showTextAligned(Element.ALIGN_RIGHT,
  156.                 day.toString(), x, y, 0);
  157.         directcontent.endText();
  158.     }
  159.    
  160.     /**
  161.      * Draws the info about the movie.
  162.      * @throws DocumentException
  163.      */
  164.     protected void drawMovieInfo(Screening screening, PdfContentByte directcontent)
  165.         throws DocumentException {
  166.         if (screening.isPress()) {
  167.             Rectangle rect = getPosition(screening);
  168.             ColumnText.showTextAligned(directcontent, Element.ALIGN_CENTER,
  169.                     press, (rect.getLeft() + rect.getRight()) / 2,
  170.                     rect.getBottom() + rect.getHeight() / 4, 0);
  171.         }
  172.     }
  173.    
  174.     /**
  175.      * Constructor for the MovieCalendar class; initializes the base font object.
  176.      * @throws IOException
  177.      * @throws DocumentException
  178.      */
  179.     public MovieTextInfo() throws DocumentException, IOException {
  180.         bf = BaseFont.createFont();
  181.         Font f = new Font(bf, HEIGHT_LOCATION / 2);
  182.         f.setColor(BaseColor.WHITE);
  183.         press = new Phrase("P", f);
  184.     }
  185.    
  186.     /**
  187.      * Main method creating the PDF.
  188.      * @param    args    no arguments needed
  189.      * @throws DocumentException
  190.      * @throws IOException
  191.      */
  192.     public static void main(String[] args)
  193.         throws IOException, DocumentException {
  194.         new MovieTextInfo().createPdf(RESULT);
  195.     }
  196. }
MovieCalendar.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.IOException;
  11.  
  12. import com.lowagie.filmfestival.Screening;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Element;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.Rectangle;
  17. import com.itextpdf.text.pdf.ColumnText;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19.  
  20. /**
  21.  * Draws a time table to the direct content using lines and simple shapes,
  22.  * adding blocks representing a movies.
  23.  */
  24. public class MovieCalendar extends MovieTextInfo {
  25.  
  26.     /** The resulting PDF. */
  27.     public static final String RESULT = "results/part1/chapter03/movie_calendar.pdf";
  28.    
  29.     /**
  30.      * Draws the info about the movie.
  31.      * @throws DocumentException
  32.      */
  33.     protected void drawMovieInfo(Screening screening, PdfContentByte directcontent)
  34.         throws DocumentException {
  35.         super.drawMovieInfo(screening, directcontent);
  36.         Rectangle rect = getPosition(screening);
  37.         ColumnText column = new ColumnText(directcontent);
  38.         column.setSimpleColumn(new Phrase(screening.getMovie().getMovieTitle()),
  39.                 rect.getLeft(), rect.getBottom(),
  40.                 rect.getRight(), rect.getTop(), 18, Element.ALIGN_CENTER);
  41.         column.go();
  42.     }
  43.    
  44.     /**
  45.      * Constructor for the MovieCalendar class; initializes the base font object.
  46.      * @throws IOException
  47.      * @throws DocumentException
  48.      */
  49.     public MovieCalendar() throws DocumentException, IOException {
  50.         super();
  51.     }
  52.    
  53.     /**
  54.      * Main method creating the PDF.
  55.      * @param    args    no arguments needed
  56.      * @throws DocumentException
  57.      * @throws IOException
  58.      */
  59.     public static void main(String[] args)
  60.         throws IOException, DocumentException {
  61.         new MovieCalendar().createPdf(RESULT);
  62.     }
  63. }
MovieColumns1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.Country;
  18. import com.lowagie.filmfestival.Director;
  19. import com.lowagie.filmfestival.FilmFonts;
  20. import com.lowagie.filmfestival.Movie;
  21. import com.lowagie.filmfestival.PojoFactory;
  22. import com.lowagie.filmfestival.PojoToElementFactory;
  23. import com.itextpdf.text.Chunk;
  24. import com.itextpdf.text.Document;
  25. import com.itextpdf.text.DocumentException;
  26. import com.itextpdf.text.Element;
  27. import com.itextpdf.text.Phrase;
  28. import com.itextpdf.text.pdf.ColumnText;
  29. import com.itextpdf.text.pdf.PdfWriter;
  30. import com.itextpdf.text.pdf.draw.LineSeparator;
  31.  
  32. public class MovieColumns1 {
  33.  
  34.     /** The resulting PDF file. */
  35.     public static final String RESULT
  36.         = "results/part1/chapter03/movie_columns1.pdf";
  37.    
  38.     /** Definition of two columns */
  39.     public static final float[][] COLUMNS = {
  40.         { 36, 36, 296, 806 } , { 299, 36, 559, 806 }
  41.     };
  42.    
  43.     /**
  44.      * Creates a PDF with information about the movies
  45.      * @param    filename the name of the PDF file that will be created.
  46.      * @throws    DocumentException
  47.      * @throws    IOException
  48.      * @throws    SQLException
  49.      */
  50.     public void createPdf(String filename)
  51.         throws IOException, DocumentException, SQLException {
  52.         // Create a database connection
  53.         DatabaseConnection connection = new HsqldbConnection("filmfestival");    
  54.         // step 1
  55.         Document document = new Document();
  56.         // step 2
  57.         PdfWriter writer
  58.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  59.         // step 3
  60.         document.open();
  61.         // step 4
  62.         List movies = PojoFactory.getMovies(connection);
  63.         ColumnText ct = new ColumnText(writer.getDirectContent());
  64.         for (Movie movie : movies) {
  65.             ct.addText(createMovieInformation(movie));
  66.             ct.addText(Chunk.NEWLINE);
  67.         }
  68.         ct.setAlignment(Element.ALIGN_JUSTIFIED);
  69.         ct.setExtraParagraphSpace(6);
  70.         ct.setLeading(0, 1.2f);
  71.         ct.setFollowingIndent(27);
  72.         int linesWritten = 0;
  73.         int column = 0;
  74.         int status = ColumnText.START_COLUMN;
  75.         while (ColumnText.hasMoreText(status)) {
  76.             ct.setSimpleColumn(
  77.                     COLUMNS[column][0], COLUMNS[column][1],
  78.                     COLUMNS[column][2], COLUMNS[column][3]);
  79.             ct.setYLine(COLUMNS[column][3]);
  80.             status = ct.go();
  81.             linesWritten += ct.getLinesWritten();
  82.             column = Math.abs(column - 1);
  83.             if (column == 0)
  84.                 document.newPage();
  85.         }
  86.        
  87.         ct.addText(new Phrase("Lines written: " + linesWritten));
  88.         ct.go();
  89.         // step 5
  90.         document.close();
  91.         // Close the database connection
  92.         connection.close();
  93.     }
  94.    
  95.     /**
  96.      * Creates a Phrase containing information about a movie.
  97.      * @param    movie    the movie for which you want to create a Paragraph
  98.      */
  99.     public Phrase createMovieInformation(Movie movie) {
  100.         Phrase p = new Phrase();
  101.         p.setFont(FilmFonts.NORMAL);
  102.         p.add(new Phrase("Title: ", FilmFonts.BOLDITALIC));
  103.         p.add(PojoToElementFactory.getMovieTitlePhrase(movie));
  104.         p.add(" ");
  105.         if (movie.getOriginalTitle() != null) {
  106.             p.add(new Phrase("Original title: ", FilmFonts.BOLDITALIC));
  107.             p.add(PojoToElementFactory.getOriginalTitlePhrase(movie));
  108.             p.add(" ");
  109.         }
  110.         p.add(new Phrase("Country: ", FilmFonts.BOLDITALIC));
  111.         for (Country country : movie.getCountries()) {
  112.             p.add(PojoToElementFactory.getCountryPhrase(country));
  113.             p.add(" ");
  114.         }
  115.         p.add(new Phrase("Director: ", FilmFonts.BOLDITALIC));
  116.         for (Director director : movie.getDirectors()) {
  117.             p.add(PojoToElementFactory.getDirectorPhrase(director));
  118.             p.add(" ");
  119.         }
  120.         p.add(new Chunk("Year: ", FilmFonts.BOLDITALIC));
  121.         p.add(new Chunk(String.valueOf(movie.getYear()), FilmFonts.NORMAL));
  122.         p.add(new Chunk(" Duration: ", FilmFonts.BOLDITALIC));
  123.         p.add(new Chunk(String.valueOf(movie.getDuration()), FilmFonts.NORMAL));
  124.         p.add(new Chunk(" minutes", FilmFonts.NORMAL));
  125.         p.add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2));
  126.         return p;
  127.     }
  128.  
  129.     /**
  130.      * Main method.
  131.      *
  132.      * @param    args    no arguments needed
  133.      * @throws DocumentException
  134.      * @throws IOException
  135.      * @throws SQLException
  136.      */
  137.     public static void main(String[] args)
  138.         throws IOException, DocumentException, SQLException {
  139.         new MovieColumns1().createPdf(RESULT);
  140.     }
  141. }
MovieColumns2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.Movie;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.itextpdf.text.Document;
  20. import com.itextpdf.text.DocumentException;
  21. import com.itextpdf.text.Element;
  22. import com.itextpdf.text.pdf.ColumnText;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. public class MovieColumns2 extends MovieColumns1 {
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String RESULT
  29.         = "results/part1/chapter03/movie_columns2.pdf";
  30.    
  31.     /**
  32.      * Creates a PDF with information about the movies
  33.      * @param    filename the name of the PDF file that will be created.
  34.      * @throws    DocumentException
  35.      * @throws    IOException
  36.      * @throws    SQLException
  37.      */
  38.     public void createPdf(String filename)
  39.         throws IOException, DocumentException, SQLException {
  40.         // Create a database connection
  41.         DatabaseConnection connection = new HsqldbConnection("filmfestival");    
  42.         // step 1
  43.         Document document = new Document();
  44.         // step 2
  45.         PdfWriter writer
  46.              = PdfWriter.getInstance(document, new FileOutputStream(filename));
  47.         // step 3
  48.         document.open();
  49.         // step 4
  50.         List movies = PojoFactory.getMovies(connection);
  51.         ColumnText ct = new ColumnText(writer.getDirectContent());
  52.         ct.setAlignment(Element.ALIGN_JUSTIFIED);
  53.         ct.setExtraParagraphSpace(6);
  54.         ct.setLeading(14);
  55.         ct.setIndent(10);
  56.         ct.setRightIndent(3);
  57.         ct.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
  58.         int column = 0;
  59.         int status = ColumnText.START_COLUMN;
  60.         ct.setSimpleColumn(
  61.             COLUMNS[column][0], COLUMNS[column][1],
  62.             COLUMNS[column][2], COLUMNS[column][3]);
  63.         for (Movie movie : movies) {
  64.             ct.addText(createMovieInformation(movie));
  65.             status = ct.go();
  66.             if (ColumnText.hasMoreText(status)) {
  67.                 column = Math.abs(column - 1);
  68.                 if (column == 0)
  69.                     document.newPage();
  70.                 ct.setSimpleColumn(
  71.                     COLUMNS[column][0], COLUMNS[column][1],
  72.                     COLUMNS[column][2], COLUMNS[column][3]);
  73.                 ct.setYLine(COLUMNS[column][3]);
  74.                 status = ct.go();
  75.             }
  76.         }
  77.         // step 5
  78.         document.close();
  79.         // Close the database connection
  80.         connection.close();
  81.     }
  82.  
  83.     /**
  84.      * Main method.
  85.      *
  86.      * @param    args    no arguments needed
  87.      * @throws DocumentException
  88.      * @throws IOException
  89.      * @throws SQLException
  90.      */
  91.     public static void main(String[] args)
  92.         throws IOException, DocumentException, SQLException {
  93.         new MovieColumns2().createPdf(RESULT);
  94.     }
  95. }
MovieColumns3.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.Movie;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.itextpdf.text.Document;
  20. import com.itextpdf.text.DocumentException;
  21. import com.itextpdf.text.Phrase;
  22. import com.itextpdf.text.pdf.ColumnText;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. public class MovieColumns3 extends MovieColumns1 {
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String RESULT = "results/part1/chapter03/movie_columns3.pdf";
  29.    
  30.     /**
  31.      * Creates a PDF with information about the movies
  32.      * @param    filename the name of the PDF file that will be created.
  33.      * @throws    DocumentException
  34.      * @throws    IOException
  35.      * @throws    SQLException
  36.      */
  37.     public void createPdf(String filename)
  38.         throws IOException, DocumentException, SQLException {
  39.         // Create a database connection
  40.         DatabaseConnection connection = new HsqldbConnection("filmfestival");
  41.         // step 1
  42.         Document document = new Document();
  43.         // step 2
  44.         PdfWriter writer
  45.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         List movies = PojoFactory.getMovies(connection);
  50.         ColumnText ct = new ColumnText(writer.getDirectContent());
  51.         int column = 0;
  52.         ct.setSimpleColumn(
  53.             COLUMNS[column][0], COLUMNS[column][1],
  54.             COLUMNS[column][2], COLUMNS[column][3]);
  55.         int status = ColumnText.START_COLUMN;
  56.         Phrase p;
  57.         float y;
  58.         for (Movie movie : movies) {
  59.             y = ct.getYLine();
  60.             p = createMovieInformation(movie);
  61.             ct.addText(p);
  62.             status = ct.go(true);
  63.             if (ColumnText.hasMoreText(status)) {
  64.                 column = Math.abs(column - 1);
  65.                 if (column == 0)
  66.                     document.newPage();
  67.                 ct.setSimpleColumn(
  68.                     COLUMNS[column][0], COLUMNS[column][1],
  69.                     COLUMNS[column][2], COLUMNS[column][3]);
  70.                 y = COLUMNS[column][3];
  71.             }
  72.             ct.setYLine(y);
  73.             ct.setText(p);
  74.             status = ct.go(false);
  75.         }
  76.         // step 5
  77.         document.close();
  78.         // Close the database connection
  79.         connection.close();
  80.     }
  81.  
  82.     /**
  83.      * Main method.
  84.      *
  85.      * @param    args    no arguments needed
  86.      * @throws DocumentException
  87.      * @throws IOException
  88.      * @throws SQLException
  89.      */
  90.     public static void main(String[] args)
  91.         throws IOException, DocumentException, SQLException {
  92.         new MovieColumns3().createPdf(RESULT);
  93.     }
  94. }
MovieColumns4.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.Movie;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.itextpdf.text.Document;
  20. import com.itextpdf.text.DocumentException;
  21. import com.itextpdf.text.Element;
  22. import com.itextpdf.text.Phrase;
  23. import com.itextpdf.text.pdf.ColumnText;
  24. import com.itextpdf.text.pdf.PdfContentByte;
  25. import com.itextpdf.text.pdf.PdfWriter;
  26.  
  27. public class MovieColumns4 extends MovieColumns1 {
  28.  
  29.     /** The resulting PDF file. */
  30.     public static final String RESULT = "results/part1/chapter03/movie_columns4.pdf";
  31.    
  32.     public static final float[][] LEFT =
  33.         { { 36,806, 36,670, 108,670, 108,596, 36,596, 36,36 },
  34.         { 299,806, 299,484, 336,484, 336,410, 299,410, 299,36 } };
  35.     public static final float[][] RIGHT =
  36.         { { 296,806, 296,484, 259,484, 259,410, 296,410, 296,36 },
  37.         { 559,806, 559,246, 487,246, 487,172, 559,172, 559,36 } };
  38.    
  39.     /**
  40.      * Creates a PDF with information about the movies
  41.      * @param    filename the name of the PDF file that will be created.
  42.      * @throws    DocumentException
  43.      * @throws    IOException
  44.      * @throws    SQLException
  45.      */
  46.     public void createPdf(String filename)
  47.         throws IOException, DocumentException, SQLException {
  48.         // Create a database connection
  49.         DatabaseConnection connection = new HsqldbConnection("filmfestival");    
  50.         // step 1
  51.         Document document = new Document();
  52.         // step 2
  53.         PdfWriter writer
  54.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  55.         // step 3
  56.         document.open();
  57.         // step 4
  58.         PdfContentByte canvas = writer.getDirectContent();
  59.         drawRectangles(canvas);
  60.         List movies = PojoFactory.getMovies(connection);
  61.         ColumnText ct = new ColumnText(canvas);
  62.         ct.setAlignment(Element.ALIGN_JUSTIFIED);
  63.         ct.setLeading(14);
  64.         int column = 0;
  65.         ct.setColumns(LEFT[column], RIGHT[column]);
  66.         int status = ColumnText.START_COLUMN;
  67.         Phrase p;
  68.         float y;
  69.         for (Movie movie : movies) {
  70.             y = ct.getYLine();
  71.             p = createMovieInformation(movie);
  72.             ct.addText(p);
  73.             status = ct.go(true);
  74.             if (ColumnText.hasMoreText(status)) {
  75.                 column = Math.abs(column - 1);
  76.                 if (column == 0) {
  77.                     document.newPage();
  78.                     drawRectangles(canvas);
  79.                 }
  80.                 ct.setColumns(LEFT[column], RIGHT[column]);
  81.                 y = 806;
  82.             }
  83.             ct.setYLine(y);
  84.             ct.setText(p);
  85.             status = ct.go();
  86.         }
  87.         // step 5
  88.         document.close();
  89.         // Close the database connection
  90.         connection.close();
  91.     }
  92.    
  93.     /**
  94.      * Draws three rectangles
  95.      * @param canvas
  96.      */
  97.     public void drawRectangles(PdfContentByte canvas) {
  98.         canvas.saveState();
  99.         canvas.setGrayFill(0.9f);
  100.         canvas.rectangle(33, 592, 72, 72);
  101.         canvas.rectangle(263, 406, 72, 72);
  102.         canvas.rectangle(491, 168, 72, 72);
  103.         canvas.fillStroke();
  104.         canvas.restoreState();
  105.     }
  106.  
  107.     /**
  108.      * Main method.
  109.      *
  110.      * @param    args    no arguments needed
  111.      * @throws DocumentException
  112.      * @throws IOException
  113.      * @throws SQLException
  114.      */
  115.     public static void main(String[] args)
  116.        throws IOException, DocumentException, SQLException {
  117.         new MovieColumns4().createPdf(RESULT);
  118.     }
  119. }
ColumnMovies1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.FilmFonts;
  18. import com.lowagie.filmfestival.Movie;
  19. import com.lowagie.filmfestival.PojoFactory;
  20. import com.lowagie.filmfestival.PojoToElementFactory;
  21. import com.itextpdf.text.Chunk;
  22. import com.itextpdf.text.Document;
  23. import com.itextpdf.text.DocumentException;
  24. import com.itextpdf.text.Image;
  25. import com.itextpdf.text.PageSize;
  26. import com.itextpdf.text.Paragraph;
  27. import com.itextpdf.text.pdf.ColumnText;
  28. import com.itextpdf.text.pdf.PdfWriter;
  29.  
  30. public class ColumnMovies1 {
  31.  
  32.     /** The resulting PDF file. */
  33.     public static final String RESULT
  34.         = "results/part1/chapter03/column_movies1.pdf";
  35.     /** Path to the resources. */
  36.     public static final String RESOURCE
  37.         = "resources/posters/%s.jpg";
  38.    
  39.     /** Definition of two columns */
  40.     public static final float[][] COLUMNS = {
  41.         { 36, 36, 224, 579 } , { 230, 36, 418, 579 },
  42.         { 424, 36, 612, 579 } , { 618, 36, 806, 579 }
  43.     };
  44.    
  45.     /**
  46.      * Creates a PDF with information about the movies
  47.      * @param    filename the name of the PDF file that will be created.
  48.      * @throws    DocumentException
  49.      * @throws    IOException
  50.      * @throws    SQLException
  51.      */
  52.     public void createPdf(String filename)
  53.         throws IOException, DocumentException, SQLException {
  54.         // Create a database connection
  55.         DatabaseConnection connection = new HsqldbConnection("filmfestival");
  56.         // step 1
  57.         Document document = new Document(PageSize.A4.rotate());
  58.         // step 2
  59.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  60.         // step 3
  61.         document.open();
  62.         // step 4
  63.         List movies = PojoFactory.getMovies(connection);
  64.         ColumnText ct = new ColumnText(writer.getDirectContent());
  65.         int column = 0;
  66.         ct.setSimpleColumn(
  67.             COLUMNS[column][0], COLUMNS[column][1],
  68.             COLUMNS[column][2], COLUMNS[column][3]);
  69.         int status = ColumnText.START_COLUMN;
  70.         float y;
  71.         Image img;
  72.         for (Movie movie : movies) {
  73.             y = ct.getYLine();
  74.             img = Image.getInstance(String.format(RESOURCE, movie.getImdb()));
  75.             img.scaleToFit(80, 1000);
  76.             addContent(ct, movie, img);
  77.             status = ct.go(true);
  78.             if (ColumnText.hasMoreText(status)) {
  79.                 column = (column + 1) % 4;
  80.                 if (column == 0)
  81.                     document.newPage();
  82.                 ct.setSimpleColumn(
  83.                     COLUMNS[column][0], COLUMNS[column][1],
  84.                     COLUMNS[column][2], COLUMNS[column][3]);
  85.                 y = COLUMNS[column][3];
  86.             }
  87.             ct.setYLine(y);
  88.             ct.setText(null);
  89.             addContent(ct, movie, img);
  90.             status = ct.go();
  91.         }
  92.         // step 5
  93.         document.close();
  94.         // Close the database connection
  95.         connection.close();
  96.     }
  97.    
  98.     /**
  99.      * Add content to a ColumnText object.
  100.      * @param ct the ColumnText object
  101.      * @param movie a Movie object
  102.      * @param img the poster of the image
  103.      */
  104.     public void addContent(ColumnText ct, Movie movie, Image img) {
  105.         ct.addElement(img);
  106.         ct.addElement(new Paragraph(movie.getTitle(), FilmFonts.BOLD));
  107.         if (movie.getOriginalTitle() != null) {
  108.             ct.addElement(new Paragraph(movie.getOriginalTitle(), FilmFonts.ITALIC));
  109.         }
  110.         ct.addElement(PojoToElementFactory.getDirectorList(movie));
  111.         ct.addElement(PojoToElementFactory.getYearPhrase(movie));
  112.         ct.addElement(PojoToElementFactory.getDurationPhrase(movie));
  113.         ct.addElement(PojoToElementFactory.getCountryList(movie));
  114.         ct.addElement(Chunk.NEWLINE);
  115.     }
  116.  
  117.     /**
  118.      * Main method.
  119.      * @param    args    no arguments needed
  120.      * @throws DocumentException
  121.      * @throws IOException
  122.      * @throws SQLException
  123.      */
  124.     public static void main(String[] args) throws IOException, DocumentException, SQLException {
  125.         new ColumnMovies1().createPdf(RESULT);
  126.     }
  127. }
ColumnMovies2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import part1.chapter02.StarSeparator;
  16.  
  17. import com.lowagie.database.DatabaseConnection;
  18. import com.lowagie.database.HsqldbConnection;
  19. import com.lowagie.filmfestival.FilmFonts;
  20. import com.lowagie.filmfestival.Movie;
  21. import com.lowagie.filmfestival.PojoFactory;
  22. import com.lowagie.filmfestival.PojoToElementFactory;
  23. import com.itextpdf.text.Chunk;
  24. import com.itextpdf.text.Document;
  25. import com.itextpdf.text.DocumentException;
  26. import com.itextpdf.text.Element;
  27. import com.itextpdf.text.PageSize;
  28. import com.itextpdf.text.Paragraph;
  29. import com.itextpdf.text.pdf.ColumnText;
  30. import com.itextpdf.text.pdf.PdfWriter;
  31.  
  32. public class ColumnMovies2 {
  33.  
  34.     /** The resulting PDF file. */
  35.     public static final String RESULT = "results/part1/chapter03/column_movies2.pdf";
  36.    
  37.     /** Definition of two columns */
  38.     public static final float[][] COLUMNS = {
  39.         { 40, 36, 219, 579 } , { 234, 36, 414, 579 },
  40.         { 428, 36, 608, 579 } , { 622, 36, 802, 579 }
  41.     };
  42.    
  43.     /**
  44.      * Creates a PDF with information about the movies
  45.      * @param    filename the name of the PDF file that will be created.
  46.      * @throws    DocumentException
  47.      * @throws    IOException
  48.      * @throws    SQLException
  49.      */
  50.     public void createPdf(String filename)
  51.         throws IOException, DocumentException, SQLException {
  52.         // Create a database connection
  53.         DatabaseConnection connection = new HsqldbConnection("filmfestival");
  54.         // step 1
  55.         Document document = new Document(PageSize.A4.rotate());
  56.         // step 2
  57.         PdfWriter writer
  58.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  59.         // step 3
  60.         document.open();
  61.         // step 4
  62.         List movies = PojoFactory.getMovies(connection);
  63.         ColumnText ct = new ColumnText(writer.getDirectContent());
  64.         int column = 0;
  65.         ct.setSimpleColumn(
  66.             COLUMNS[column][0], COLUMNS[column][1],
  67.             COLUMNS[column][2], COLUMNS[column][3]);
  68.         int status = ColumnText.START_COLUMN;
  69.         float y;
  70.         for (Movie movie : movies) {
  71.             y = ct.getYLine();
  72.             addContent(ct, movie);
  73.             status = ct.go(true);
  74.             if (ColumnText.hasMoreText(status)) {
  75.                 column = (column + 1) % 4;
  76.                 if (column == 0)
  77.                     document.newPage();
  78.                 ct.setSimpleColumn(
  79.                     COLUMNS[column][0], COLUMNS[column][1],
  80.                     COLUMNS[column][2], COLUMNS[column][3]);
  81.                 y = COLUMNS[column][3];
  82.             }
  83.             ct.setYLine(y);
  84.             ct.setText(null);
  85.             addContent(ct, movie);
  86.             status = ct.go();
  87.         }
  88.         // step 5
  89.         document.close();
  90.         // Close the database connection
  91.         connection.close();
  92.     }
  93.    
  94.     /**
  95.      * Adds info about a movie in a ColumnText object
  96.      * @param ct A ColumnText object
  97.      * @param movie A Movie POJO
  98.      */
  99.     public void addContent(ColumnText ct, Movie movie) {
  100.         Paragraph p;
  101.         p = new Paragraph(new Paragraph(movie.getTitle(), FilmFonts.BOLD));
  102.         p.setAlignment(Element.ALIGN_CENTER);
  103.         p.setSpacingBefore(16);
  104.         ct.addElement(p);
  105.         if (movie.getOriginalTitle() != null) {
  106.             p = new Paragraph(movie.getOriginalTitle(), FilmFonts.ITALIC);
  107.             p.setAlignment(Element.ALIGN_RIGHT);
  108.             ct.addElement(p);
  109.         }
  110.         p = new Paragraph();
  111.         p.add(PojoToElementFactory.getYearPhrase(movie));
  112.         p.add(" ");
  113.         p.add(PojoToElementFactory.getDurationPhrase(movie));
  114.         p.setAlignment(Element.ALIGN_JUSTIFIED_ALL);
  115.         ct.addElement(p);
  116.         p = new Paragraph(new Chunk(new StarSeparator()));
  117.         p.setSpacingAfter(12);
  118.         ct.addElement(p);
  119.     }
  120.  
  121.     /**
  122.      * Main method.
  123.      *
  124.      * @param    args    no arguments needed
  125.      * @throws DocumentException
  126.      * @throws IOException
  127.      * @throws SQLException
  128.      */
  129.     public static void main(String[] args) throws IOException, DocumentException, SQLException {
  130.         new ColumnMovies2().createPdf(RESULT);
  131.     }
  132. }
ImageDirect.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Element;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Image;
  18. import com.itextpdf.text.PageSize;
  19. import com.itextpdf.text.Paragraph;
  20. import com.itextpdf.text.Font.FontFamily;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23. public class ImageDirect {
  24.  
  25.     /** The resulting PDF. */
  26.     public static final String RESULT
  27.         = "results/part1/chapter03/image_direct.pdf";
  28.     /** The movie poster. */
  29.     public static final String RESOURCE
  30.         = "resources/img/loa.jpg";
  31.    
  32.     public static void main(String[] args)
  33.         throws IOException, DocumentException {
  34.         // step 1
  35.         Document document
  36.             = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
  37.         // step 2
  38.         PdfWriter writer
  39.             = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  40.         writer.setCompressionLevel(0);
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         Image img = Image.getInstance(RESOURCE);
  45.         img.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
  46.                 (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
  47.         writer.getDirectContent().addImage(img);
  48.         Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
  49.         p.setAlignment(Element.ALIGN_CENTER);
  50.         document.add(p);
  51.         // step 5
  52.         document.close();
  53.     }
  54. }
ImageSkew.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class ImageSkew {
  20.  
  21.     /** The resulting PDF. */
  22.     public static final String RESULT = "results/part1/chapter03/image_skew.pdf";
  23.     /** The movie poster. */
  24.     public static final String RESOURCE = "resources/img/loa.jpg";
  25.  
  26.     /**
  27.      * Main method.
  28.      *
  29.      * @param    args    no arguments needed
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public static void main(String[] args)
  34.         throws IOException, DocumentException {
  35.         // step 1
  36.         Document document = new Document(PageSize.POSTCARD.rotate());
  37.         // step 2
  38.         PdfWriter writer = PdfWriter.getInstance(document,
  39.                 new FileOutputStream(RESULT));
  40.         writer.setCompressionLevel(0);
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         Image img = Image.getInstance(RESOURCE);
  45.         // Add the image to the upper layer
  46.         writer.getDirectContent().addImage(img,
  47.             img.getWidth(), 0, 0.35f * img.getHeight(),
  48.             0.65f * img.getHeight(), 30, 30);
  49.         // step 5
  50.         document.close();
  51.     }
  52. }
ImageInline.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class ImageInline {
  20.  
  21.     /** The resulting PDF. */
  22.     public static final String RESULT
  23.         = "results/part1/chapter03/image_inline.pdf";
  24.     /** The movie poster. */
  25.     public static final String RESOURCE
  26.         = "resources/img/loa.jpg";
  27.  
  28.     /**
  29.      * Main method.
  30.      *
  31.      * @param    args    no arguments needed
  32.      * @throws DocumentException
  33.      * @throws IOException
  34.      */
  35.     public static void main(String[] args)
  36.         throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
  39.         // step 2
  40.         PdfWriter writer = PdfWriter.getInstance(document,
  41.                 new FileOutputStream(RESULT));
  42.         writer.setCompressionLevel(0);
  43.         // step 3
  44.         document.open();
  45.         // step 4
  46.         Image img = Image.getInstance(RESOURCE);
  47.         img.setAbsolutePosition(
  48.             (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
  49.             (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
  50.         writer.getDirectContent().addImage(img, true);
  51.         // step 5
  52.         document.close();
  53.     }
  54. }
MoviePosters.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14.  
  15. import com.lowagie.database.DatabaseConnection;
  16. import com.lowagie.database.HsqldbConnection;
  17. import com.lowagie.filmfestival.Movie;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.itextpdf.text.Document;
  20. import com.itextpdf.text.DocumentException;
  21. import com.itextpdf.text.Image;
  22. import com.itextpdf.text.PageSize;
  23. import com.itextpdf.text.pdf.PdfContentByte;
  24. import com.itextpdf.text.pdf.PdfTemplate;
  25. import com.itextpdf.text.pdf.PdfWriter;
  26.  
  27. public class MoviePosters {
  28.     /** The resulting PDF file. */
  29.     public static final String RESULT
  30.         = "results/part1/chapter03/movie_posters.pdf";
  31.     /** Path to the resources. */
  32.     public static final String RESOURCE
  33.         = "resources/posters/%s.jpg";
  34.    
  35.     /**
  36.      * Creates a PDF with information about the movies
  37.      * @param    filename the name of the PDF file that will be created.
  38.      * @throws    DocumentException
  39.      * @throws    IOException
  40.      * @throws    SQLException
  41.      */
  42.     public void createPdf(String filename)
  43.         throws IOException, DocumentException, SQLException {
  44.         // Creates a database connection
  45.         DatabaseConnection connection = new HsqldbConnection("filmfestival");    
  46.         // step 1
  47.         Document document = new Document(PageSize.A4);
  48.         // step 2
  49.         PdfWriter writer
  50.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  51.         writer.setCompressionLevel(0);
  52.         // step 3
  53.         document.open();
  54.         // step 4
  55.         PdfContentByte canvas = writer.getDirectContent();
  56.         // Create the XObject
  57.         PdfTemplate celluloid = canvas.createTemplate(595, 84.2f);
  58.         celluloid.rectangle(8, 8, 579, 68);
  59.         for (float f = 8.25f; f < 581; f+= 6.5f) {
  60.             celluloid.roundRectangle(f, 8.5f, 6, 3, 1.5f);
  61.             celluloid.roundRectangle(f, 72.5f, 6, 3, 1.5f);
  62.         }
  63.         celluloid.setGrayFill(0.1f);
  64.         celluloid.eoFill();
  65.         // Write the XObject to the OutputStream
  66.         writer.releaseTemplate(celluloid);
  67.         // Add the XObject 10 times
  68.         for (int i = 0; i < 10; i++) {
  69.             canvas.addTemplate(celluloid, 0, i * 84.2f);
  70.         }
  71.         // Go to the next page
  72.         document.newPage();
  73.         // Add the XObject 10 times
  74.         for (int i = 0; i < 10; i++) {
  75.             canvas.addTemplate(celluloid, 0, i * 84.2f);
  76.         }
  77.         // Get the movies from the database
  78.         List movies = PojoFactory.getMovies(connection);
  79.         Image img;
  80.         float x = 11.5f;
  81.         float y = 769.7f;
  82.         // Loop over the movies and add images
  83.         for (Movie movie : movies) {
  84.             img = Image.getInstance(String.format(RESOURCE, movie.getImdb()));
  85.             img.scaleToFit(1000, 60);
  86.             img.setAbsolutePosition(x + (45 - img.getScaledWidth()) / 2, y);
  87.             canvas.addImage(img);
  88.             x += 48;
  89.             if (x > 578) {
  90.                 x = 11.5f;
  91.                 y -= 84.2f;
  92.             }
  93.         }
  94.         // Go to the next page
  95.         document.newPage();
  96.         // Add the template using a different CTM
  97.         canvas.addTemplate(celluloid, 0.8f, 0, 0.35f, 0.65f, 0, 600);
  98.         // Wrap the XObject in an Image object
  99.         Image tmpImage = Image.getInstance(celluloid);
  100.         tmpImage.setAbsolutePosition(0, 480);
  101.         document.add(tmpImage);
  102.         // Perform transformations on the image
  103.         tmpImage.setRotationDegrees(30);
  104.         tmpImage.scalePercent(80);
  105.         tmpImage.setAbsolutePosition(30, 500);
  106.         document.add(tmpImage);
  107.         // More transformations
  108.         tmpImage.setRotation((float)Math.PI / 2);
  109.         tmpImage.setAbsolutePosition(200, 300);
  110.         document.add(tmpImage);
  111.         // step 5
  112.         document.close();
  113.         // Close the database connection
  114.         connection.close();
  115.     }
  116.  
  117.     /**
  118.      * Main method.
  119.      *
  120.      * @param    args    no arguments needed
  121.      * @throws DocumentException
  122.      * @throws IOException
  123.      * @throws SQLException
  124.      */
  125.     public static void main(String[] args)
  126.         throws IOException, SQLException, DocumentException {
  127.         new MoviePosters().createPdf(RESULT);
  128.     }
  129. }
MovieTemplates.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter03;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.sql.Date;
  13. import java.sql.SQLException;
  14. import java.util.List;
  15.  
  16. import com.lowagie.database.DatabaseConnection;
  17. import com.lowagie.database.HsqldbConnection;
  18. import com.lowagie.filmfestival.PojoFactory;
  19. import com.lowagie.filmfestival.Screening;
  20. import com.itextpdf.text.Document;
  21. import com.itextpdf.text.DocumentException;
  22. import com.itextpdf.text.PageSize;
  23. import com.itextpdf.text.Paragraph;
  24. import com.itextpdf.text.pdf.PdfContentByte;
  25. import com.itextpdf.text.pdf.PdfTemplate;
  26. import com.itextpdf.text.pdf.PdfWriter;
  27.  
  28. /**
  29.  * Draws a time table to the direct content using lines and simple shapes,
  30.  * adding blocks representing a movies.
  31.  */
  32. public class MovieTemplates extends MovieCalendar {
  33.  
  34.     /** The resulting PDF. */
  35.     public static final String RESULT
  36.         = "results/part1/chapter03/movie_templates.pdf";
  37.  
  38.     /**
  39.      * Creates a PDF document.
  40.      * @param filename the path to the new PDF document
  41.      * @throws    DocumentException
  42.      * @throws    IOException
  43.      */
  44.     public void createPdf(String filename)
  45.         throws IOException, DocumentException {
  46.         // step 1
  47.         Document document = new Document(PageSize.A4.rotate());
  48.         // step 2
  49.         PdfWriter writer
  50.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  51.         // step 3
  52.         document.open();
  53.         // step 4
  54.         PdfContentByte over = writer.getDirectContent();
  55.         PdfContentByte under = writer.getDirectContentUnder();
  56.         try {
  57.             DatabaseConnection connection = new HsqldbConnection("filmfestival");
  58.             locations = PojoFactory.getLocations(connection);
  59.             PdfTemplate t_under = under.createTemplate(
  60.                     PageSize.A4.getHeight(), PageSize.A4.getWidth());
  61.             drawTimeTable(t_under);
  62.             PdfTemplate t_over = over.createTemplate(
  63.                     PageSize.A4.getHeight(), PageSize.A4.getWidth());
  64.             drawTimeSlots(t_over);
  65.             drawInfo(t_over);
  66.             List days = PojoFactory.getDays(connection);
  67.             List screenings;
  68.             int d = 1;
  69.             for (Date day : days) {
  70.                 over.addTemplate(t_over, 0, 0);
  71.                 under.addTemplate(t_under, 0, 0);
  72.                 drawDateInfo(day, d++, over);
  73.                 screenings = PojoFactory.getScreenings(connection, day);
  74.                 for (Screening screening : screenings) {
  75.                     drawBlock(screening, under, over);
  76.                     drawMovieInfo(screening, over);
  77.                 }
  78.                 document.newPage();
  79.             }
  80.             connection.close();
  81.         }
  82.         catch(SQLException sqle) {
  83.             sqle.printStackTrace();
  84.             document.add(new Paragraph("Database error: " + sqle.getMessage()));
  85.         }
  86.         // step 5
  87.         document.close();
  88.     }
  89.    
  90.     /**
  91.      * Constructs a MovieTemplates object.
  92.      * @throws DocumentException
  93.      * @throws IOException
  94.      */
  95.     public MovieTemplates() throws DocumentException, IOException {
  96.         super();
  97.     }
  98.    
  99.     /**
  100.      * Main method creating the PDF.
  101.      * @param    args    no arguments needed
  102.      * @throws DocumentException
  103.      * @throws IOException
  104.      */
  105.     public static void main(String[] args)
  106.         throws IOException, DocumentException {
  107.         new MovieTemplates().createPdf(RESULT);
  108.     }
  109. }
FestivalOpening.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class FestivalOpening : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       string RESOURCE = Utility.ResourcePosters;
  18.       // step 1
  19.       using (Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30)) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         // Create and add a Paragraph
  26.         Paragraph p = new Paragraph(
  27.           "Foobar Film Festival",
  28.           new Font(Font.FontFamily.HELVETICA, 22)
  29.         );
  30.         p.Alignment = Element.ALIGN_CENTER;
  31.         document.Add(p);
  32.         // Create and add an Image
  33.         Image img = Image.GetInstance(Path.Combine(
  34.           Utility.ResourceImage, "loa.jpg"
  35.         ));
  36.         img.SetAbsolutePosition(
  37.           (PageSize.POSTCARD.Width - img.ScaledWidth) / 2,
  38.           (PageSize.POSTCARD.Height - img.ScaledHeight) / 2
  39.         );
  40.         document.Add(img);
  41.         // Now we go to the next page
  42.         document.NewPage();
  43.         document.Add(p);
  44.         document.Add(img);
  45.         // Add text on top of the image
  46.         PdfContentByte over = writer.DirectContent;
  47.         over.SaveState();
  48.         float sinus = (float)Math.Sin(Math.PI / 60);
  49.         float cosinus = (float)Math.Cos(Math.PI / 60);
  50.         BaseFont bf = BaseFont.CreateFont();
  51.         over.BeginText();
  52.         over.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
  53.         over.SetLineWidth(1.5f);
  54.         over.SetRGBColorStroke(0xFF, 0x00, 0x00);
  55.         over.SetRGBColorFill(0xFF, 0xFF, 0xFF);
  56.         over.SetFontAndSize(bf, 36);
  57.         over.SetTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324);
  58.         over.ShowText("SOLD OUT");
  59.         over.EndText();
  60.         over.RestoreState();
  61.         // Add a rectangle under the image
  62.         PdfContentByte under = writer.DirectContentUnder;
  63.         under.SaveState();
  64.         under.SetRGBColorFill(0xFF, 0xD7, 0x00);
  65.         under.Rectangle(5, 5,
  66.           PageSize.POSTCARD.Width - 10,
  67.           PageSize.POSTCARD.Height - 10
  68.         );
  69.         under.Fill();
  70.         under.RestoreState();        
  71.       }
  72.     }
  73. // ===========================================================================
  74.   }
  75. }
GraphicsStateStack.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class GraphicsStateStack : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       // step 1
  18.       using (Document document = new Document(new Rectangle(200, 120))) {
  19.         // step 2
  20.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  21.         // step 3
  22.         document.Open();
  23.         // step 4
  24.         PdfContentByte canvas = writer.DirectContent;
  25.         // state 1:
  26.         canvas.SetRGBColorFill(0xFF, 0x45, 0x00);
  27.         // fill a rectangle in state 1
  28.         canvas.Rectangle(10, 10, 60, 60);
  29.         canvas.Fill();
  30.         canvas.SaveState();
  31.         // state 2;
  32.         canvas.SetLineWidth(3);
  33.         canvas.SetRGBColorFill(0x8B, 0x00, 0x00);
  34.         // fill and stroke a rectangle in state 2
  35.         canvas.Rectangle(40, 20, 60, 60);
  36.         canvas.FillStroke();
  37.         canvas.SaveState();
  38.         // state 3:
  39.         canvas.ConcatCTM(1, 0, 0.1f, 1, 0, 0);
  40.         canvas.SetRGBColorStroke(0xFF, 0x45, 0x00);
  41.         canvas.SetRGBColorFill(0xFF, 0xD7, 0x00);
  42.         // fill and stroke a rectangle in state 3
  43.         canvas.Rectangle(70, 30, 60, 60);
  44.         canvas.FillStroke();
  45.         canvas.RestoreState();
  46.         // stroke a rectangle in state 2
  47.         canvas.Rectangle(100, 40, 60, 60);
  48.         canvas.Stroke();
  49.         canvas.RestoreState();
  50.         // fill and stroke a rectangle in state 1
  51.         canvas.Rectangle(130, 50, 60, 60);
  52.         canvas.FillStroke();        
  53.       }
  54.     }
  55. // ===========================================================================
  56.   }
  57. }
MovieTimeTable.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class MovieTimeTable : IWriter {
  15. // ===========================================================================
  16.     public virtual void Write(Stream stream) {
  17.       // step 1
  18.       using (Document document = new Document(PageSize.A4.Rotate())) {
  19.         // step 2
  20.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  21.         // step 3
  22.         document.Open();
  23.         // step 4
  24.         DrawTimeTable(writer.DirectContentUnder);
  25.         DrawTimeSlots(writer.DirectContent);
  26.       }
  27.     }
  28. // ---------------------------------------------------------------------------    
  29.     /** The number of locations on our time table. */
  30.     public const int LOCATIONS = 9;
  31.     /** The number of time slots on our time table. */
  32.     public const int TIMESLOTS = 32;
  33.    
  34.     /** The offset to the left of our time table. */
  35.     public const float OFFSET_LEFT = 76;
  36.     /** The width of our time table. */
  37.     public const float WIDTH = 740;
  38.     /** The offset from the bottom of our time table. */
  39.     public const float OFFSET_BOTTOM = 36;
  40.     /** The height of our time table */
  41.     public const float HEIGHT = 504;
  42.    
  43.     /** The offset of the location bar next to our time table. */
  44.     public const float OFFSET_LOCATION = 26;
  45.     /** The width of the location bar next to our time table. */
  46.     public const float WIDTH_LOCATION = 48;
  47.    
  48.     /** The height of a bar showing the movies at one specific location. */
  49.     public static float HEIGHT_LOCATION = HEIGHT / LOCATIONS;
  50.     /** The width of a time slot. */
  51.     public static float WIDTH_TIMESLOT = WIDTH / TIMESLOTS;
  52. // ---------------------------------------------------------------------------    
  53.     /**
  54.      * Draws the time table for a day at the film festival.
  55.      * @param directcontent a canvas to which the time table has to be drawn.
  56.      */
  57.     protected void DrawTimeTable(PdfContentByte directcontent) {        
  58.         directcontent.SaveState();
  59.         directcontent.SetLineWidth(1.2f);
  60.         float llx, lly, urx, ury;
  61.        
  62.         llx = OFFSET_LEFT;
  63.         lly = OFFSET_BOTTOM;
  64.         urx = OFFSET_LEFT + WIDTH;
  65.         ury = OFFSET_BOTTOM + HEIGHT;
  66.         directcontent.MoveTo(llx, lly);
  67.         directcontent.LineTo(urx, lly);
  68.         directcontent.LineTo(urx, ury);
  69.         directcontent.LineTo(llx, ury);
  70.         directcontent.ClosePath();
  71.         directcontent.Stroke();
  72.        
  73.         llx = OFFSET_LOCATION;
  74.         lly = OFFSET_BOTTOM;
  75.         urx = OFFSET_LOCATION + WIDTH_LOCATION;
  76.         ury = OFFSET_BOTTOM + HEIGHT;
  77.         directcontent.MoveTo(llx, lly);
  78.         directcontent.LineTo(urx, lly);
  79.         directcontent.LineTo(urx, ury);
  80.         directcontent.LineTo(llx, ury);
  81.         directcontent.ClosePathStroke();
  82.        
  83.         directcontent.SetLineWidth(1);
  84.         directcontent.MoveTo(
  85.           OFFSET_LOCATION + WIDTH_LOCATION / 2, OFFSET_BOTTOM
  86.         );
  87.         directcontent.LineTo(
  88.           OFFSET_LOCATION + WIDTH_LOCATION / 2, OFFSET_BOTTOM + HEIGHT
  89.         );
  90.         float y;
  91.         for (int i = 1; i < LOCATIONS; i++) {
  92.           y = OFFSET_BOTTOM + (i * HEIGHT_LOCATION);
  93.           if (i == 2 || i == 6) {
  94.             directcontent.MoveTo(OFFSET_LOCATION, y);
  95.             directcontent.LineTo(OFFSET_LOCATION + WIDTH_LOCATION, y);
  96.           }
  97.           else {
  98.             directcontent.MoveTo(OFFSET_LOCATION + WIDTH_LOCATION / 2, y);
  99.             directcontent.LineTo(OFFSET_LOCATION + WIDTH_LOCATION, y);
  100.           }
  101.           directcontent.MoveTo(OFFSET_LEFT, y);
  102.           directcontent.LineTo(OFFSET_LEFT + WIDTH, y);
  103.         }
  104.         directcontent.Stroke();
  105.         directcontent.RestoreState();
  106.     }
  107. // ---------------------------------------------------------------------------    
  108.     /**
  109.      * Draws the time slots for a day at the film festival.
  110.      * @param directcontent the canvas to which the time table has to be drawn.
  111.      */
  112.     protected void DrawTimeSlots(PdfContentByte directcontent) {
  113.       directcontent.SaveState();
  114.       float x;
  115.       for (int i = 1; i < TIMESLOTS; i++) {
  116.         x = OFFSET_LEFT + (i * WIDTH_TIMESLOT);
  117.         directcontent.MoveTo(x, OFFSET_BOTTOM);
  118.         directcontent.LineTo(x, OFFSET_BOTTOM + HEIGHT);
  119.       }
  120.       directcontent.SetLineWidth(0.3f);
  121.       directcontent.SetColorStroke(BaseColor.GRAY);
  122.       directcontent.SetLineDash(3, 1);
  123.       directcontent.Stroke();
  124.       directcontent.RestoreState();
  125.     }    
  126. // ===========================================================================
  127.   }
  128. }
MovieTimeBlocks.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. using iTextSharp.text.html;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  16.   public class MovieTimeBlocks : MovieTimeTable {
  17. // ===========================================================================
  18.     /** A list containing all the locations. */
  19.     protected List locations;
  20. // ---------------------------------------------------------------------------        
  21.     public override void Write(Stream stream) {
  22.       // step 1
  23.       using (Document document = new Document(PageSize.A4.Rotate())) {
  24.         // step 2
  25.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  26.         writer.CompressionLevel = 0;
  27.         // step 3
  28.         document.Open();
  29.         // step 4
  30.         PdfContentByte over = writer.DirectContent;
  31.         PdfContentByte under = writer.DirectContentUnder;
  32.         locations = PojoFactory.GetLocations();
  33.         List days = PojoFactory.GetDays();
  34.         List screenings;
  35.         foreach (string day in days) {
  36.           DrawTimeTable(under);
  37.           DrawTimeSlots(over);
  38.           screenings = PojoFactory.GetScreenings(day);
  39.           foreach (Screening screening in screenings) {
  40.             DrawBlock(screening, under, over);
  41.           }
  42.           document.NewPage();
  43.         }
  44.       }
  45.     }
  46. // ---------------------------------------------------------------------------        
  47.     /**
  48.      * Draws a colored block on the time table, corresponding with
  49.      * the screening of a specific movie.
  50.      * @param    screening    a screening POJO, contains a movie and a category
  51.      * @param    under    the canvas to which the block is drawn
  52.      */
  53.     protected void DrawBlock(
  54.       Screening screening, PdfContentByte under, PdfContentByte over
  55.     ) {
  56.       under.SaveState();
  57.       BaseColor color = WebColors.GetRGBColor(
  58.         "#" + screening.movie.entry.category.color
  59.       );
  60.       under.SetColorFill(color);
  61.       Rectangle rect = GetPosition(screening);
  62.       under.Rectangle(
  63.         rect.Left, rect.Bottom, rect.Width, rect.Height
  64.       );
  65.       under.Fill();
  66.       over.Rectangle(
  67.         rect.Left, rect.Bottom, rect.Width, rect.Height
  68.       );
  69.       over.Stroke();
  70.       under.RestoreState();
  71.     }
  72. // ---------------------------------------------------------------------------        
  73.     /** The "offset time" for our calendar sheets. */
  74.     // public static long TIME930 = 30600000L;
  75.     public const long TIME930 = 34200000L;
  76.    
  77.     /** The width of one minute. */
  78.     public readonly float MINUTE = WIDTH_TIMESLOT / 30f;
  79. // ---------------------------------------------------------------------------        
  80.     /**
  81.      * Calculates the position of a rectangle corresponding with a screening.
  82.      * @param    screening    a screening POJO, contains a movie
  83.      * @return    a Rectangle
  84.      */
  85.     protected Rectangle GetPosition(Screening screening) {
  86.       float llx, lly, urx, ury;
  87.       // long minutesAfter930 = (screening.getTime().getTime() - TIME930) / 60000L;
  88.       long minutesAfter930 = (
  89.         Utility.GetMilliseconds(screening.Time) - TIME930
  90.       ) / 60000L;
  91.       llx = OFFSET_LEFT + (MINUTE * minutesAfter930);
  92.       int location = locations.IndexOf(screening.Location) + 1;
  93.       lly = OFFSET_BOTTOM + (LOCATIONS - location) * HEIGHT_LOCATION;
  94.       urx = llx + MINUTE * screening.movie.Duration;
  95.       ury = lly + HEIGHT_LOCATION;
  96.       return new Rectangle(llx, lly, urx, ury);
  97.     }
  98. // ===========================================================================
  99.   }
  100. }
FoobarFilmFestival.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class FoobarFilmFestival : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       string RESOURCE = Utility.ResourcePosters;
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         Chunk c;
  26.         String foobar = "Foobar Film Festival";
  27.         // Measuring a String in Helvetica
  28.         Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
  29.         BaseFont bf_helv = helvetica.GetCalculatedBaseFont(false);
  30.         float width_helv = bf_helv.GetWidthPoint(foobar, 12);
  31.         c = new Chunk(foobar + ": " + width_helv, helvetica);
  32.         document.Add(new Paragraph(c));
  33.         document.Add(new Paragraph(string.Format(
  34.           "Chunk width: {0}", c.GetWidthPoint()
  35.         )));
  36.         // Measuring a String in Times
  37.         BaseFont bf_times = BaseFont.CreateFont(
  38.           "c:/windows/fonts/times.ttf",
  39.           BaseFont.WINANSI, BaseFont.EMBEDDED
  40.         );
  41.         Font times = new Font(bf_times, 12);
  42.         float width_times = bf_times.GetWidthPoint(foobar, 12);
  43.         c = new Chunk(foobar + ": " + width_times, times);
  44.         document.Add(new Paragraph(c));
  45.         document.Add(new Paragraph(String.Format(
  46.           "Chunk width: {0}", c.GetWidthPoint()
  47.         )));
  48.         document.Add(Chunk.NEWLINE);
  49.         // Ascent and descent of the String
  50.         document.Add(new Paragraph(
  51.           "Ascent Helvetica: " + bf_helv.GetAscentPoint(foobar, 12)
  52.         ));
  53.         document.Add(new Paragraph(
  54.           "Ascent Times: " + bf_times.GetAscentPoint(foobar, 12)
  55.         ));
  56.         document.Add(new Paragraph(
  57.           "Descent Helvetica: " + bf_helv.GetDescentPoint(foobar, 12)
  58.         ));
  59.         document.Add(new Paragraph(
  60.           "Descent Times: " + bf_times.GetDescentPoint(foobar, 12)
  61.         ));
  62.         document.Add(Chunk.NEWLINE);
  63.         // Kerned text
  64.         width_helv = bf_helv.GetWidthPointKerned(foobar, 12);
  65.         c = new Chunk(foobar + ": " + width_helv, helvetica);
  66.         document.Add(new Paragraph(c));
  67.         // Drawing lines to see where the text is added
  68.         PdfContentByte canvas = writer.DirectContent;
  69.         canvas.SaveState();
  70.         canvas.SetLineWidth(0.05f);
  71.         canvas.MoveTo(400, 806);
  72.         canvas.LineTo(400, 626);
  73.         canvas.MoveTo(508.7f, 806);
  74.         canvas.LineTo(508.7f, 626);
  75.         canvas.MoveTo(280, 788);
  76.         canvas.LineTo(520, 788);
  77.         canvas.MoveTo(280, 752);
  78.         canvas.LineTo(520, 752);
  79.         canvas.MoveTo(280, 716);
  80.         canvas.LineTo(520, 716);
  81.         canvas.MoveTo(280, 680);
  82.         canvas.LineTo(520, 680);
  83.         canvas.MoveTo(280, 644);
  84.         canvas.LineTo(520, 644);
  85.         canvas.Stroke();
  86.         canvas.RestoreState();
  87.         // Adding text with PdfContentByte.ShowTextAligned()
  88.         canvas.BeginText();
  89.         canvas.SetFontAndSize(bf_helv, 12);
  90.         canvas.ShowTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
  91.         canvas.ShowTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
  92.         canvas.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
  93.         canvas.ShowTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
  94.         canvas.ShowTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
  95.         canvas.EndText();
  96.         // More lines to see where the text is added
  97.         canvas.SaveState();
  98.         canvas.SetLineWidth(0.05f);
  99.         canvas.MoveTo(200, 590);
  100.         canvas.LineTo(200, 410);
  101.         canvas.MoveTo(400, 590);
  102.         canvas.LineTo(400, 410);
  103.         canvas.MoveTo(80, 572);
  104.         canvas.LineTo(520, 572);
  105.         canvas.MoveTo(80, 536);
  106.         canvas.LineTo(520, 536);
  107.         canvas.MoveTo(80, 500);
  108.         canvas.LineTo(520, 500);
  109.         canvas.MoveTo(80, 464);
  110.         canvas.LineTo(520, 464);
  111.         canvas.MoveTo(80, 428);
  112.         canvas.LineTo(520, 428);
  113.         canvas.Stroke();
  114.         canvas.RestoreState();
  115.         // Adding text with ColumnText.ShowTextAligned()
  116.         Phrase phrase = new Phrase(foobar, times);
  117.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
  118.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
  119.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
  120.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
  121.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
  122.         // Chunk attributes
  123.         c = new Chunk(foobar, times);
  124.         c.SetHorizontalScaling(0.5f);
  125.         phrase = new Phrase(c);
  126.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
  127.         c = new Chunk(foobar, times);
  128.         c.SetSkew(15, 15);
  129.         phrase = new Phrase(c);
  130.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
  131.         c = new Chunk(foobar, times);
  132.         c.SetSkew(0, 25);
  133.         phrase = new Phrase(c);
  134.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
  135.         c = new Chunk(foobar, times);
  136.         c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
  137.         phrase = new Phrase(c);
  138.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
  139.         c = new Chunk(foobar, times);
  140.         c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
  141.         phrase = new Phrase(c);
  142.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);        
  143.       }
  144.     }
  145. // ===========================================================================
  146.   }
  147. }
MovieTextInfo.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. using iTextSharp.text.html;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. /**
  16.  * Draws a time table to the direct content using lines and simple shapes,
  17.  * adding blocks representing a movies.
  18.  */
  19. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  20.   public class MovieTextInfo : MovieTimeBlocks {
  21. // ===========================================================================
  22.     public override void Write(Stream stream) {
  23.       // step 1
  24.       using (Document document = new Document(PageSize.A4.Rotate())) {
  25.         // step 2
  26.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  27.         // step 3
  28.         document.Open();
  29.         // step 4
  30.         PdfContentByte over = writer.DirectContent;
  31.         PdfContentByte under = writer.DirectContentUnder;
  32.         locations = PojoFactory.GetLocations();
  33.         List days = PojoFactory.GetDays();
  34.         List screenings;
  35.         int d = 1;
  36.         foreach (string day in days) {
  37.           DrawTimeTable(under);
  38.           DrawTimeSlots(over);
  39.           DrawInfo(over);
  40.           DrawDateInfo(day, d++, over);
  41.           screenings = PojoFactory.GetScreenings(day);
  42.           foreach (Screening screening in screenings) {
  43.             DrawBlock(screening, under, over);
  44.             DrawMovieInfo(screening, over);
  45.           }
  46.           document.NewPage();
  47.         }
  48.       }
  49.     }
  50. // ---------------------------------------------------------------------------        
  51.     /** The base font that will be used to write info on the calendar sheet. */
  52.     protected BaseFont bf;
  53.    
  54.     /** A phrase containing a white letter "P" (for Press) */
  55.     protected Phrase press;
  56.  
  57.     /** The different time slots. */
  58.     public readonly String[] TIME = {
  59.       "09:30", "10:00", "10:30", "11:00", "11:30", "12:00",
  60.       "00:30", "01:00", "01:30", "02:00", "02:30", "03:00",
  61.       "03:30", "04:00", "04:30", "05:00", "05:30", "06:00",
  62.       "06:30", "07:00", "07:30", "08:00", "08:30", "09:00",
  63.       "09:30", "10:00", "10:30", "11:00", "11:30", "12:00",
  64.       "00:30", "01:00"
  65.     };
  66. // ---------------------------------------------------------------------------          
  67.     /**
  68.      * Draws some text on every calendar sheet.
  69.      *
  70.      */
  71.     protected void DrawInfo(PdfContentByte directcontent) {
  72.       directcontent.BeginText();
  73.       directcontent.SetFontAndSize(bf, 18);
  74.       float x, y;
  75.       x = (OFFSET_LEFT + WIDTH + OFFSET_LOCATION) / 2;
  76.       y = OFFSET_BOTTOM + HEIGHT + 24;
  77.       directcontent.ShowTextAligned(
  78.         Element.ALIGN_CENTER,
  79.         "FOOBAR FILM FESTIVAL", x, y, 0
  80.       );
  81.       x = OFFSET_LOCATION + WIDTH_LOCATION / 2f - 6;
  82.       y = OFFSET_BOTTOM + HEIGHT_LOCATION;
  83.       directcontent.ShowTextAligned(
  84.         Element.ALIGN_CENTER,
  85.         "The Majestic", x, y, 90
  86.       );
  87.       y = OFFSET_BOTTOM + HEIGHT_LOCATION * 4f;
  88.       directcontent.ShowTextAligned(
  89.         Element.ALIGN_CENTER,
  90.         "Googolplex", x, y, 90
  91.       );
  92.       y = OFFSET_BOTTOM + HEIGHT_LOCATION * 7.5f;
  93.       directcontent.ShowTextAligned(
  94.         Element.ALIGN_CENTER,
  95.         "Cinema Paradiso", x, y, 90
  96.       );
  97.       directcontent.SetFontAndSize(bf, 12);
  98.       x = OFFSET_LOCATION + WIDTH_LOCATION - 6;
  99.       for (int i = 0; i < LOCATIONS; i++) {
  100.         y = OFFSET_BOTTOM + ((8.5f - i) * HEIGHT_LOCATION);
  101.         directcontent.ShowTextAligned(
  102.           Element.ALIGN_CENTER,
  103.           locations[i], x, y, 90
  104.         );
  105.       }
  106.       directcontent.SetFontAndSize(bf, 6);
  107.       y = OFFSET_BOTTOM + HEIGHT + 1;
  108.       for (int i = 0; i < TIMESLOTS; i++) {
  109.         x = OFFSET_LEFT + (i * WIDTH_TIMESLOT);
  110.         directcontent.ShowTextAligned(
  111.           Element.ALIGN_LEFT,
  112.           TIME[i], x, y, 45
  113.         );
  114.       }
  115.       directcontent.EndText();
  116.     }
  117. // ---------------------------------------------------------------------------            
  118.     /**
  119.      * Draws some text on every calendar sheet.
  120.      *
  121.      */
  122.     protected void DrawDateInfo(string day, int d, PdfContentByte directcontent) {
  123.       directcontent.BeginText();
  124.       directcontent.SetFontAndSize(bf, 18);
  125.       float x, y;
  126.       x = OFFSET_LOCATION;
  127.       y = OFFSET_BOTTOM + HEIGHT + 24;
  128.       directcontent.ShowTextAligned(
  129.         Element.ALIGN_LEFT,
  130.         "Day " + d, x, y, 0
  131.       );
  132.       x = OFFSET_LEFT + WIDTH;
  133.       directcontent.ShowTextAligned(
  134.         Element.ALIGN_RIGHT,
  135.         day, x, y, 0
  136.       );
  137.       directcontent.EndText();
  138.     }
  139. // ---------------------------------------------------------------------------            
  140.     /**
  141.      * Draws the info about the movie.
  142.      * @throws DocumentException
  143.      */
  144.     protected virtual void DrawMovieInfo(
  145.       Screening screening, PdfContentByte directcontent
  146.     ) {
  147.       // if (true) {
  148.       if (screening.Press) {
  149.         Rectangle rect = GetPosition(screening);
  150.         ColumnText.ShowTextAligned(
  151.           directcontent,
  152.           Element.ALIGN_CENTER,
  153.           press, (rect.Left + rect.Right) / 2,
  154.           rect.Bottom + rect.Height / 4, 0
  155.         );
  156.       }
  157.     }
  158. // ---------------------------------------------------------------------------            
  159.     /**
  160.      * Constructor for the MovieCalendar class; initializes the base font object.
  161.      * @throws IOException
  162.      * @throws DocumentException
  163.      */
  164.     public MovieTextInfo() {
  165.       bf = BaseFont.CreateFont();
  166.       Font f = new Font(bf, HEIGHT_LOCATION / 2);
  167.       f.Color = BaseColor.WHITE;
  168.       press = new Phrase("P", f);
  169.     }
  170. // ===========================================================================
  171.   }
  172. }
MovieCalendar.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. using iTextSharp.text.html;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  16.   public class MovieCalendar : MovieTextInfo {
  17. // ===========================================================================
  18.     /**
  19.      * Draws the info about the movie.
  20.      * @throws DocumentException
  21.      */
  22.     protected override void DrawMovieInfo(
  23.       Screening screening, PdfContentByte directcontent
  24.     ) {
  25.       base.DrawMovieInfo(screening, directcontent);
  26.       Rectangle rect = GetPosition(screening);
  27.       ColumnText column = new ColumnText(directcontent);
  28.       column.SetSimpleColumn(
  29.         new Phrase(screening.movie.Title),
  30.         rect.Left, rect.Bottom,
  31.         rect.Right, rect.Top, 18, Element.ALIGN_CENTER
  32.       );
  33.       column.Go();
  34.     }
  35. // ---------------------------------------------------------------------------            
  36.     /**
  37.      * Constructor for the MovieCalendar class; initializes the base font object.
  38.      * @throws IOException
  39.      * @throws DocumentException
  40.      */
  41.     public MovieCalendar() : base() {  }
  42. // ===========================================================================
  43.   }
  44. }
MovieColumns1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using iTextSharp.text.pdf.draw;
  14. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  17.   public class MovieColumns1 : IWriter {
  18. // ===========================================================================
  19.     public virtual void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         IEnumerable movies = PojoFactory.GetMovies();
  28.         ColumnText ct = new ColumnText(writer.DirectContent);
  29.         foreach (Movie movie in movies) {
  30.           ct.AddText(CreateMovieInformation(movie));
  31.           ct.AddText(Chunk.NEWLINE);
  32.         }
  33.         ct.Alignment = Element.ALIGN_JUSTIFIED;
  34.         ct.ExtraParagraphSpace = 6;
  35.         ct.SetLeading(0, 1.2f);
  36.         ct.FollowingIndent = 27;
  37.         int linesWritten = 0;
  38.         int column = 0;
  39.         // iText-ONLY, 'Initial value of the status' => 0
  40.         // iTextSharp **DOES NOT** include this member variable
  41.         // int status = ColumnText.START_COLUMN;
  42.         int status = 0;
  43.         while (ColumnText.HasMoreText(status)) {
  44.           ct.SetSimpleColumn(
  45.             COLUMNS[column][0], COLUMNS[column][1],
  46.             COLUMNS[column][2], COLUMNS[column][3]
  47.           );
  48.           ct.YLine = COLUMNS[column][3];
  49.           status = ct.Go();
  50.           linesWritten += ct.LinesWritten;
  51.           column = Math.Abs(column - 1);
  52.           if (column == 0) document.NewPage();
  53.         }
  54.        
  55.         ct.AddText(new Phrase("Lines written: " + linesWritten));
  56.         ct.Go();
  57.       }
  58.     }
  59. // ---------------------------------------------------------------------------    
  60.     /** Definition of two columns */
  61.     public readonly float[][] COLUMNS = {
  62.       new float[] { 36, 36, 296, 806 },
  63.       new float[] { 299, 36, 559, 806 }
  64.     };
  65. // ---------------------------------------------------------------------------    
  66.     /**
  67.      * Creates a Phrase containing information about a movie.
  68.      * @param    movie    the movie for which you want to create a Paragraph
  69.      */
  70.     public Phrase CreateMovieInformation(Movie movie) {
  71.       Phrase p = new Phrase();
  72.       p.Font = FilmFonts.NORMAL;
  73.       p.Add(new Phrase("Title: ", FilmFonts.BOLDITALIC));
  74.       p.Add(PojoToElementFactory.GetMovieTitlePhrase(movie));
  75.       p.Add(" ");
  76.       if (!string.IsNullOrEmpty(movie.OriginalTitle)) {
  77.         p.Add(new Phrase("Original title: ", FilmFonts.BOLDITALIC));
  78.         p.Add(PojoToElementFactory.GetOriginalTitlePhrase(movie));
  79.         p.Add(" ");
  80.       }
  81.       p.Add(new Phrase("Country: ", FilmFonts.BOLDITALIC));
  82.       foreach (Country country in movie.Countries) {
  83.         p.Add(PojoToElementFactory.GetCountryPhrase(country));
  84.         p.Add(" ");
  85.       }
  86.       p.Add(new Phrase("Director: ", FilmFonts.BOLDITALIC));
  87.       foreach (Director director in movie.Directors) {
  88.         p.Add(PojoToElementFactory.GetDirectorPhrase(director));
  89.         p.Add(" ");
  90.       }
  91.       p.Add(new Chunk("Year: ", FilmFonts.BOLDITALIC));
  92.       p.Add(new Chunk(movie.Year.ToString(), FilmFonts.NORMAL));
  93.       p.Add(new Chunk(" Duration: ", FilmFonts.BOLDITALIC));
  94.       p.Add(new Chunk(movie.Duration.ToString(), FilmFonts.NORMAL));
  95.       p.Add(new Chunk(" minutes", FilmFonts.NORMAL));
  96.       p.Add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2));
  97.       return p;
  98.     }    
  99. // ===========================================================================
  100.   }
  101. }
MovieColumns2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using iTextSharp.text.pdf.draw;
  14. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  17.   public class MovieColumns2 : MovieColumns1 {
  18. // ===========================================================================
  19.     public override void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         ColumnText ct = new ColumnText(writer.DirectContent) {
  28.           Alignment = Element.ALIGN_JUSTIFIED, ExtraParagraphSpace = 6,
  29.           Leading = 14, Indent = 10, RightIndent = 3,
  30.           SpaceCharRatio = PdfWriter.NO_SPACE_CHAR_RATIO
  31.         };
  32.         int column = 0;
  33.         // iText-ONLY, 'Initial value of the status' => 0
  34.         // iTextSharp **DOES NOT** include this member variable
  35.         // int status = ColumnText.START_COLUMN;
  36.         int status = 0;
  37.         ct.SetSimpleColumn(
  38.           COLUMNS[column][0], COLUMNS[column][1],
  39.           COLUMNS[column][2], COLUMNS[column][3]
  40.         );
  41.         IEnumerable movies = PojoFactory.GetMovies();
  42.         foreach (Movie movie in movies) {
  43.           ct.AddText(CreateMovieInformation(movie));
  44.           status = ct.Go();
  45.           if (ColumnText.HasMoreText(status)) {
  46.             column = Math.Abs(column - 1);
  47.             if (column == 0) {
  48.               document.NewPage();
  49.             }
  50.             ct.SetSimpleColumn(
  51.               COLUMNS[column][0], COLUMNS[column][1],
  52.               COLUMNS[column][2], COLUMNS[column][3]
  53.             );
  54.             ct.YLine = COLUMNS[column][3];
  55.             status = ct.Go();
  56.           }
  57.         }
  58.       }
  59.     }
  60. // ===========================================================================
  61.   }
  62. }
MovieColumns3.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using iTextSharp.text.pdf.draw;
  14. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  17.   public class MovieColumns3 : MovieColumns1 {
  18. // ===========================================================================
  19.     public override void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         ColumnText ct = new ColumnText(writer.DirectContent);
  28.         int column = 0;
  29.         ct.SetSimpleColumn(
  30.           COLUMNS[column][0], COLUMNS[column][1],
  31.           COLUMNS[column][2], COLUMNS[column][3]
  32.         );
  33.         // iText-ONLY, 'Initial value of the status' => 0
  34.         // iTextSharp **DOES NOT** include this member variable
  35.         // int status = ColumnText.START_COLUMN;
  36.         int status = 0;
  37.         Phrase p;
  38.         float y;        
  39.         IEnumerable movies = PojoFactory.GetMovies();
  40.         foreach (Movie movie in movies) {
  41.           y = ct.YLine;
  42.           p = CreateMovieInformation(movie);        
  43.           ct.AddText(p);
  44.           status = ct.Go(true);
  45.           if (ColumnText.HasMoreText(status)) {
  46.             column = Math.Abs(column - 1);
  47.             if (column == 0) {
  48.               document.NewPage();
  49.             }
  50.             ct.SetSimpleColumn(
  51.               COLUMNS[column][0], COLUMNS[column][1],
  52.               COLUMNS[column][2], COLUMNS[column][3]
  53.             );
  54.             y = COLUMNS[column][3];
  55.           }
  56.           ct.YLine = y;
  57.           ct.SetText(p);
  58.           status = ct.Go(false);
  59.         }
  60.       }
  61.     }
  62. // ===========================================================================
  63.   }
  64. }
MovieColumns4.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using iTextSharp.text.pdf.draw;
  14. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  17.   public class MovieColumns4 : MovieColumns1 {
  18. // ===========================================================================
  19.     public override void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte canvas = writer.DirectContent;
  28.         DrawRectangles(canvas);
  29.         ColumnText ct = new ColumnText(canvas);
  30.         ct.Alignment = Element.ALIGN_JUSTIFIED;
  31.         ct.Leading = 14;
  32.         int column = 0;
  33.         ct.SetColumns(
  34.           new float[] { 36,806, 36,670, 108,670, 108,596, 36,596, 36,36 },
  35.           new float[] { 296,806, 296,484, 259,484, 259,410, 296,410, 296,36 }
  36.         );
  37.         ct.SetColumns(LEFT[column], RIGHT[column] );
  38.         // ct.SetColumns(LEFT[column], RIGHT[column]);
  39.         // iText-ONLY, 'Initial value of the status' => 0
  40.         // iTextSharp **DOES NOT** include this member variable
  41.         // int status = ColumnText.START_COLUMN;
  42.         int status = 0;
  43.         Phrase p;
  44.         float y;
  45.         IEnumerable movies = PojoFactory.GetMovies();
  46.         foreach (Movie movie in movies) {
  47.           y = ct.YLine;
  48.           p = CreateMovieInformation(movie);
  49.           ct.AddText(p);
  50.           status = ct.Go(true);
  51.           if (ColumnText.HasMoreText(status)) {
  52.             column = Math.Abs(column - 1);
  53.             if (column == 0) {
  54.               document.NewPage();
  55.               DrawRectangles(canvas);
  56.             }
  57.             ct.SetColumns(LEFT[column], RIGHT[column]);
  58.             y = 806;
  59.           }
  60.           ct.YLine = y;
  61.           ct.SetText(p);
  62.           status = ct.Go();
  63.         }        
  64.       }
  65.     }
  66. // ---------------------------------------------------------------------------    
  67.     public readonly float[][] LEFT = {
  68.       new float[] { 36,806, 36,670, 108,670, 108,596, 36,596, 36,36 },
  69.       new float[] { 299,806, 299,484, 336,484, 336,410, 299,410, 299,36 }
  70.     };
  71.     public readonly float[][] RIGHT = {
  72.       new float[] { 296,806, 296,484, 259,484, 259,410, 296,410, 296,36 },
  73.       new float[] { 559,806, 559,246, 487,246, 487,172, 559,172, 559,36 }
  74.     };
  75. // ---------------------------------------------------------------------------    
  76.     /**
  77.      * Draws three rectangles
  78.      * @param canvas
  79.      */
  80.     public void DrawRectangles(PdfContentByte canvas) {
  81.       canvas.SaveState();
  82.       canvas.SetGrayFill(0.9f);
  83.       canvas.Rectangle(33, 592, 72, 72);
  84.       canvas.Rectangle(263, 406, 72, 72);
  85.       canvas.Rectangle(491, 168, 72, 72);
  86.       canvas.FillStroke();
  87.       canvas.RestoreState();
  88.     }    
  89. // ===========================================================================
  90.   }
  91. }
ColumnMovies1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  16.   public class ColumnMovies1 : IWriter {
  17. // ===========================================================================
  18.     public readonly string RESOURCE = Utility.ResourcePosters;
  19.     /** Definition of two columns */
  20.     public readonly float[,] COLUMNS = new float[,]  {
  21.       { 36, 36, 224, 579 } , { 230, 36, 418, 579 },
  22.       { 424, 36, 612, 579 } , { 618, 36, 806, 579 }
  23.     };
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       // step 1
  27.       using (Document document = new Document(PageSize.A4.Rotate())) {
  28.         // step 2
  29.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  30.         // step 3
  31.         document.Open();
  32.         // step 4
  33.         ColumnText ct = new ColumnText(writer.DirectContent);
  34.         int column = 0;
  35.         ct.SetSimpleColumn(
  36.           COLUMNS[column,0], COLUMNS[column,1],
  37.           COLUMNS[column,2], COLUMNS[column,3]
  38.         );
  39.         // iText-ONLY, 'Initial value of the status' => 0
  40.         // iTextSharp **DOES NOT** include this member variable
  41.         // int status = ColumnText.START_COLUMN;
  42.         int status = 0;
  43.         float y;
  44.         Image img;
  45.         IEnumerable movies = PojoFactory.GetMovies();
  46.         foreach (Movie movie in movies) {
  47.           y = ct.YLine;
  48.           img = Image.GetInstance(
  49.             Path.Combine(RESOURCE, movie.Imdb + ".jpg")
  50.           );
  51.           img.ScaleToFit(80, 1000);
  52.           AddContent(ct, movie, img);
  53.           status = ct.Go(true);
  54.           if (ColumnText.HasMoreText(status)) {
  55.             column = (column + 1) % 4;
  56.             if (column == 0) {
  57.               document.NewPage();
  58.             }
  59.             ct.SetSimpleColumn(
  60.               COLUMNS[column, 0], COLUMNS[column, 1],
  61.               COLUMNS[column, 2], COLUMNS[column, 3]
  62.             );
  63.             y = COLUMNS[column, 3];
  64.           }
  65.           ct.YLine = y;
  66.           ct.SetText(null);
  67.           AddContent(ct, movie, img);
  68.           status = ct.Go();
  69.         }
  70.       }
  71.     }
  72. // ---------------------------------------------------------------------------    
  73.     /**
  74.      * Add content to a ColumnText object.
  75.      * @param ct the ColumnText object
  76.      * @param movie a Movie object
  77.      * @param img the poster of the image
  78.      */
  79.     public void AddContent(ColumnText ct, Movie movie, Image img) {
  80.       ct.AddElement(img);
  81.       ct.AddElement(new Paragraph(movie.Title, FilmFonts.BOLD));
  82.       if (!string.IsNullOrEmpty(movie.OriginalTitle)) {
  83.         ct.AddElement(new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC));
  84.       }
  85.       ct.AddElement(PojoToElementFactory.GetDirectorList(movie));
  86.       ct.AddElement(PojoToElementFactory.GetYearPhrase(movie));
  87.       ct.AddElement(PojoToElementFactory.GetDurationPhrase(movie));
  88.       ct.AddElement(PojoToElementFactory.GetCountryList(movie));
  89.       ct.AddElement(Chunk.NEWLINE);
  90.     }    
  91. // ===========================================================================
  92.   }
  93. }
ColumnMovies2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14. using kuujinbo.iTextInAction2Ed.Chapter02;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  17.   public class ColumnMovies2 : IWriter {
  18. // ===========================================================================
  19.     /** Definition of two columns */
  20.     public readonly float[,] COLUMNS = new float[,]  {
  21.       { 40, 36, 219, 579 } , { 234, 36, 414, 579 },
  22.       { 428, 36, 608, 579 } , { 622, 36, 802, 579 }
  23.     };
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       // step 1
  27.       using (Document document = new Document(PageSize.A4.Rotate())) {
  28.         // step 2
  29.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  30.         // step 3
  31.         document.Open();
  32.         // step 4
  33.         ColumnText ct = new ColumnText(writer.DirectContent);
  34.         int column = 0;
  35.         ct.SetSimpleColumn(
  36.           COLUMNS[column,0], COLUMNS[column,1],
  37.           COLUMNS[column,2], COLUMNS[column,3]
  38.         );
  39.         // iText-ONLY, 'Initial value of the status' => 0
  40.         // iTextSharp **DOES NOT** include this member variable
  41.         // int status = ColumnText.START_COLUMN;
  42.         int status = 0;
  43.         float y;
  44.         IEnumerable movies = PojoFactory.GetMovies();
  45.         foreach (Movie movie in movies) {
  46.           y = ct.YLine;
  47.           AddContent(ct, movie);
  48.           status = ct.Go(true);
  49.           if (ColumnText.HasMoreText(status)) {
  50.             column = (column + 1) % 4;
  51.             if (column == 0) {
  52.               document.NewPage();
  53.             }
  54.             ct.SetSimpleColumn(
  55.               COLUMNS[column, 0], COLUMNS[column, 1],
  56.               COLUMNS[column, 2], COLUMNS[column, 3]
  57.             );
  58.             y = COLUMNS[column, 3];
  59.           }
  60.           ct.YLine = y;
  61.           ct.SetText(null);
  62.           AddContent(ct, movie);
  63.           status = ct.Go();
  64.         }
  65.       }
  66.     }
  67. // ---------------------------------------------------------------------------    
  68.     /**
  69.      * Add content to a ColumnText object.
  70.      * @param ct the ColumnText object
  71.      * @param movie a Movie object
  72.      * @param img the poster of the image
  73.      */
  74.     public void AddContent(ColumnText ct, Movie movie) {
  75.       Paragraph p;
  76.       p = new Paragraph(new Paragraph(movie.Title, FilmFonts.BOLD));
  77.       p.Alignment = Element.ALIGN_CENTER;
  78.       p.SpacingBefore = 16;
  79.       ct.AddElement(p);
  80.       if (!string.IsNullOrEmpty(movie.OriginalTitle)) {
  81.         p = new Paragraph(movie.OriginalTitle, FilmFonts.ITALIC);
  82.         p.Alignment = Element.ALIGN_RIGHT;
  83.         ct.AddElement(p);
  84.       }
  85.       p = new Paragraph();
  86.       p.Add(PojoToElementFactory.GetYearPhrase(movie));
  87.       p.Add(" ");
  88.       p.Add(PojoToElementFactory.GetDurationPhrase(movie));
  89.       p.Alignment = Element.ALIGN_JUSTIFIED_ALL;
  90.       ct.AddElement(p);
  91.       p = new Paragraph(new Chunk(new StarSeparator()));
  92.       p.SpacingAfter = 12;
  93.       ct.AddElement(p);
  94.   }    
  95. // ===========================================================================
  96.   }
  97. }
ImageDirect.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class ImageDirect : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       // step 1
  18.       using (Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30)) {
  19.         // step 2
  20.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  21.         writer.CompressionLevel = 0;
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         Image img = Image.GetInstance(Path.Combine(
  26.           Utility.ResourceImage, "loa.jpg"
  27.         ));
  28.         img.SetAbsolutePosition(
  29.           (PageSize.POSTCARD.Width - img.ScaledWidth) / 2,
  30.           (PageSize.POSTCARD.Height - img.ScaledHeight) / 2
  31.         );
  32.         writer.DirectContent.AddImage(img);
  33.         Paragraph p = new Paragraph(
  34.           "Foobar Film Festival",
  35.           new Font(Font.FontFamily.HELVETICA, 22)
  36.         );
  37.         p.Alignment = Element.ALIGN_CENTER;
  38.         document.Add(p);        
  39.       }
  40.     }
  41. // ===========================================================================
  42.   }
  43. }
ImageSkew.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class ImageSkew : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       // step 1
  18.       using (Document document = new Document(PageSize.POSTCARD.Rotate())) {
  19.         // step 2
  20.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  21.         writer.CompressionLevel = 0;
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         Image img = Image.GetInstance(Path.Combine(
  26.           Utility.ResourceImage, "loa.jpg"
  27.         ));
  28.         // Add the image to the upper layer
  29.         writer.DirectContent.AddImage(
  30.           img,
  31.           img.Width, 0, 0.35f * img.Height,
  32.           0.65f * img.Height, 30, 30
  33.         );
  34.       }
  35.     }
  36. // ===========================================================================
  37.   }
  38. }
ImageInline.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  14.   public class ImageInline : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       // step 1
  18.       using (Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30)) {
  19.         // step 2
  20.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  21.         writer.CompressionLevel = 0;
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         Image img = Image.GetInstance(Path.Combine(
  26.           Utility.ResourceImage, "loa.jpg"
  27.         ));
  28.         img.SetAbsolutePosition(
  29.           (PageSize.POSTCARD.Width - img.ScaledWidth) / 2,
  30.           (PageSize.POSTCARD.Height - img.ScaledHeight) / 2
  31.         );
  32.         writer.DirectContent.AddImage(img, true);
  33.       }
  34.     }
  35. // ===========================================================================
  36.   }
  37. }
MoviePosters.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  16.   public class MoviePosters : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document(PageSize.A4)) {
  21.         // step 2
  22.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  23.         writer.CompressionLevel = 0;
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte canvas = writer.DirectContent;
  28.         // Create the XObject
  29.         PdfTemplate celluloid = canvas.CreateTemplate(595, 84.2f);
  30.         celluloid.Rectangle(8, 8, 579, 68);
  31.         for (float f = 8.25f; f < 581; f+= 6.5f) {
  32.           celluloid.RoundRectangle(f, 8.5f, 6, 3, 1.5f);
  33.           celluloid.RoundRectangle(f, 72.5f, 6, 3, 1.5f);
  34.         }
  35.         celluloid.SetGrayFill(0.1f);
  36.         celluloid.EoFill();
  37.         // Write the XObject to the OutputStream
  38.         writer.ReleaseTemplate(celluloid);
  39.         // Add the XObject 10 times
  40.         for (int i = 0; i < 10; i++) {
  41.           canvas.AddTemplate(celluloid, 0, i * 84.2f);
  42.         }
  43.         // Go to the next page
  44.         document.NewPage();
  45.         // Add the XObject 10 times
  46.         for (int i = 0; i < 10; i++) {
  47.           canvas.AddTemplate(celluloid, 0, i * 84.2f);
  48.         }
  49.         // Get the movies from the database
  50.         IEnumerable movies = PojoFactory.GetMovies();
  51.         Image img;
  52.         float x = 11.5f;
  53.         float y = 769.7f;
  54.         string RESOURCE = Utility.ResourcePosters;
  55.         // Loop over the movies and add images
  56.         foreach (Movie movie in movies) {
  57.           img = Image.GetInstance(Path.Combine(RESOURCE, movie.Imdb + ".jpg"));
  58.           img.ScaleToFit(1000, 60);
  59.           img.SetAbsolutePosition(x + (45 - img.ScaledWidth) / 2, y);
  60.           canvas.AddImage(img);
  61.           x += 48;
  62.           if (x > 578) {
  63.             x = 11.5f;
  64.             y -= 84.2f;
  65.           }
  66.         }
  67.         // Go to the next page
  68.         document.NewPage();
  69.         // Add the template using a different CTM
  70.         canvas.AddTemplate(celluloid, 0.8f, 0, 0.35f, 0.65f, 0, 600);
  71.         // Wrap the XObject in an Image object
  72.         Image tmpImage = Image.GetInstance(celluloid);
  73.         tmpImage.SetAbsolutePosition(0, 480);
  74.         document.Add(tmpImage);
  75.         // Perform transformations on the image
  76.         tmpImage.RotationDegrees = 30;
  77.         tmpImage.ScalePercent(80);
  78.         tmpImage.SetAbsolutePosition(30, 500);
  79.         document.Add(tmpImage);
  80.         // More transformations
  81.         tmpImage.Rotation = (float)Math.PI / 2;
  82.         tmpImage.SetAbsolutePosition(200, 300);
  83.         document.Add(tmpImage);
  84.       }
  85.     }
  86. // ===========================================================================
  87.   }
  88. }
MovieTemplates.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. using iTextSharp.text.html;
  13. using kuujinbo.iTextInAction2Ed.Intro_1_2;
  14.  
  15. /**
  16.  * Draws a time table to the direct content using lines and simple shapes,
  17.  * adding blocks representing a movies.
  18.  */
  19. namespace kuujinbo.iTextInAction2Ed.Chapter03 {
  20.   public class MovieTemplates : MovieCalendar {
  21. // ===========================================================================
  22.     public override void Write(Stream stream) {
  23.       // step 1
  24.       using (Document document = new Document(PageSize.A4.Rotate())) {
  25.         // step 2
  26.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  27.         // step 3
  28.         document.Open();
  29.         // step 4
  30.         PdfContentByte over = writer.DirectContent;
  31.         PdfContentByte under = writer.DirectContentUnder;
  32.         locations = PojoFactory.GetLocations();
  33.         PdfTemplate t_under = under.CreateTemplate(
  34.           PageSize.A4.Height, PageSize.A4.Width
  35.         );
  36.         DrawTimeTable(t_under);
  37.         PdfTemplate t_over = over.CreateTemplate(
  38.           PageSize.A4.Height, PageSize.A4.Width
  39.         );
  40.         DrawTimeSlots(t_over);
  41.         DrawInfo(t_over);        
  42.         List days = PojoFactory.GetDays();
  43.         List screenings;
  44.         int d = 1;
  45.         foreach (string day in days) {
  46.           over.AddTemplate(t_over, 0, 0);
  47.           under.AddTemplate(t_under, 0, 0);
  48.           DrawDateInfo(day, d++, over);
  49.           screenings = PojoFactory.GetScreenings(day);
  50.           foreach (Screening screening in screenings) {
  51.             DrawBlock(screening, under, over);
  52.             DrawMovieInfo(screening, over);
  53.           }
  54.           document.NewPage();
  55.         }
  56.       }
  57.     }
  58. // ---------------------------------------------------------------------------            
  59.     /**
  60.      * Constructor MovieTemplates object.
  61.      */
  62.     public MovieTemplates() : base() {  }
  63. // ===========================================================================
  64.   }
  65. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now