title: How to make a PDF of multiple pages with iText Java
body: <p>I have a list with fruits, I'm trying to put a fruit of the list with it's data into a JPanel by using a for loop. I want to fill the screen with JPanels of 4 by 2 (total 8) into one page. See result on the image below. </p>
  
  <p>In the code example I didn't used the fruitslist, but instead I used a for loop which represents like I have a list of 11 fruits >> for(int i = 0; i &lt; 11; i++).
  So what I'm expecting to get now is one PDF file with 2 pages. The first one contains 8 panels and the second one 3 panels. What I'm wondering is how I can make the application to get multiple paged PDF where one page only contains max 8 panels. Any help would be appreciated.</p>
  
  <p>//The code of the JFrame//</p>
  
  <pre><code>import com.itextpdf.text.Document;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfTemplate;
  import com.itextpdf.text.pdf.PdfWriter;
  import java.awt.*;
  import java.io.FileOutputStream;
  import java.util.Date;
  import javax.swing.*;
  
  public class SmallList extends JFrame {
  
  GridLayout gridLayout = new GridLayout(0,4);
  
  public SmallList(String name) {
      super(name);
      setResizable(false);
  }
  
  public void addComponentsToPane(final Container pane) {
      final JPanel components = new JPanel();
      components.setLayout(gridLayout);
  
      for(int i = 0; i &lt; 11; i++) {
          JPanel jPanel = new javax.swing.JPanel();
          JSeparator seperator = new javax.swing.JSeparator();
          JSeparator separator1 = new javax.swing.JSeparator();
          JLabel nameLabel = new javax.swing.JLabel();
          JLabel unitLabel = new javax.swing.JLabel();
          JLabel priceLabel = new javax.swing.JLabel();
          JLabel centLabel = new javax.swing.JLabel();
          JLabel comma = new javax.swing.JLabel();
          jPanel.setBackground(new java.awt.Color(255, 255, 255));
          jPanel.setAlignmentX(0.0F);
          jPanel.setAlignmentY(0.0F);
          jPanel.setMaximumSize(null);
          jPanel.setPreferredSize(new java.awt.Dimension(203, 289));
  
          seperator.setBackground(new java.awt.Color(0, 0, 0));
  
          separator1.setBackground(new java.awt.Color(0, 0, 0));
  
          nameLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
          nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  
          unitLabel.setFont(new java.awt.Font("Tahoma", 0, 40)); // NOI18N
          unitLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  
          priceLabel.setFont(new java.awt.Font("Tahoma", 0, 90)); // NOI18N
          priceLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
          priceLabel.setText(" ");
  
          centLabel.setFont(new java.awt.Font("Tahoma", 0, 55)); // NOI18N
          centLabel.setText("  ");
  
          comma.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
          comma.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
          comma.setText(",");
  
          javax.swing.GroupLayout jPanelLayout = new javax.swing.GroupLayout(jPanel);
          jPanel.setLayout(jPanelLayout);
          jPanelLayout.setHorizontalGroup(
              jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(seperator)
              .addComponent(separator1)
              .addGroup(jPanelLayout.createSequentialGroup()
                  .addContainerGap()
                  .addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(unitLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
                          .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                              .addGroup(jPanelLayout.createSequentialGroup()
                                  .addComponent(comma)
                                  .addGap(0, 0, Short.MAX_VALUE))
                              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
                                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                  .addComponent(centLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                  .addContainerGap())
          );
          jPanelLayout.setVerticalGroup(
              jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanelLayout.createSequentialGroup()
                  .addGap(6, 6, 6)
                  .addComponent(unitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(seperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanelLayout.createSequentialGroup()
                          .addGap(32, 32, 32)
                          .addGroup(jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                              .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(comma)))
                      .addGroup(jPanelLayout.createSequentialGroup()
                          .addGap(29, 29, 29)
                          .addComponent(centLabel)))
                  .addGap(32, 32, 32)
                  .addComponent(separator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap())
          );
          jPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  
          components.add(jPanel);
      }
  
      pane.add(components);
      print();
  }
  
  private void print() {
      Date date = new Date();
      try {
          JFrame f = new JFrame("Productlijst klein");
          f.add(this.getComponent(0));
          f.setUndecorated(true);
          f.pack();
          f.setVisible(true);
  
          Document document = new Document(PageSize.A4.rotate());
          String fileName = "prijslijst" + date.getTime() + ".pdf";
          PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
          document.open();
  
          PdfContentByte cb = writer.getDirectContent();
          PdfTemplate template = cb.createTemplate(PageSize.A4.getHeight(), PageSize.A4.getWidth());
          Graphics2D g2d = template.createGraphics(f.getWidth(), f.getHeight());
          f.paintAll(g2d);
          g2d.dispose();
          cb.addTemplate(template, 0, 0);
  
          document.close();           
  
      } catch (Exception e) {
          System.out.println(e);
      }
  }
  
  /**
   * Create the GUI and show it.  For thread safety,
   * this method is invoked from the
   * event dispatch thread.
   */
  private static void createAndShowGUI() {
      //Create and set up the window.
      SmallList frame = new SmallList("Fruit - kleine afmeting");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //Set up the content pane.
      frame.addComponentsToPane(frame.getContentPane());
      //Display the window.
      frame.pack();
      frame.setVisible(true);
  }
  
  public static void main(String[] args) {
      /* Use an appropriate Look and Feel */
      try {
          //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      } catch (UnsupportedLookAndFeelException ex) {
          ex.printStackTrace();
      } catch (IllegalAccessException ex) {
          ex.printStackTrace();
      } catch (InstantiationException ex) {
          ex.printStackTrace();
      } catch (ClassNotFoundException ex) {
          ex.printStackTrace();
      }
      /* Turn off metal's use of bold fonts */
      UIManager.put("swing.boldMetal", Boolean.FALSE);
  
      //Schedule a job for the event dispatch thread:
      //creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              createAndShowGUI();
          }
      });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The result will be like this, when the application is finished. One page will contain 8 JPanels of fruits.
  <a href="https://i.stack.imgur.com/0arsW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0arsW.png" alt="Result"></a></p>