Objects in columns

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

31st May 2016
admin-marketing

Switch code for this example

ColumnTextChunkImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/30843163/how-to-add-inline-images-in-itext-when-using-columntext-in-text-mode
  11.  */
  12. package com.itextpdf.samples.sandbox.columntext;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Image;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.layout.LayoutArea;
  22. import com.itextpdf.layout.renderer.ParagraphRenderer;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class ColumnTextChunkImage extends GenericTest {
  34.     public static final String DEST
  35.             = "./target/test/resources/sandbox/columntext/column_text_chunk_image.pdf";
  36.     public static final String DOG
  37.             = "./src/test/resources/img/dog.bmp";
  38.     public static final String FOX
  39.             = "./src/test/resources/img/fox.bmp";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new ColumnTextChunkImage().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         Image dog = new Image(ImageDataFactory.create(DOG));
  53.         Image fox = new Image(ImageDataFactory.create(FOX));
  54.         Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.");
  55.         p.add("Or, to say it in a more colorful way: quick brown ");
  56.         p.add(fox);
  57.         p.add(" jumps over the lazy ");
  58.         p.add(dog);
  59.         p.add(".");
  60.  
  61.         p.setNextRenderer(new ParagraphRenderer(p) {
  62.             @Override
  63.             public List initElementAreas(LayoutArea area) {
  64.                 List areas = new ArrayList();
  65.                 Rectangle rect = new Rectangle(50, 600, 350, 200);
  66.                 areas.add(rect);
  67.                 return areas;
  68.             }
  69.         });
  70.         doc.add(p);
  71.  
  72.         doc.close();
  73.     }
  74. }
SimpleColumn.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/34445641
  11.  */
  12. package com.itextpdf.samples.sandbox.columntext;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.layout.LayoutArea;
  21. import com.itextpdf.layout.renderer.ParagraphRenderer;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. @Category(SampleTest.class)
  32. public class SimpleColumn extends GenericTest {
  33.     public static final String DEST
  34.             = "./target/test/resources/sandbox/columntext/simple_column.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new SimpleColumn().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc, new PageSize(100, 120));
  46.  
  47.         Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text").setFontSize(4.5f);
  48.         paragraph.setNextRenderer(new ParagraphRenderer(paragraph) {
  49.             @Override
  50.             public List initElementAreas(LayoutArea area) {
  51.                 List list = new ArrayList();
  52.                 list.add(new Rectangle(9, 70, 61, 25));
  53.                 return list;
  54.             }
  55.         });
  56.         doc.add(paragraph);
  57.  
  58.         doc.close();
  59.     }
  60. }
AddLongTable.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.stamper;
  9.  
  10. import com.itextpdf.kernel.geom.PageSize;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfReader;
  13. import com.itextpdf.kernel.pdf.PdfWriter;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.Table;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23. @Category(SampleTest.class)
  24. public class AddLongTable extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_long_table.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddLongTable().manipulatePdf(DEST);
  32.     }
  33.  
  34.     @Override
  35.     protected void manipulatePdf(String dest) throws Exception {
  36.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  37.         Document doc = new Document(pdfDoc, PageSize.A4);
  38.         doc.setTopMargin(72);
  39.         Table table = new Table(2);
  40.         for (int i = 0; i < 250; ) {
  41.             table.addCell("Row " + (++i));
  42.             table.addCell("Test");
  43.         }
  44.         doc.add(table);
  45.         doc.close();
  46.     }
  47. }
ColumnTextChunkImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.objects;
  9.  
  10. import com.itextpdf.io.image.ImageDataFactory;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. @Category(SampleTest.class)
  26. public class ColumnTextChunkImage extends GenericTest {
  27.     public static final String DOG = "src/test/resources/img/dog.bmp";
  28.     public static final String FOX = "src/test/resources/img/fox.bmp";
  29.     public static final String DEST = "./target/test/resources/sandbox/objects/column_text_chunk_image.pdf";
  30.  
  31.     public static void main(String[] args) throws IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ColumnTextChunkImage().manipulatePdf(DEST);
  35.     }
  36.  
  37.     public void manipulatePdf(String dest) throws IOException {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         Document doc = new Document(pdfDoc);
  40.  
  41.         PdfImageXObject dog = new PdfImageXObject(ImageDataFactory.create(DOG));
  42.         PdfImageXObject fox = new PdfImageXObject(ImageDataFactory.create(FOX));
  43.         Paragraph p = new Paragraph("quick brown fox jumps over the lazy dog.").
  44.                 add("Or, to say it in a more colorful way: quick brown ").
  45.                 add(new Image(fox)).
  46.                 add(" jumps over the lazy ").
  47.                 add(new Image(dog)).
  48.                 add(".").
  49.                 setMultipliedLeading(1);
  50.         doc.add(p);
  51.  
  52.         doc.close();
  53.     }
  54.  
  55. }
ColumnTextParagraphs.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.layout.LayoutArea;
  21. import com.itextpdf.layout.layout.LayoutResult;
  22. import com.itextpdf.layout.renderer.DocumentRenderer;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29. import java.io.IOException;
  30.  
  31. @Category(SampleTest.class)
  32. public class ColumnTextParagraphs extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/objects/column_text_paragraphs.pdf";
  34.     public static final String TEXT = "This is some long paragraph " +
  35.             "that will be added over and over again to prove a point.";
  36.     public static final Rectangle[] COLUMNS = {
  37.             new Rectangle(36, 36, 254, 770),
  38.             new Rectangle(305, 36, 254, 770)
  39.     };
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new ColumnTextParagraphs().manipulatePdf(DEST);
  45.     }
  46.  
  47.     public void manipulatePdf(String dest) throws IOException {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.         Document doc = new Document(pdfDoc);
  50.  
  51.         doc.setRenderer(new DocumentRenderer(doc) {
  52.             int nextAreaNumber = 0;
  53.             int currentPageNumber;
  54.  
  55.             @Override
  56.             public LayoutArea updateCurrentArea(LayoutResult overflowResult) {
  57.                 if (nextAreaNumber % 2 == 0) {
  58.                     currentPageNumber = super.updateCurrentArea(overflowResult).getPageNumber();
  59.                 } else {
  60.                     new PdfCanvas(document.getPdfDocument(), document.getPdfDocument().getNumberOfPages())
  61.                             .moveTo(297.5f, 36)
  62.                             .lineTo(297.5f, 806)
  63.                             .stroke();
  64.                 }
  65.                 return (currentArea = new LayoutArea(currentPageNumber, COLUMNS[nextAreaNumber++ % 2].clone()));
  66.             }
  67.         });
  68.  
  69.         int paragraphs = 0;
  70.         while (paragraphs < 30) {
  71.             doc.add(new Paragraph(String.format("Paragraph %s: %s", ++paragraphs, TEXT)));
  72.         }
  73.         doc.close();
  74.     }
  75. }
ListInColumn.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/29277611/itextsharp-continuing-ordered-list-on-second-page-with-a-number-other-than-1
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.ColumnDocumentRenderer;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.List;
  21. import com.itextpdf.layout.property.ListNumberingType;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class ListInColumn extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/objects/list_in_column.pdf";
  33.     public static final String SRC = "./src/test/resources/pdfs/pages.pdf";
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new ListInColumn().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  43.         while (pdfDoc.getNumberOfPages() > 2) {
  44.             pdfDoc.removePage(pdfDoc.getLastPage());
  45.         }
  46.         Document doc = new Document(pdfDoc);
  47.         doc.setRenderer(new ColumnDocumentRenderer(doc, new Rectangle[]{new Rectangle(250, 400, 250, 406)}));
  48.  
  49.         List list = new List(ListNumberingType.DECIMAL);
  50.         for (int i = 0; i < 10; i++) {
  51.             list.add("This is a list item. It will be repeated a number of times. "
  52.                     + "This is done only for test purposes. "
  53.                     + "I want a list item that is distributed over several lines.");
  54.         }
  55.         doc.add(list);
  56.  
  57.         doc.close();
  58.     }
  59. }
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