Objects in columns

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

23rd October 2015
admin-marketing

Switch code for this example

ColumnTextParagraphs.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ColumnTextParagraphs {
  22.  
  23.     public static final String DEST = "results/objects/column_paragraphs.pdf";
  24.  
  25.     public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point.";
  26.     public static final Rectangle[] COLUMNS = {
  27.         new Rectangle(36, 36, 290, 806),
  28.         new Rectangle(305, 36, 559, 806)
  29.     };
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ColumnTextParagraphs().createPdf(DEST);
  35.     }
  36.  
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document();
  40.         // step 2
  41.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         // step 3
  43.         document.open();
  44.         // step 4
  45.         PdfContentByte canvas = writer.getDirectContent();
  46.         ColumnText ct = new ColumnText(canvas);
  47.         int side_of_the_page = 0;
  48.         ct.setSimpleColumn(COLUMNS[side_of_the_page]);
  49.         int paragraphs = 0;
  50.         while (paragraphs < 30) {
  51.             ct.addElement(new Paragraph(String.format("Paragraph %s: %s", ++paragraphs, TEXT)));
  52.             while (ColumnText.hasMoreText(ct.go())) {
  53.                 if (side_of_the_page == 0) {
  54.                     side_of_the_page = 1;
  55.                     canvas.moveTo(297.5f, 36);
  56.                     canvas.lineTo(297.5f, 806);
  57.                     canvas.stroke();
  58.                 }
  59.                 else {
  60.                     side_of_the_page = 0;
  61.                     document.newPage();
  62.                 }
  63.                 ct.setSimpleColumn(COLUMNS[side_of_the_page]);
  64.             }
  65.         }
  66.         // step 5
  67.         document.close();
  68.     }
  69. }
ColumnTextParagraphs2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29378407/how-can-you-eliminate-white-space-in-multiple-columns-using-itextsharp
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ColumnTextParagraphs2 {
  22.  
  23.     public static final String DEST = "results/objects/column_paragraphs2.pdf";
  24.  
  25.     public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point.";
  26.     public static final float COLUMN_WIDTH = 254;
  27.     public static final float ERROR_MARGIN = 16;
  28.     public static final Rectangle[] COLUMNS = {
  29.         new Rectangle(36, 36, 36 + COLUMN_WIDTH, 806),
  30.         new Rectangle(305, 36, 305 + COLUMN_WIDTH, 806)
  31.     };
  32.    
  33.     public static void main(String[] args) throws IOException, DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ColumnTextParagraphs2().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException, DocumentException {
  40.         // step 1
  41.         Document document = new Document();
  42.         // step 2
  43.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  44.         // step 3
  45.         document.open();
  46.         // step 4
  47.         PdfContentByte canvas = writer.getDirectContent();
  48.         ColumnText ct = new ColumnText(canvas);
  49.         addContent(ct);
  50.         float height = getNecessaryHeight(ct);
  51.         addContent(ct);
  52.         Rectangle left;
  53.         float top = COLUMNS[0].getTop();
  54.         float middle = (COLUMNS[0].getLeft() + COLUMNS[1].getRight()) / 2;
  55.         float columnheight;
  56.         int status = ColumnText.START_COLUMN;
  57.         while (ColumnText.hasMoreText(status)) {
  58.             if (checkHeight(height)) {
  59.                 columnheight = COLUMNS[0].getHeight();
  60.                 left = COLUMNS[0];
  61.             }
  62.             else {
  63.                 columnheight = (height / 2) + ERROR_MARGIN;
  64.                 left = new Rectangle(
  65.                     COLUMNS[0].getLeft(),
  66.                     COLUMNS[0].getTop() - columnheight,
  67.                     COLUMNS[0].getRight(),
  68.                     COLUMNS[0].getTop()
  69.                 );
  70.            }
  71.             // left half
  72.             ct.setSimpleColumn(left);
  73.             ct.go();
  74.             height -= COLUMNS[0].getTop() - ct.getYLine();
  75.             // separator
  76.             canvas.moveTo(middle, top - columnheight);
  77.             canvas.lineTo(middle, top);
  78.             canvas.stroke();
  79.             // right half
  80.             ct.setSimpleColumn(COLUMNS[1]);
  81.             status = ct.go();
  82.             height -= COLUMNS[1].getTop() - ct.getYLine();
  83.             // new page
  84.             document.newPage();
  85.         }
  86.         // step 5
  87.         document.close();
  88.     }
  89.    
  90.     public void addContent(ColumnText ct) {
  91.         for (int i = 0; i < 35; i++) {
  92.             ct.addElement(new Paragraph(String.format("Paragraph %s: %s", i, TEXT)));
  93.         }
  94.     }
  95.    
  96.     public float getNecessaryHeight(ColumnText ct) throws DocumentException {
  97.         ct.setSimpleColumn(new Rectangle(0, 0, COLUMN_WIDTH, -500000));
  98.         ct.go(true);
  99.         return -ct.getYLine();
  100.     }
  101.    
  102.     public boolean checkHeight(float height) {
  103.         height -= COLUMNS[0].getHeight() + COLUMNS[1].getHeight() + ERROR_MARGIN;
  104.         return height > 0;
  105.     }
  106. }
ListInColumn.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29277611/itextsharp-continuing-ordered-list-on-second-page-with-a-number-other-than-1
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.List;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.ColumnText;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ListInColumn {
  22.  
  23.     public static final String SRC = "resources/pdfs/pages.pdf";
  24.     public static final String DEST = "results/objects/list_in_column.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException,
  27.             DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ListInColumn().manipulatePdf(SRC, DEST);
  31.     }
  32.    
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         reader.selectPages("1-2");
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         List list = new List(List.ORDERED);
  38.         for (int i = 0; i < 10; i++) {
  39.             list.add("This is a list item. It will be repeated a number of times. "
  40.                     + "This is done only for test purposes. "
  41.                     + "I want a list item that is distributed over several lines.");
  42.         }
  43.         ColumnText ct = new ColumnText(stamper.getOverContent(1));
  44.         ct.addElement(list);
  45.         Rectangle rect = new Rectangle(250, 400, 500, 806);
  46.         ct.setSimpleColumn(rect);
  47.         int status = ct.go();
  48.         if (ColumnText.hasMoreText(status)) {
  49.             ct.setCanvas(stamper.getOverContent(2));
  50.             ct.setSimpleColumn(rect);
  51.             ct.go();
  52.         }
  53.         stamper.close();
  54.     }
  55. }
ColumnTextChunkImage.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30843163/how-to-add-inline-images-in-itext-when-using-columntext-in-text-mode
  4.  */
  5. package sandbox.columntext;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.ColumnText;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. @WrapToTest
  26. public class ColumnTextChunkImage {
  27.     public static final String DOG = "resources/images/dog.bmp";
  28.     public static final String FOX = "resources/images/fox.bmp";
  29.     public static final String DEST = "results/columntext/column_text_chunk_image.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ColumnTextChunkImage().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document();
  40.         // step 2
  41.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         // step 3
  43.         document.open();
  44.         // step 4
  45.         Image dog = Image.getInstance(DOG);
  46.         Image fox = Image.getInstance(FOX);
  47.         Phrase p = new Phrase("quick brown fox jumps over the lazy dog.");
  48.         p.add("Or, to say it in a more colorful way: quick brown ");
  49.         p.add(new Chunk(fox, 0, 0, true));
  50.         p.add(" jumps over the lazy ");
  51.         p.add(new Chunk(dog, 0, 0, true));
  52.         p.add(".");
  53.         ColumnText ct = new ColumnText(writer.getDirectContent());
  54.         ct.setSimpleColumn(new Rectangle(50, 600, 400, 800));
  55.         ct.addText(p);
  56.         ct.go();
  57.         // step 5
  58.         document.close();
  59.     }
  60. }
TableInColumn.java
  1. package sandbox.objects;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.log.LoggerFactory;
  8. import com.itextpdf.text.log.SysoLogger;
  9. import com.itextpdf.text.pdf.ColumnText;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15.  
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class TableInColumn {
  23.  
  24.     public static final String DEST = "results/objects/table_in_column.pdf";
  25.    
  26.     private int status = ColumnText.START_COLUMN;
  27.     private float y_position = 0;
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         LoggerFactory.getInstance().setLogger(new SysoLogger());
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new TableInColumn().createPdf(DEST);
  34.     }
  35.  
  36.     public void createPdf(String dest) throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document();
  39.         // step 2
  40.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         PdfContentByte pagecanvas = writer.getDirectContent();
  45.         PdfPTable table;
  46.         PdfPCell cell = new PdfPCell();
  47.         for (int i = 1; i
AddLongTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21838846/inserting-a-table-into-multiple-pdf-pages-with-columntext
  4.  *
  5.  * The person asking the question forgot to set the canvas for the ColumnText object.
  6.  */
  7. package sandbox.stamper;
  8.  
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class AddLongTable {
  24.  
  25.  
  26.     public static final String SRC = "resources/pdfs/hello.pdf";
  27.     public static final String DEST = "results/stamper/hello_table.pdf";
  28.  
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddLongTable().manipulatePdf(SRC, DEST);
  33.     }
  34.  
  35.  
  36.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  37.         PdfPTable table = new PdfPTable(2);
  38.         for (int i = 0; i < 250; ) {
  39.             table.addCell("Row " + (++i));
  40.             table.addCell("Test");
  41.         }
  42.         PdfReader reader = new PdfReader(src);
  43.         int pageNum = 1;
  44.         Rectangle page = reader.getPageSize(pageNum);
  45.         Rectangle rect = new Rectangle(36, 36, page.getRight(36), page.getTop(72));
  46.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  47.         ColumnText ct = new ColumnText(stamper.getOverContent(pageNum));
  48.         ct.addElement(table);
  49.         while(true) {
  50.             ct.setSimpleColumn(rect);
  51.             if (!ColumnText.hasMoreText(ct.go()))
  52.                 break;
  53.             stamper.insertPage(++pageNum, page);
  54.             ct.setCanvas(stamper.getOverContent(pageNum));
  55.         }
  56.         stamper.close();
  57.         reader.close();
  58.     }
  59. }
SimpleColumn.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34445641
  4.  */
  5. package sandbox.columntext;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.ColumnText;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  * @author Bruno Lowagie (iText Software)
  23.  */
  24. @WrapToTest
  25. public class SimpleColumn {
  26.     public static final String DEST = "results/columntext/simple_column.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new SimpleColumn().createPdf(DEST);
  32.     }
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         // step 1
  35.         Rectangle rect = new Rectangle(100, 120);
  36.         Document document = new Document(rect);
  37.         // step 2
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         PdfContentByte canvas = writer.getDirectContent();
  43.         BaseFont baseFont = BaseFont.createFont();
  44.         ColumnText ct = new ColumnText(canvas);
  45.         Font paragraphFont=new Font(baseFont,4.5f);
  46.         ct.setSimpleColumn(9, 70, 70, 95);
  47.         Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
  48.         ct.addElement(paragraph);
  49.         ct.go();
  50.         // step 5
  51.         document.close();
  52.     }
  53. }
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