Hello World

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

1st September 2016
iText PDF

Switch code for this example

HelloWorld.java
  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 5 version of one of the examples.
  6.  */
  7. package javaone.edition16;
  8.  
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class HelloWorld {
  21.  
  22.     public static final String DEST
  23.         = "results/javaone/edition16/hello.pdf";
  24.  
  25.     public static void main(String[] args)
  26.         throws DocumentException, IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new HelloWorld().createPdf(DEST);
  30.     }
  31.  
  32.     public void createPdf(String dest)
  33.         throws DocumentException, IOException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(
  36.             document, new FileOutputStream(dest));
  37.         document.open();
  38.         document.add(new Paragraph("Hello World!"));
  39.         document.close();
  40.     }
  41. }
HelloWorld1.java
  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 7 version of one of the examples.
  6.  */
  7. package com.itextpdf.sandbox.javaone16;
  8.  
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.Paragraph;
  13. import com.itextpdf.test.annotations.WrapToTest;
  14. import java.io.File;
  15. import java.io.IOException;
  16.  
  17. @WrapToTest
  18. public class HelloWorld1 {
  19.  
  20.     public static final String DEST
  21.         = "results/javaone16/hello1.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new HelloWorld1().createPdf(DEST);
  27.     }
  28.    
  29.     public void createPdf(String dest) throws IOException {
  30.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  31.         Document document = new Document(pdf);
  32.         document.add(new Paragraph("Hello World!"));
  33.         document.close();
  34.     }
  35. }
HelloWorld2.java
  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 7 version of one of the examples.
  6.  */
  7. package com.itextpdf.sandbox.javaone16;
  8.  
  9. import com.itextpdf.kernel.pdf.PdfDocument;
  10. import com.itextpdf.kernel.pdf.PdfWriter;
  11. import com.itextpdf.layout.Document;
  12. import com.itextpdf.layout.element.Paragraph;
  13. import com.itextpdf.test.annotations.WrapToTest;
  14. import java.io.File;
  15. import java.io.IOException;
  16.  
  17. @WrapToTest
  18. public class HelloWorld2 {
  19.  
  20.     public static final String DEST
  21.         = "results/javaone16/hello2.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new HelloWorld2().createPdf(DEST);
  27.     }
  28.    
  29.     public void createPdf(String dest) throws IOException {
  30.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  31.         try (Document document = new Document(pdf)) {
  32.             document.add(new Paragraph("Hello World!"));
  33.         }
  34.     }
  35. }
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