iText 7 jump-start tutorial : Chapter 1

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

4th April 2016
admin-marketing

Switch code for this example

C01E01_HelloWorld.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter01;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  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.  
  23. /**
  24.  * Simple Hello World example.
  25.  */
  26. @WrapToTest
  27. public class C01E01_HelloWorld {
  28.    
  29.     public static final String DEST = "results/chapter01/hello_world.pdf";
  30.    
  31.     public static void main(String args[]) throws IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new C01E01_HelloWorld().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException {
  38.         //Initialize PDF writer
  39.         PdfWriter writer = new PdfWriter(dest);
  40.  
  41.         //Initialize PDF document
  42.         PdfDocument pdf = new PdfDocument(writer);
  43.        
  44.         // Initialize document
  45.         Document document = new Document(pdf);
  46.  
  47.         //Add paragraph to the document
  48.         document.add(new Paragraph("Hello World!"));
  49.  
  50.         //Close document
  51.         document.close();
  52.     }
  53. }
C01E02_RickAstley.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter01;
  13.  
  14. import com.itextpdf.io.font.constants.StandardFonts;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.List;
  21. import com.itextpdf.layout.element.ListItem;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24.  
  25. import java.io.File;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * Simple List example.
  30.  */
  31. @WrapToTest
  32. public class C01E02_RickAstley {
  33.         public static final String DEST = "results/chapter01/rick_astley.pdf";
  34.    
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C01E02_RickAstley().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF writer
  43.         PdfWriter writer = new PdfWriter(dest);
  44.  
  45.         //Initialize PDF document
  46.         PdfDocument pdf = new PdfDocument(writer);
  47.        
  48.         // Initialize document
  49.         Document document = new Document(pdf);
  50.        
  51.         // Create a PdfFont
  52.         PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
  53.         // Add a Paragraph
  54.         document.add(new Paragraph("iText is:").setFont(font));
  55.         // Create a List
  56.         List list = new List()
  57.             .setSymbolIndent(12)
  58.             .setListSymbol("\u2022")
  59.             .setFont(font);
  60.         // Add ListItem objects
  61.         list.add(new ListItem("Never gonna give you up"))
  62.             .add(new ListItem("Never gonna let you down"))
  63.             .add(new ListItem("Never gonna run around and desert you"))
  64.             .add(new ListItem("Never gonna make you cry"))
  65.             .add(new ListItem("Never gonna say goodbye"))
  66.             .add(new ListItem("Never gonna tell a lie and hurt you"));
  67.         // Add the list
  68.         document.add(list);
  69.  
  70.         //Close document
  71.         document.close();
  72.     }
  73. }
C01E03_QuickBrownFox.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter01;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Image;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.test.annotations.WrapToTest;
  21.  
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * Simple image example.
  27.  */
  28. @WrapToTest
  29. public class C01E03_QuickBrownFox {
  30.     public static final String DOG = "src/main/resources/img/dog.bmp";
  31.     public static final String FOX = "src/main/resources/img/fox.bmp";
  32.        
  33.     public static final String DEST = "results/chapter01/quick_brown_fox.pdf";
  34.    
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C01E03_QuickBrownFox().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF writer
  43.         PdfWriter writer = new PdfWriter(dest);
  44.  
  45.         //Initialize PDF document
  46.         PdfDocument pdf = new PdfDocument(writer);
  47.        
  48.         // Initialize document
  49.         Document document = new Document(pdf);
  50.        
  51.         // Compose Paragraph
  52.         Image fox = new Image(ImageDataFactory.create(FOX));
  53.         Image dog = new Image(ImageDataFactory.create(DOG));
  54.         Paragraph p = new Paragraph("The quick brown ")
  55.                 .add(fox)
  56.                 .add(" jumps over the lazy ")
  57.                 .add(dog);
  58.         // Add Paragraph to document
  59.         document.add(p);
  60.        
  61.         //Close document
  62.         document.close();
  63.     }
  64. }
C01E04_UnitedStates.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter01;
  13.  
  14. import com.itextpdf.io.font.constants.StandardFonts;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.property.UnitValue;
  25. import com.itextpdf.test.annotations.WrapToTest;
  26.  
  27. import java.io.BufferedReader;
  28. import java.io.File;
  29. import java.io.FileReader;
  30. import java.io.IOException;
  31. import java.util.StringTokenizer;
  32.  
  33. /**
  34.  * Simple table example.
  35.  */
  36. @WrapToTest
  37. public class C01E04_UnitedStates {
  38.     public static final String DATA = "src/main/resources/data/united_states.csv";
  39.  
  40.     public static final String DEST = "results/chapter01/united_states.pdf";
  41.  
  42.     public static void main(String args[]) throws IOException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new C01E04_UnitedStates().createPdf(DEST);
  46.     }
  47.  
  48.     public void createPdf(String dest) throws IOException {
  49.         //Initialize PDF writer
  50.         PdfWriter writer = new PdfWriter(dest);
  51.  
  52.         //Initialize PDF document
  53.         PdfDocument pdf = new PdfDocument(writer);
  54.  
  55.         // Initialize document
  56.         Document document = new Document(pdf, PageSize.A4.rotate());
  57.         document.setMargins(20, 20, 20, 20);
  58.  
  59.         PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
  60.         PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
  61.         Table table = new Table(UnitValue.createPercentArray(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1}))
  62.                 .useAllAvailableWidth();
  63.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  64.         String line = br.readLine();
  65.         process(table, line, bold, true);
  66.         while ((line = br.readLine()) != null) {
  67.             process(table, line, font, false);
  68.         }
  69.         br.close();
  70.         document.add(table);
  71.  
  72.         //Close document
  73.         document.close();
  74.     }
  75.  
  76.     public void process(Table table, String line, PdfFont font, boolean isHeader) {
  77.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  78.         while (tokenizer.hasMoreTokens()) {
  79.             if (isHeader) {
  80.                 table.addHeaderCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  81.             } else {
  82.                 table.addCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  83.             }
  84.         }
  85.     }
  86. }
C01E01_HelloWorld.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.Kernel.Pdf;
  7. using iText.Layout;
  8. using iText.Layout.Element;
  9. using iText.Test.Attributes;
  10.  
  11. namespace Tutorial.Chapter01 {
  12.     /// <summary>Simple Hello World example.</summary>
  13.     [WrapToTest]
  14.     public class C01E01_HelloWorld {
  15.         public const String DEST = "../../results/chapter01/hello_world.pdf";
  16.  
  17.         /// <exception cref="System.IO.IOException"/>
  18.         public static void Main(String[] args) {
  19.             FileInfo file = new FileInfo(DEST);
  20.             file.Directory.Create();
  21.             new C01E01_HelloWorld().CreatePdf(DEST);
  22.         }
  23.  
  24.         /// <exception cref="System.IO.IOException"/>
  25.         public virtual void CreatePdf(String dest) {
  26.             //Initialize PDF writer
  27.             PdfWriter writer = new PdfWriter(dest);
  28.             //Initialize PDF document
  29.             PdfDocument pdf = new PdfDocument(writer);
  30.             // Initialize document
  31.             Document document = new Document(pdf);
  32.             //Add paragraph to the document
  33.             document.Add(new Paragraph("Hello World!"));
  34.             //Close document
  35.             document.Close();
  36.         }
  37.     }
  38. }
C01E02_RickAstley.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.IO.Font.Constants;
  7. using iText.Kernel.Font;
  8. using iText.Kernel.Pdf;
  9. using iText.Layout;
  10. using iText.Layout.Element;
  11. using iText.Test.Attributes;
  12.  
  13. namespace Tutorial.Chapter01 {
  14.     /// <summary>Simple List example.</summary>
  15.     [WrapToTest]
  16.     public class C01E02_RickAstley {
  17.         public const String DEST = "../../results/chapter01/rick_astley.pdf";
  18.  
  19.         /// <exception cref="System.IO.IOException"/>
  20.         public static void Main(String[] args) {
  21.             FileInfo file = new FileInfo(DEST);
  22.             if (!file.Directory.Exists) file.Directory.Create();
  23.             new C01E02_RickAstley().CreatePdf(DEST);
  24.         }
  25.  
  26.         /// <exception cref="System.IO.IOException"/>
  27.         public virtual void CreatePdf(String dest) {
  28.             //Initialize PDF writer
  29.             PdfWriter writer = new PdfWriter(dest);
  30.             //Initialize PDF document
  31.             PdfDocument pdf = new PdfDocument(writer);
  32.             // Initialize document
  33.             Document document = new Document(pdf);
  34.             // Create a PdfFont
  35.             PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
  36.             // Add a Paragraph
  37.             document.Add(new Paragraph("iText is:").SetFont(font));
  38.             // Create a List
  39.             List list = new List().SetSymbolIndent(12).SetListSymbol("\u2022").SetFont(font);
  40.             // Add ListItem objects
  41.             list.Add(new ListItem("Never gonna give you up")).Add(new ListItem("Never gonna let you down")).Add(new ListItem
  42.                 ("Never gonna run around and desert you")).Add(new ListItem("Never gonna make you cry")).Add(new ListItem
  43.                 ("Never gonna say goodbye")).Add(new ListItem("Never gonna tell a lie and hurt you"));
  44.             // Add the list
  45.             document.Add(list);
  46.             //Close document
  47.             document.Close();
  48.         }
  49.     }
  50. }
C01E03_QuickBrownFox.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.IO.Image;
  7. using iText.Kernel.Pdf;
  8. using iText.Layout;
  9. using iText.Layout.Element;
  10. using iText.Test.Attributes;
  11.  
  12. namespace Tutorial.Chapter01 {
  13.     /// <summary>Simple image example.</summary>
  14.     [WrapToTest]
  15.     public class C01E03_QuickBrownFox {
  16.         public const String DOG = "../../resources/img/dog.bmp";
  17.  
  18.         public const String FOX = "../../resources/img/fox.bmp";
  19.  
  20.         public const String DEST = "../../results/chapter01/quick_brown_fox.pdf";
  21.  
  22.         /// <exception cref="System.IO.IOException"/>
  23.         public static void Main(String[] args) {
  24.             FileInfo file = new FileInfo(DEST);
  25.             file.Directory.Create();
  26.             new C01E03_QuickBrownFox().CreatePdf(DEST);
  27.         }
  28.  
  29.         /// <exception cref="System.IO.IOException"/>
  30.         public virtual void CreatePdf(String dest) {
  31.             //Initialize PDF writer
  32.             PdfWriter writer = new PdfWriter(dest);
  33.             //Initialize PDF document
  34.             PdfDocument pdf = new PdfDocument(writer);
  35.             // Initialize document
  36.             Document document = new Document(pdf);
  37.             // Compose Paragraph
  38.             iText.Layout.Element.Image fox = new Image(ImageDataFactory.Create(FOX));
  39.             iText.Layout.Element.Image dog = new iText.Layout.Element.Image(ImageDataFactory.Create(DOG));
  40.             Paragraph p = new Paragraph("The quick brown ").Add(fox).Add(" jumps over the lazy ").Add(dog);
  41.             // Add Paragraph to document
  42.             document.Add(p);
  43.             //Close document
  44.             document.Close();
  45.         }
  46.     }
  47. }
C01E04_UnitedStates.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.IO.Font.Constants;
  7. using iText.IO.Util;
  8. using iText.Kernel.Font;
  9. using iText.Kernel.Geom;
  10. using iText.Kernel.Pdf;
  11. using iText.Layout;
  12. using iText.Layout.Element;
  13. using iText.Layout.Properties;
  14. using iText.Test.Attributes;
  15.  
  16. namespace Tutorial.Chapter01 {
  17.     /// <summary>Simple table example.</summary>
  18.     [WrapToTest]
  19.     public class C01E04_UnitedStates {
  20.         public const String DATA = "../../resources/data/united_states.csv";
  21.  
  22.         public const String DEST = "../../results/chapter01/united_states.pdf";
  23.  
  24.         /// <exception cref="System.IO.IOException"/>
  25.         public static void Main(String[] args) {
  26.             FileInfo file = new FileInfo(DEST);
  27.             file.Directory.Create();
  28.             new C01E04_UnitedStates().CreatePdf(DEST);
  29.         }
  30.  
  31.         /// <exception cref="System.IO.IOException"/>
  32.         public virtual void CreatePdf(String dest) {
  33.             //Initialize PDF writer
  34.             PdfWriter writer = new PdfWriter(dest);
  35.             //Initialize PDF document
  36.             PdfDocument pdf = new PdfDocument(writer);
  37.             // Initialize document
  38.             Document document = new Document(pdf, PageSize.A4.Rotate());
  39.             document.SetMargins(20, 20, 20, 20);
  40.             PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
  41.             PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
  42.             Table table = new Table(UnitValue.CreatePercentArray(new float[] { 4, 1, 3, 4, 3, 3, 3, 3, 1 }))
  43.                 .UseAllAvailableWidth();
  44.             StreamReader sr = File.OpenText(DATA);
  45.             String line = sr.ReadLine();
  46.             Process(table, line, bold, true);
  47.             while ((line = sr.ReadLine()) != null) {
  48.                 Process(table, line, font, false);
  49.             }
  50.             sr.Close();
  51.             document.Add(table);
  52.             //Close document
  53.             document.Close();
  54.         }
  55.  
  56.         public virtual void Process(Table table, String line, PdfFont font, bool isHeader) {
  57.             StringTokenizer tokenizer = new StringTokenizer(line, ";");
  58.             while (tokenizer.HasMoreTokens()) {
  59.                 if (isHeader) {
  60.                     table.AddHeaderCell(new Cell().Add(new Paragraph(tokenizer.NextToken()).SetFont(font)));
  61.                 }
  62.                 else {
  63.                     table.AddCell(new Cell().Add(new Paragraph(tokenizer.NextToken()).SetFont(font)));
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
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