iText 7 Building Blocks - Chapter 4: examples AbstractElement (part 1)

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

2nd July 2016
iText PDF

Switch code for this example

C04E01_DivExample1.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.kernel.pdf.action.PdfAction;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.border.SolidBorder;
  14. import com.itextpdf.layout.element.Div;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.layout.element.Link;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.test.annotations.WrapToTest;
  19.  
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.List;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E01_DivExample1 {
  29.  
  30.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  31.     public static final String DEST = "results/chapter04/jekyll_hyde_overviewV1.pdf";
  32.  
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C04E01_DivExample1().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException {
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.         Document document = new Document(pdf);
  42.         List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
  43.         resultSet.remove(0);
  44.         for (List<String> record : resultSet) {
  45.             Div div = new Div()
  46.                 .setBorderLeft(new SolidBorder(2))
  47.                 .setPaddingLeft(3)
  48.                 .setMarginBottom(10);
  49.             String url = String.format(
  50.                 "http://www.imdb.com/title/tt%s", record.get(0));
  51.             Link movie = new Link(record.get(2), PdfAction.createURI(url));
  52.             div.add(new Paragraph(movie.setFontSize(14)))
  53.                 .add(new Paragraph(String.format(
  54.                     "Directed by %s (%s, %s)",
  55.                     record.get(3), record.get(4), record.get(1))));
  56.             File file = new File(String.format(
  57.                 "src/main/resources/img/%s.jpg", record.get(0)));
  58.             if (file.exists()) {
  59.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  60.                 img.scaleToFit(10000, 120);
  61.                 div.add(img);
  62.             }
  63.             document.add(div);
  64.         }
  65.         document.close();
  66.     }
  67.  
  68. }
C04E02_DivExample2.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.kernel.pdf.action.PdfAction;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.border.SolidBorder;
  14. import com.itextpdf.layout.element.Div;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.layout.element.Link;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.test.annotations.WrapToTest;
  19.  
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.List;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E02_DivExample2 {
  29.  
  30.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  31.     public static final String DEST = "results/chapter04/jekyll_hyde_overviewV2.pdf";
  32.  
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C04E02_DivExample2().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException {
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.         Document document = new Document(pdf);
  42.         List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
  43.         resultSet.remove(0);
  44.         for (List<String> record : resultSet) {
  45.             Div div = new Div()
  46.                 .setKeepTogether(true)
  47.                 .setBorderLeft(new SolidBorder(2))
  48.                 .setPaddingLeft(3)
  49.                 .setMarginBottom(10);
  50.             String url = String.format(
  51.                 "http://www.imdb.com/title/tt%s", record.get(0));
  52.             Link movie = new Link(record.get(2), PdfAction.createURI(url));
  53.             div.add(new Paragraph(movie.setFontSize(14)))
  54.                 .add(new Paragraph(String.format(
  55.                     "Directed by %s (%s, %s)",
  56.                     record.get(3), record.get(4), record.get(1))));
  57.             File file = new File(String.format(
  58.                 "src/main/resources/img/%s.jpg", record.get(0)));
  59.             if (file.exists()) {
  60.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  61.                 img.scaleToFit(10000, 120);
  62.                 div.add(img);
  63.             }
  64.             document.add(div);
  65.         }
  66.         document.close();
  67.     }
  68.  
  69. }
C04E03_LineSeparatorExample.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.color.Color;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.kernel.pdf.action.PdfAction;
  13. import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.border.SolidBorder;
  16. import com.itextpdf.layout.element.*;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18.  
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.List;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class C04E03_LineSeparatorExample {
  28.  
  29.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  30.     public static final String DEST = "results/chapter04/jekyll_hyde_overviewV3.pdf";
  31.  
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C04E03_LineSeparatorExample().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException {
  39.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  40.         Document document = new Document(pdf);
  41.         SolidLine line = new SolidLine(1f);
  42.         line.setColor(Color.RED);
  43.         LineSeparator ls = new LineSeparator(line);
  44.         ls.setWidthPercent(50);
  45.         ls.setMarginTop(5);
  46.         List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
  47.         resultSet.remove(0);
  48.         for (List<String> record : resultSet) {
  49.             String url = String.format(
  50.                 "http://www.imdb.com/title/tt%s", record.get(0));
  51.             Link movie = new Link(record.get(2), PdfAction.createURI(url));
  52.             Div div = new Div()
  53.                 .setKeepTogether(true)
  54.                 .setBorderLeft(new SolidBorder(2))
  55.                 .setPaddingLeft(3)
  56.                 .setMarginBottom(10)
  57.                 .add(new Paragraph(movie.setFontSize(14f)))
  58.                 .add(new Paragraph(String.format(
  59.                     "Directed by %s (%s, %s)",
  60.                     record.get(3), record.get(4), record.get(1))));
  61.             File file = new File(String.format(
  62.                 "src/main/resources/img/%s.jpg", record.get(0)));
  63.             if (file.exists()) {
  64.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  65.                 img.scaleToFit(10000, 120);
  66.                 div.add(img);
  67.             }
  68.             div.add(ls);
  69.             document.add(div);
  70.         }
  71.         document.close();
  72.     }
  73.  
  74. }
C04E04_ParagraphAndDiv1.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Div;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  16. import com.itextpdf.layout.property.TextAlignment;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E04_ParagraphAndDiv1 {
  29.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  30.     public static final String DEST = "results/chapter04/jekyll_hydeV1.pdf";
  31.  
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C04E04_ParagraphAndDiv1().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException {
  39.         //Initialize PDF document
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.  
  42.         // Initialize document
  43.         Document document = new Document(pdf);
  44.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  45.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  46.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  47.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
  48.  
  49.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  50.         String line;
  51.         Div div = new Div();
  52.         while ((line = br.readLine()) != null) {
  53.             Paragraph title = new Paragraph(line)
  54.                 .setFont(bold).setFontSize(12)
  55.                 .setMarginBottom(0);
  56.             div = new Div()
  57.                 .add(title)
  58.                 .setFont(font).setFontSize(11)
  59.                 .setMarginBottom(18);
  60.             while ((line = br.readLine()) != null) {
  61.                 div.add(
  62.                     new Paragraph(line)
  63.                         .setMarginBottom(0)
  64.                         .setFirstLineIndent(36)
  65.                 );
  66.                 if (line.isEmpty()) {
  67.                     document.add(div);
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         document.add(div);
  73.  
  74.         //Close document
  75.         document.close();
  76.     }
  77.  
  78. }
C04E05_ParagraphAndDiv2.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Div;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  16. import com.itextpdf.layout.property.TextAlignment;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E05_ParagraphAndDiv2 {
  29.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  30.     public static final String DEST = "results/chapter04/jekyll_hydeV2.pdf";
  31.  
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C04E05_ParagraphAndDiv2().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException {
  39.         //Initialize PDF document
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.  
  42.         // Initialize document
  43.         Document document = new Document(pdf);
  44.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  45.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  46.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  47.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
  48.  
  49.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  50.         String line;
  51.         Div div = new Div();
  52.         while ((line = br.readLine()) != null) {
  53.             document.add(new Paragraph(line)
  54.                 .setFont(bold).setFontSize(12)
  55.                 .setMarginBottom(0)
  56.                 .setKeepWithNext(true));
  57.             div = new Div()
  58.                 .setFont(font).setFontSize(11)
  59.                 .setMarginBottom(18);
  60.             while ((line = br.readLine()) != null) {
  61.                 div.add(
  62.                     new Paragraph(line)
  63.                         .setMarginBottom(0)
  64.                         .setFirstLineIndent(36)
  65.                 );
  66.                 if (line.isEmpty()) {
  67.                     document.add(div);
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         document.add(div);
  73.  
  74.         //Close document
  75.         document.close();
  76.     }
  77.  
  78. }
C04E05_ParagraphAndDiv3.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Div;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.hyphenation.HyphenationConfig;
  16. import com.itextpdf.layout.property.TextAlignment;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E05_ParagraphAndDiv3 {
  29.     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
  30.     public static final String DEST = "results/chapter04/jekyll_hydeV3.pdf";
  31.  
  32.     public static void main(String args[]) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new C04E05_ParagraphAndDiv3().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException {
  39.         //Initialize PDF document
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.  
  42.         // Initialize document
  43.         Document document = new Document(pdf);
  44.         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  45.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  46.         document.setTextAlignment(TextAlignment.JUSTIFIED)
  47.             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
  48.  
  49.         BufferedReader br = new BufferedReader(new FileReader(SRC));
  50.         String line;
  51.         Div div = new Div();
  52.         while ((line = br.readLine()) != null) {
  53.             document.add(new Paragraph(line)
  54.                 .setFont(bold).setFontSize(12)
  55.                 .setMarginBottom(0)
  56.                 .setKeepWithNext(true));
  57.             div = new Div()
  58.                 .setFont(font).setFontSize(11)
  59.                 .setMarginBottom(18);
  60.             while ((line = br.readLine()) != null) {
  61.                 div.add(
  62.                     new Paragraph(line)
  63.                         .setMarginBottom(0)
  64.                         .setFirstLineIndent(36)
  65.                         .setMultipliedLeading(1.2f)
  66.                 );
  67.                 if (line.isEmpty()) {
  68.                     document.add(div);
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.         document.add(div);
  74.  
  75.         //Close document
  76.         document.close();
  77.     }
  78.  
  79. }
C04E06_CustomParagraph.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.kernel.color.Color;
  8. import com.itextpdf.kernel.geom.Rectangle;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.kernel.pdf.canvas.CanvasArtifact;
  12. import com.itextpdf.kernel.pdf.tagutils.IAccessibleElement;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.property.Background;
  16. import com.itextpdf.layout.property.Property;
  17. import com.itextpdf.layout.renderer.DrawContext;
  18. import com.itextpdf.layout.renderer.ParagraphRenderer;
  19. import com.itextpdf.test.annotations.WrapToTest;
  20. import java.io.File;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class C04E06_CustomParagraph {
  28.  
  29.     class MyParagraphRenderer extends ParagraphRenderer {
  30.  
  31.         public MyParagraphRenderer(Paragraph modelElement) {
  32.             super(modelElement);
  33.         }
  34.  
  35.         @Override
  36.         public void drawBackground(DrawContext drawContext) {
  37.             Background background = this.<Background>getProperty(Property.BACKGROUND);
  38.             if (background != null) {
  39.                 Rectangle bBox = getOccupiedAreaBBox();
  40.                 boolean isTagged =
  41.                     drawContext.isTaggingEnabled()
  42.                     && getModelElement() instanceof IAccessibleElement;
  43.                 if (isTagged) {
  44.                     drawContext.getCanvas().openTag(new CanvasArtifact());
  45.                 }
  46.                 Rectangle bgArea = applyMargins(bBox, false);
  47.                 if (bgArea.getWidth() <= 0 || bgArea.getHeight() <= 0) {
  48.                     return;
  49.                 }
  50.                 drawContext.getCanvas().saveState()
  51.                     .setFillColor(background.getColor())
  52.                     .roundRectangle(
  53.                     (double)bgArea.getX() - background.getExtraLeft(),
  54.                     (double)bgArea.getY() - background.getExtraBottom(),
  55.                     (double)bgArea.getWidth()
  56.                         + background.getExtraLeft() + background.getExtraRight(),
  57.                     (double)bgArea.getHeight()
  58.                         + background.getExtraTop() + background.getExtraBottom(),
  59.                     5)
  60.                     .fill().restoreState();
  61.                 if (isTagged) {
  62.                     drawContext.getCanvas().closeTag();
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static final String DEST = "results/chapter04/custom_paragraph.pdf";
  69.  
  70.     public static void main(String args[]) throws IOException {
  71.         File file = new File(DEST);
  72.         file.getParentFile().mkdirs();
  73.         new C04E06_CustomParagraph().createPdf(DEST);
  74.     }
  75.  
  76.     public void createPdf(String dest) throws IOException {
  77.         // Initialize PDF document
  78.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  79.  
  80.         // Initialize document
  81.         Document document = new Document(pdf);
  82.  
  83.         Paragraph p1 = new Paragraph(
  84.             "The Strange Case of Dr. Jekyll and Mr. Hyde");
  85.         p1.setBackgroundColor(Color.ORANGE);
  86.         document.add(p1);
  87.  
  88.         Paragraph p2 = new Paragraph(
  89.             "The Strange Case of Dr. Jekyll and Mr. Hyde");
  90.         p2.setBackgroundColor(Color.ORANGE);
  91.         p2.setNextRenderer(new MyParagraphRenderer(p2));
  92.         document.add(p2);
  93.  
  94.         document.close();
  95.     }
  96. }
C04E07_ListTypes.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.kernel.geom.PageSize;
  8. import com.itextpdf.kernel.geom.Rectangle;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.layout.ColumnDocumentRenderer;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.List;
  14. import com.itextpdf.layout.property.ListNumberingType;
  15. import com.itextpdf.test.annotations.WrapToTest;
  16. import java.io.File;
  17. import java.io.IOException;
  18.  
  19. /**
  20.  * @author Bruno Lowagie (iText Software)
  21.  */
  22. @WrapToTest
  23. public class C04E07_ListTypes {
  24.  
  25.     public static final String DEST = "results/chapter04/list_types.pdf";
  26.  
  27.     public static void main(String args[]) throws IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new C04E07_ListTypes().createPdf(DEST);
  31.     }
  32.  
  33.     public void createPdf(String dest) throws IOException {
  34.         //Initialize PDF document
  35.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  36.  
  37.         // Initialize document
  38.         PageSize pagesize = PageSize.A6.rotate();
  39.         Document document = new Document(pdf, pagesize);
  40.         //Set column parameters
  41.         float offSet = 36;
  42.         float gutter = 23;
  43.         float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
  44.         float columnHeight = pagesize.getHeight() - offSet * 2;
  45.  
  46.         //Define column areas
  47.         Rectangle[] columns = {
  48.             new Rectangle(offSet, offSet, columnWidth, columnHeight),
  49.             new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
  50.         document.setRenderer(new ColumnDocumentRenderer(document, columns));    
  51.  
  52.         List list = new List();
  53.         list.add("Dr. Jekyll");
  54.         list.add("Mr. Hyde");
  55.         document.add(list);
  56.  
  57.         list = new List(ListNumberingType.DECIMAL);
  58.         list.add("Dr. Jekyll");
  59.         list.add("Mr. Hyde");
  60.         document.add(list);
  61.  
  62.         list = new List(ListNumberingType.ENGLISH_LOWER);
  63.         list.add("Dr. Jekyll");
  64.         list.add("Mr. Hyde");
  65.         document.add(list);
  66.  
  67.         list = new List(ListNumberingType.ENGLISH_UPPER);
  68.         list.add("Dr. Jekyll");
  69.         list.add("Mr. Hyde");
  70.         document.add(list);
  71.  
  72.         list = new List(ListNumberingType.GREEK_LOWER);
  73.         list.add("Dr. Jekyll");
  74.         list.add("Mr. Hyde");
  75.         document.add(list);
  76.  
  77.         list = new List(ListNumberingType.GREEK_UPPER);
  78.         list.add("Dr. Jekyll");
  79.         list.add("Mr. Hyde");
  80.         document.add(list);
  81.  
  82.         list = new List(ListNumberingType.ROMAN_LOWER);
  83.         list.add("Dr. Jekyll");
  84.         list.add("Mr. Hyde");
  85.         document.add(list);
  86.  
  87.         list = new List(ListNumberingType.ROMAN_UPPER);
  88.         list.add("Dr. Jekyll");
  89.         list.add("Mr. Hyde");
  90.         document.add(list);
  91.  
  92.         list = new List(ListNumberingType.ZAPF_DINGBATS_1);
  93.         list.add("Dr. Jekyll");
  94.         list.add("Mr. Hyde");
  95.         document.add(list);
  96.  
  97.         list = new List(ListNumberingType.ZAPF_DINGBATS_2);
  98.         list.add("Dr. Jekyll");
  99.         list.add("Mr. Hyde");
  100.         document.add(list);
  101.  
  102.         list = new List(ListNumberingType.ZAPF_DINGBATS_3);
  103.         list.add("Dr. Jekyll");
  104.         list.add("Mr. Hyde");
  105.         document.add(list);
  106.  
  107.         list = new List(ListNumberingType.ZAPF_DINGBATS_4);
  108.         list.add("Dr. Jekyll");
  109.         list.add("Mr. Hyde");
  110.         document.add(list);
  111.         //Close document
  112.         document.close();
  113.     }
  114. }
C04E08_CustomListSymbols.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.color.Color;
  10. import com.itextpdf.kernel.font.PdfFont;
  11. import com.itextpdf.kernel.font.PdfFontFactory;
  12. import com.itextpdf.kernel.geom.PageSize;
  13. import com.itextpdf.kernel.geom.Rectangle;
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.ColumnDocumentRenderer;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Image;
  19. import com.itextpdf.layout.element.List;
  20. import com.itextpdf.layout.element.Text;
  21. import com.itextpdf.layout.property.ListNumberingType;
  22. import com.itextpdf.layout.property.ListSymbolAlignment;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C04E08_CustomListSymbols {
  32.  
  33.     public static final String DEST = "results/chapter04/custom_list_symbols.pdf";
  34.     public static final String INFO = "src/main/resources/img/test/info.png";
  35.  
  36.     public static void main(String args[]) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new C04E08_CustomListSymbols().createPdf(DEST);
  40.     }
  41.  
  42.     public void createPdf(String dest) throws IOException {
  43.         //Initialize PDF document
  44.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  45.  
  46.         // Initialize document
  47.         PageSize pagesize = PageSize.A6.rotate();
  48.         Document document = new Document(pdf, pagesize);
  49.         //Set column parameters
  50.         float offSet = 36;
  51.         float gutter = 23;
  52.         float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
  53.         float columnHeight = pagesize.getHeight() - offSet * 2;
  54.  
  55.         //Define column areas
  56.         Rectangle[] columns = {
  57.             new Rectangle(offSet, offSet, columnWidth, columnHeight),
  58.             new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
  59.         document.setRenderer(new ColumnDocumentRenderer(document, columns));    
  60.  
  61.         List list = new List();
  62.         list.setListSymbol("\u2022");
  63.         list.add("Dr. Jekyll");
  64.         list.add("Mr. Hyde");
  65.         document.add(list);
  66.  
  67.         list = new List();
  68.         PdfFont font = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);
  69.         list.setListSymbol(new Text("*").setFont(font).setFontColor(Color.ORANGE));
  70.         list.setSymbolIndent(10);
  71.         list.add("Dr. Jekyll");
  72.         list.add("Mr. Hyde");
  73.         document.add(list);
  74.  
  75.         Image info = new Image(ImageDataFactory.create(INFO));
  76.         info.scaleAbsolute(12, 12);
  77.         list = new List().setSymbolIndent(3);
  78.         list.setListSymbol(info);
  79.         list.add("Dr. Jekyll");
  80.         list.add("Mr. Hyde");
  81.         document.add(list);
  82.  
  83.         list = new List();
  84.         list.setListSymbol(ListNumberingType.ENGLISH_LOWER);
  85.         list.setPostSymbolText("- ");
  86.         list.add("Dr. Jekyll");
  87.         list.add("Mr. Hyde");
  88.         document.add(list);
  89.  
  90.         list = new List(ListNumberingType.DECIMAL);
  91.         list.setPreSymbolText("Part ");
  92.         list.setPostSymbolText(": ");
  93.         list.add("Dr. Jekyll");
  94.         list.add("Mr. Hyde");
  95.         document.add(list);
  96.  
  97.         list = new List(ListNumberingType.DECIMAL);
  98.         list.setItemStartIndex(5);
  99.         list.add("Dr. Jekyll");
  100.         list.add("Mr. Hyde");
  101.         document.add(list);
  102.  
  103.         list = new List(ListNumberingType.ROMAN_LOWER);
  104.         list.setListSymbolAlignment(ListSymbolAlignment.LEFT);
  105.         for (int i = 0; i < 6; i++) {
  106.             list.add("Dr. Jekyll");
  107.             list.add("Mr. Hyde");
  108.         }
  109.         document.add(list);
  110.         //Close document
  111.         document.close();
  112.     }
  113. }
C04E09_ListItemExample.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.highlevel.util.CsvTo2DList;
  8. import com.itextpdf.io.image.ImageDataFactory;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.kernel.pdf.action.PdfAction;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Image;
  14. import com.itextpdf.layout.element.Link;
  15. import com.itextpdf.layout.element.ListItem;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.layout.property.ListNumberingType;
  18. import com.itextpdf.test.annotations.WrapToTest;
  19.  
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.List;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class C04E09_ListItemExample {
  29.  
  30.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  31.     public static final String DEST = "results/chapter04/jekyll_hyde_overviewV4.pdf";
  32.  
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C04E09_ListItemExample().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException {
  40.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  41.         Document document = new Document(pdf);
  42.         List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
  43.         resultSet.remove(0);
  44.         com.itextpdf.layout.element.List list =
  45.             new com.itextpdf.layout.element.List(ListNumberingType.DECIMAL);
  46.         for (List<String> record : resultSet) {
  47.             ListItem li = new ListItem();
  48.             li.setKeepTogether(true);
  49.             String url = String.format(
  50.                 "http://www.imdb.com/title/tt%s", record.get(0));
  51.             Link movie = new Link(record.get(2), PdfAction.createURI(url));
  52.             li.add(new Paragraph(movie.setFontSize(14)))
  53.                 .add(new Paragraph(String.format(
  54.                     "Directed by %s (%s, %s)",
  55.                     record.get(3), record.get(4), record.get(1))));
  56.             File file = new File(String.format(
  57.                 "src/main/resources/img/%s.jpg", record.get(0)));
  58.             if (file.exists()) {
  59.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  60.                 img.scaleToFit(10000, 120);
  61.                 li.add(img);
  62.             }
  63.             list.add(li);
  64.         }
  65.         document.add(list);
  66.         document.close();
  67.     }
  68.  
  69. }
C04E10_NestedLists.java
  1. /*
  2.  * This example was written by Bruno Lowagie
  3.  * in the context of the book: iText 7 building blocks
  4.  */
  5. package com.itextpdf.highlevel.chapter04;
  6.  
  7. import com.itextpdf.kernel.geom.PageSize;
  8. import com.itextpdf.kernel.geom.Rectangle;
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.layout.ColumnDocumentRenderer;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.List;
  14. import com.itextpdf.layout.element.ListItem;
  15. import com.itextpdf.layout.property.ListNumberingType;
  16. import com.itextpdf.test.annotations.WrapToTest;
  17. import java.io.File;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class C04E10_NestedLists {
  25.  
  26.     public static final String DEST = "results/chapter04/nested_list.pdf";
  27.  
  28.     public static void main(String args[]) throws IOException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new C04E10_NestedLists().createPdf(DEST);
  32.     }
  33.  
  34.     public void createPdf(String dest) throws IOException {
  35.         //Initialize PDF document
  36.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  37.  
  38.         // Initialize document
  39.         PageSize pagesize = PageSize.A6.rotate();
  40.         Document document = new Document(pdf, pagesize);
  41.         //Set column parameters
  42.         float offSet = 36;
  43.         float gutter = 23;
  44.         float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
  45.         float columnHeight = pagesize.getHeight() - offSet * 2;
  46.  
  47.         //Define column areas
  48.         Rectangle[] columns = {
  49.             new Rectangle(offSet, offSet, columnWidth, columnHeight),
  50.             new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
  51.         document.setRenderer(new ColumnDocumentRenderer(document, columns));    
  52.  
  53.         List list = new List();
  54.  
  55.         List list1 = new List(ListNumberingType.DECIMAL);
  56.         List listEL = new List(ListNumberingType.ENGLISH_LOWER);
  57.         listEL.add("Dr. Jekyll");
  58.         listEL.add("Mr. Hyde");
  59.         ListItem liEL = new ListItem();
  60.         liEL.add(listEL);
  61.         list1.add(liEL);
  62.         List listEU = new List(ListNumberingType.ENGLISH_UPPER);
  63.         listEU.add("Dr. Jekyll");
  64.         listEU.add("Mr. Hyde");
  65.         ListItem liEU = new ListItem();
  66.         liEU.add(listEU);
  67.         list1.add(liEU);
  68.         ListItem li1 = new ListItem();
  69.         li1.add(list1);
  70.         list.add(li1);
  71.  
  72.         ListItem li = new ListItem();
  73.         List listGL = new List(ListNumberingType.GREEK_LOWER);
  74.         listGL.add("Dr. Jekyll");
  75.         listGL.add("Mr. Hyde");
  76.         li.add(listGL);
  77.         List listGU = new List(ListNumberingType.GREEK_UPPER);
  78.         listGU.add("Dr. Jekyll");
  79.         listGU.add("Mr. Hyde");
  80.         li.add(listGU);
  81.         List listRL = new List(ListNumberingType.ROMAN_LOWER);
  82.         listRL.add("Dr. Jekyll");
  83.         listRL.add("Mr. Hyde");
  84.         li.add(listRL);
  85.         List listRU = new List(ListNumberingType.ROMAN_UPPER);
  86.         listRU.add("Dr. Jekyll");
  87.         listRU.add("Mr. Hyde");
  88.         li.add(listRU);
  89.         list.add(li);
  90.  
  91.         List listZ1 = new List(ListNumberingType.ZAPF_DINGBATS_1);
  92.         listZ1.add("Dr. Jekyll");
  93.         listZ1.add("Mr. Hyde");
  94.         ListItem liZ1 = new ListItem();
  95.         liZ1.add(listZ1);
  96.  
  97.         List listZ2 = new List(ListNumberingType.ZAPF_DINGBATS_2);
  98.         listZ2.add("Dr. Jekyll");
  99.         listZ2.add("Mr. Hyde");
  100.         ListItem liZ2 = new ListItem();
  101.         liZ2.add(listZ2);
  102.  
  103.         List listZ3 = new List(ListNumberingType.ZAPF_DINGBATS_3);
  104.         listZ3.add("Dr. Jekyll");
  105.         listZ3.add("Mr. Hyde");
  106.         ListItem liZ3 = new ListItem();
  107.         liZ3.add(listZ3);
  108.  
  109.         List listZ4 = new List(ListNumberingType.ZAPF_DINGBATS_4);
  110.         listZ4.add("Dr. Jekyll");
  111.         listZ4.add("Mr. Hyde");
  112.         ListItem liZ4 = new ListItem();
  113.         liZ4.add(listZ4);
  114.  
  115.         listZ3.add(liZ4);
  116.         listZ2.add(liZ3);
  117.         listZ1.add(liZ2);
  118.         list.add(liZ1);
  119.         document.add(list);
  120.         //Close document
  121.         document.close();
  122.     }
  123. }
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