Chapter 1: Introducing PDF and iText

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

10th October 2015
admin-marketing

Switch code for this example

HelloWorld.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.  
  18. /**
  19.  * First iText example: Hello World.
  20.  */
  21. public class HelloWorld {
  22.  
  23.     /** Path to the resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part1/chapter01/hello.pdf";
  26.    
  27.     /**
  28.      * Creates a PDF file: hello.pdf
  29.      * @param    args    no arguments needed
  30.      */
  31.     public static void main(String[] args)
  32.         throws DocumentException, IOException {
  33.         new HelloWorld().createPdf(RESULT);
  34.     }
  35.  
  36.     /**
  37.      * Creates a PDF document.
  38.      * @param filename the path to the new PDF document
  39.      * @throws    DocumentException
  40.      * @throws    IOException
  41.      */
  42.     public void createPdf(String filename)
  43.     throws DocumentException, IOException {
  44.         // step 1
  45.         Document document = new Document();
  46.         // step 2
  47.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         document.add(new Paragraph("Hello World!"));
  52.         // step 5
  53.         document.close();
  54.     }
  55. }
HelloWorldNarrow.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.Rectangle;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * Hello World: document constructor.
  21.  */
  22. public class HelloWorldNarrow {
  23.  
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_narrow.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_narrow.pdf
  30.      * @param    args    no arguments needed
  31.      */
  32.     public static void main(String[] args)
  33.          throws DocumentException, IOException {
  34.         // step 1
  35.         // Using a custom page size
  36.         Rectangle pagesize = new Rectangle(216f, 720f);
  37.         Document document = new Document(pagesize, 36f, 72f, 108f, 180f);
  38.         // step 2
  39.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         document.add(new Paragraph(
  44.             "Hello World! Hello People! " +
  45.             "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));
  46.         // step 5
  47.         document.close();
  48.     }
  49. }
HelloWorldMaximum.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.Rectangle;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * Creates a PDF with the biggest possible page size.
  21.  */
  22. public class HelloWorldMaximum {
  23.  
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_maximum.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_maximum.pdf
  30.      * Important notice: the PDF is valid (in conformance with
  31.      * ISO-32000), but some PDF viewers won't be able to render
  32.      * the PDF correctly due to their own limitations.
  33.      * @param    args    no arguments needed
  34.      */
  35.     public static void main(String[] args)
  36.         throws DocumentException, IOException {
  37.         // step 1
  38.         // maximum page size
  39.         Document document = new Document(new Rectangle(14400, 14400));
  40.         // step 2
  41.         PdfWriter writer =
  42.             PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  43.         // changes the user unit
  44.         writer.setUserunit(75000f);
  45.         // step 3
  46.         document.open();
  47.         // step 4
  48.         document.add(new Paragraph("Hello World"));
  49.         // step 5
  50.         document.close();
  51.     }
  52. }
HelloWorldLetter.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.PageSize;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * Hello World example using the paper size Letter.
  21.  */
  22. public class HelloWorldLetter {
  23.    
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_letter.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_letter.pdf.
  30.      * @param    args    no arguments needed
  31.      */
  32.     public static void main(String[] args) throws DocumentException, IOException {
  33.         // step 1
  34.         // Specifying the page size
  35.         Document document = new Document(PageSize.LETTER);
  36.         // step 2
  37.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  38.         // step 3
  39.         document.open();
  40.         // step 4
  41.         document.add(new Paragraph("Hello World"));
  42.         // step 5
  43.         document.close();
  44.     }
  45. }
HelloWorldLandscape1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.PageSize;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * A possible way to create a document in the landscape format.
  21.  */
  22. public class HelloWorldLandscape1 {
  23.  
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_landscape1.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_landscape1.pdf
  30.      * @param    args    no arguments needed
  31.      */
  32.     public static void main(String[] args)
  33.         throws DocumentException, IOException {
  34.         // step 1
  35.         // landscape format using the rotate() method
  36.         Document document = new Document(PageSize.LETTER.rotate());
  37.         // step 2
  38.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         document.add(new Paragraph("Hello World"));
  43.         // step 5
  44.         document.close();
  45.     }
  46. }
HelloWorldLandscape2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.*;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. /**
  17.  * A possible way to create a document in the landscape format.
  18.  */
  19. public class HelloWorldLandscape2 {
  20.  
  21.     /** Path to the resulting PDF file. */
  22.     public static final String RESULT
  23.         = "results/part1/chapter01/hello_landscape2.pdf";
  24.    
  25.     /**
  26.      * Creates a PDF file: hello_landscape2.pdf
  27.      * @param    args    no arguments needed
  28.      */
  29.     public static void main(String[] args)
  30.         throws DocumentException, IOException {
  31.         // step 1
  32.         // landscape format because width > height
  33.         Document document = new Document(new Rectangle(792, 612));
  34.         // step 2
  35.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  36.         // step 3
  37.         document.open();
  38.         // step 4
  39.         document.add(new Paragraph("Hello World"));
  40.         // step 5
  41.         document.close();
  42.     }
  43. }
HelloWorldMirroredMargins.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.Element;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. /**
  21.  * Examples with mirrored margins.
  22.  */
  23. public class HelloWorldMirroredMargins {
  24.  
  25.     /** Path to the resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part1/chapter01/hello_mirrored_margins.pdf";
  28.  
  29.     /**
  30.      * Creates a PDF file: hello_mirrored_margins.pdf
  31.      * @param    args    no arguments needed
  32.      */
  33.     public static void main(String[] args)
  34.         throws DocumentException, IOException {
  35.         // step 1
  36.         Document document = new Document();
  37.         // step 2
  38.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  39.         document.setPageSize(PageSize.A5);
  40.         document.setMargins(36, 72, 108, 180);
  41.         document.setMarginMirroring(true);
  42.         // step 3
  43.         document.open();
  44.         // step 4
  45.         document.add(new Paragraph(
  46.             "The left margin of this odd page is 36pt (0.5 inch); " +
  47.             "the right margin 72pt (1 inch); " +
  48.             "the top margin 108pt (1.5 inch); " +
  49.             "the bottom margin 180pt (2.5 inch)."));
  50.         Paragraph paragraph = new Paragraph();
  51.         paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
  52.         for (int i = 0; i < 20; i++) {
  53.             paragraph.add("Hello World! Hello People! " +
  54.                     "Hello Sky! Hello Sun! Hello Moon! Hello Stars!");
  55.         }
  56.         document.add(paragraph);
  57.         document.add(new Paragraph(
  58.             "The right margin of this even page is 36pt (0.5 inch); " +
  59.             "the left margin 72pt (1 inch)."));
  60.         // step 5
  61.         document.close();
  62.     }
  63. }
HelloWorldMirroredMarginsTop.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.Element;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. /**
  21.  * Examples with mirrored margins.
  22.  */
  23. public class HelloWorldMirroredMarginsTop {
  24.  
  25.     /** Path to the resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part1/chapter01/hello_mirrored_top.pdf";
  28.  
  29.     /**
  30.      * Creates a PDF file: hello_mirrored_margins.pdf
  31.      * @param    args    no arguments needed
  32.      */
  33.     public static void main(String[] args)
  34.         throws DocumentException, IOException {
  35.         // step 1
  36.         Document document = new Document();
  37.         // step 2
  38.         PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  39.         // setting page size, margins and mirrered margins
  40.         document.setPageSize(PageSize.A5);
  41.         document.setMargins(36, 72, 108, 180);
  42.         document.setMarginMirroringTopBottom(true);
  43.         // step 3
  44.         document.open();
  45.         // step 4
  46.         document.add(new Paragraph(
  47.             "The left margin of this odd page is 36pt (0.5 inch); " +
  48.             "the right margin 72pt (1 inch); " +
  49.             "the top margin 108pt (1.5 inch); " +
  50.             "the bottom margin 180pt (2.5 inch)."));
  51.         Paragraph paragraph = new Paragraph();
  52.         paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
  53.         for (int i = 0; i < 20; i++) {
  54.             paragraph.add("Hello World! Hello People! " +
  55.                 "Hello Sky! Hello Sun! Hello Moon! Hello Stars!");
  56.         }
  57.         document.add(paragraph);
  58.         document.add(new Paragraph("The top margin 180pt (2.5 inch)."));
  59.         // step 5
  60.         document.close();
  61.     }
  62. }
HelloWorldMemory.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * Creates a PDF file in memory.
  21.  */
  22. public class HelloWorldMemory {
  23.  
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_memory.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_memory.pdf
  30.      * @param    args    no arguments needed
  31.      */
  32.     public static void main(String[] args)
  33.         throws DocumentException, IOException {
  34.         // step 1
  35.         Document document = new Document();
  36.         // step 2
  37.         // we'll create the file in memory
  38.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  39.         PdfWriter.getInstance(document, baos);
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         document.add(new Paragraph("Hello World!"));
  44.         // step 5
  45.         document.close();
  46.    
  47.         // let's write the file in memory to a file anyway
  48.         FileOutputStream fos = new FileOutputStream(RESULT);
  49.         fos.write(baos.toByteArray());
  50.         fos.close();
  51.     }
  52. }
HelloWorldVersion_1_7.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.  
  18. /**
  19.  * Creates a Hello World in PDF version 1.7
  20.  */
  21. public class HelloWorldVersion_1_7 {
  22.  
  23.     /** Path to the resulting PDF file. */
  24.     public static final String RESULT =
  25.         "results/part1/chapter01/hello_1_7.pdf";
  26.    
  27.     /**
  28.      * Creates a PDF file: hello.pdf
  29.      * @param    args    no arguments needed
  30.      */
  31.     public static void main(String[] args)
  32.         throws DocumentException, IOException {
  33.         // step 1
  34.         Document document = new Document();
  35.         // step 2
  36.         // Creating a PDF 1.7 document
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  38.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         document.add(new Paragraph("Hello World!"));
  43.         // step 5
  44.         document.close();
  45.     }
  46. }
HelloWorldDirect.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.pdf.BaseFont;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. /**
  20.  * First iText example: Hello World.
  21.  */
  22. public class HelloWorldDirect {
  23.  
  24.     /** Path to the resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part1/chapter01/hello_direct.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF file: hello_direct.pdf
  30.      * @param    args    no arguments needed
  31.      */
  32.     public static void main(String[] args)
  33.         throws DocumentException, IOException {
  34.         // step 1
  35.         Document document = new Document();
  36.         // step 2
  37.         PdfWriter writer =
  38.             PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         PdfContentByte canvas = writer.getDirectContentUnder();
  43.         writer.setCompressionLevel(0);
  44.         canvas.saveState();                               // q
  45.         canvas.beginText();                               // BT
  46.         canvas.moveText(36, 788);                         // 36 788 Td
  47.         canvas.setFontAndSize(BaseFont.createFont(), 12); // /F1 12 Tf
  48.         canvas.showText("Hello World");                   // (Hello World)Tj
  49.         canvas.endText();                                 // ET
  50.         canvas.restoreState();                            // Q
  51.         // step 5
  52.         document.close();
  53.     }
  54. }
HelloWorldColumn.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  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.Element;
  16. import com.itextpdf.text.Phrase;
  17. import com.itextpdf.text.pdf.ColumnText;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. /**
  22.  * First iText example: Hello World.
  23.  */
  24. public class HelloWorldColumn {
  25.  
  26.     /** Path to the resulting PDF file. */
  27.     public static final String RESULT
  28.         = "results/part1/chapter01/hello_column.pdf";
  29.    
  30.     /**
  31.      * Creates a PDF file: hello_column.pdf
  32.      * @param args no arguments needed
  33.      */
  34.     public static void main(String[] args)
  35.         throws DocumentException, IOException {
  36.         // step 1
  37.         Document document = new Document();
  38.         // step 2
  39.         PdfWriter writer
  40.             = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         // we set the compression to 0 so that we can read the PDF syntax
  45.         writer.setCompressionLevel(0);
  46.         // writes something to the direct content using a convenience method
  47.         Phrase hello = new Phrase("Hello World");
  48.         PdfContentByte canvas = writer.getDirectContentUnder();
  49.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  50.             hello, 36, 788, 0);
  51.         // step 5
  52.         document.close();
  53.     }
  54. }
HelloZip.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part1.chapter01;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.util.zip.ZipEntry;
  13. import java.util.zip.ZipOutputStream;
  14.  
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. /**
  21.  * Writing one PDF file to a ZipOutputStream.
  22.  */
  23. public class HelloZip {
  24.    
  25.     /** Path to the resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part1/chapter01/hello.zip";
  28.    
  29.     /**
  30.      * Creates a zip file with five PDF documents:
  31.      * hello1.pdf to hello5.pdf
  32.      * @param    args    no arguments needed
  33.      */
  34.     public static void main(String[] args)
  35.         throws DocumentException, IOException {
  36.         // creating a zip file with different PDF documents
  37.         ZipOutputStream zip =
  38.             new ZipOutputStream(new FileOutputStream(RESULT));
  39.         for (int i = 1; i
HelloWorld.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13.  
  14. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  15.   public class HelloWorld : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         document.Add(new Paragraph("Hello World!"));
  26.       }          
  27.     }
  28. // ===========================================================================
  29.   }
  30. }
HelloWorldNarrow.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * Creates a PDF file: hello_narrow.pdf
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldNarrow : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       // Using a custom page size
  21.       Rectangle pagesize = new Rectangle(216f, 720f);
  22.       using (Document document = new Document(pagesize, 36f, 72f, 108f, 180f)) {
  23.         // step 2
  24.         PdfWriter.GetInstance(document, stream);
  25.         // step 3
  26.         document.Open();
  27.         // step 4
  28.         document.Add(new Paragraph(
  29.           "Hello World! Hello People! " +
  30.           "Hello Sky! Hello Sun! Hello Moon! Hello Stars!")
  31.         );
  32.       }
  33.     }
  34. // ===========================================================================
  35.   }
  36. }
HelloWorldMaximum.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * Creates a PDF with the biggest possible page size.
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldMaximum : IWriter {
  17. // ===========================================================================
  18.     /**
  19.      * Creates a PDF file: hello_maximum.pdf
  20.      * Important notice: the PDF is valid (in conformance with
  21.      * ISO-32000), but some PDF viewers won't be able to render
  22.      * the PDF correctly due to their own limitations.
  23.      */
  24.     public void Write(Stream stream) {
  25.       // step 1
  26.       // Specifying the page size
  27.       using (Document document = new Document(new Rectangle(14400, 14400))) {
  28.         // step 2        
  29.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  30.         // changes the user unit
  31.         writer.Userunit = 75000f;        
  32.         // step 3
  33.         document.Open();
  34.         // step 4
  35.         document.Add(new Paragraph("HelloWorldMaximum"));
  36.       }
  37.     }
  38. // ===========================================================================
  39.   }
  40. }
HelloWorldLetter.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * Hello World example using the paper size Letter.
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldLetter : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       // Specifying the page size
  21.       using (Document document = new Document(PageSize.LETTER)) {
  22.         // step 2
  23.         PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         document.Add(new Paragraph("HelloWorldLetter"));
  28.       }
  29.     }
  30. // ===========================================================================
  31.   }
  32. }
HelloWorldLandscape1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * A possible way to create a document in the landscape format.
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldLandscape1 : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       // landscape format using the rotate() method
  21.       using (Document document = new Document(PageSize.LETTER.Rotate())) {
  22.         // step 2
  23.         PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         document.Add(new Paragraph("HelloWorldLandscape1"));
  28.       }
  29.     }
  30. // ===========================================================================
  31.   }
  32. }
HelloWorldLandscape2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * A possible way to create a document in the landscape format.
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldLandscape2 : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       // landscape format because width > height
  21.       using (Document document = new Document(new Rectangle(792, 612))) {
  22.         // step 2
  23.         PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         document.Add(new Paragraph("HelloWorldLandscape2"));
  28.       }
  29.     }
  30. // ===========================================================================
  31.   }
  32. }
HelloWorldMirroredMargins.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11. /**
  12.  * Examples with mirrored margins.
  13.  */
  14. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  15.   public class HelloWorldMirroredMargins : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter.GetInstance(document, stream);
  22.         document.SetPageSize(PageSize.A5);
  23.         document.SetMargins(36, 72, 108, 180);
  24.         document.SetMarginMirroring(true);        
  25.         // step 3
  26.         document.Open();
  27.         // step 4
  28.         document.Add(new Paragraph(
  29.           "The left margin of this odd page is 36pt (0.5 inch); " +
  30.           "the right margin 72pt (1 inch); " +
  31.           "the top margin 108pt (1.5 inch); " +
  32.           "the bottom margin 180pt (2.5 inch).")
  33.         );
  34.         Paragraph paragraph = new Paragraph();
  35.         paragraph.Alignment = Element.ALIGN_JUSTIFIED;
  36.         for (int i = 0; i < 20; i++) {
  37.           paragraph.Add("Hello World! Hello People! " +
  38.           "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"
  39.           );
  40.         }
  41.         document.Add(paragraph);
  42.         document.Add(new Paragraph(
  43.           "The right margin of this even page is 36pt (0.5 inch); " +
  44.           "the left margin 72pt (1 inch).")
  45.         );
  46.       }
  47.     }
  48. // ===========================================================================
  49.   }
  50. }
HelloWorldMirroredMarginsTop.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * Examples with mirrored margins.
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldMirroredMarginsTop : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       // Specifying the page size
  21.       using (Document document = new Document(PageSize.LETTER)) {
  22.         // step 2
  23.         PdfWriter.GetInstance(document, stream);
  24.         // setting page size, margins and mirrered margins
  25.         document.SetPageSize(PageSize.A5);
  26.         document.SetMargins(36, 72, 108, 180);
  27.         document.SetMarginMirroringTopBottom(true);        
  28.         // step 3
  29.         document.Open();
  30.         // step 4
  31.         document.Add(new Paragraph(
  32.           "The left margin of this odd page is 36pt (0.5 inch); " +
  33.           "the right margin 72pt (1 inch); " +
  34.           "the top margin 108pt (1.5 inch); " +
  35.           "the bottom margin 180pt (2.5 inch).")
  36.         );
  37.         Paragraph paragraph = new Paragraph();
  38.         paragraph.Alignment = Element.ALIGN_JUSTIFIED;
  39.         for (int i = 0; i < 20; i++) {
  40.           paragraph.Add("Hello World! Hello People! " +
  41.             "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"
  42.           );
  43.         }
  44.         document.Add(paragraph);
  45.         document.Add(new Paragraph("The top margin 180pt (2.5 inch)."));
  46.       }
  47.     }
  48. // ===========================================================================
  49.   }
  50. }
HelloWorldMemory.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using System.Web;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12.  
  13. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  14.   public class HelloWorldMemory : IWriter {
  15. // ===========================================================================
  16.     public void Write(Stream stream) {
  17.       HttpContext current = HttpContext.Current;
  18. // running from: [1] web context or [2] command line?
  19.       if (current != null) {
  20. // [1]      
  21.         using (MemoryStream ms = new MemoryStream()) {
  22.           // step 1
  23.           using (Document document = new Document()) {
  24.             // step 2
  25.             PdfWriter.GetInstance(document, ms);
  26.             // step 3
  27.             document.Open();
  28.             // step 4
  29.             document.Add(new Paragraph("HelloWorldMemory"));      
  30.           }
  31.           HttpContext.Current.Response.BinaryWrite(ms.ToArray());
  32.         }
  33.       }
  34. // [2]      
  35.       else {
  36.         // step 1
  37.         using (Document document = new Document()) {
  38.           // step 2
  39.           PdfWriter.GetInstance(document, stream);
  40.           // step 3
  41.           document.Open();
  42.           // step 4
  43.           document.Add(new Paragraph("HelloWorldMemory"));      
  44.         }
  45.       }
  46.     }
  47. // ===========================================================================
  48.   }
  49. }
HelloWorldVersion_1_7.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. /**
  13.  * Creates a Hello World in PDF version 1.7
  14.  */
  15. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  16.   public class HelloWorldVersion_1_7 : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document()) {
  21.         // step 2
  22.         // Creating a PDF 1.7 document
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         writer.PdfVersion = PdfWriter.VERSION_1_7;        
  25.         // step 3
  26.         document.Open();
  27.         // step 4
  28.         document.Add(new Paragraph("HelloWorldVersion_1_7"));
  29.       }
  30.     }
  31. // ===========================================================================
  32.   }
  33. }
HelloWorldDirect.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  13.   public class HelloWorldDirect : IWriter {
  14. // ===========================================================================
  15.     public void Write(Stream stream) {
  16.       // step 1
  17.       using (Document document = new Document()) {
  18.         // step 2
  19.         var writer = PdfWriter.GetInstance(document, stream);
  20.         // step 3
  21.         document.Open();
  22.         // step 4
  23.         PdfContentByte canvas = writer.DirectContentUnder;
  24.         writer.CompressionLevel =0;
  25.         canvas.SaveState();                               // q
  26.         canvas.BeginText();                               // BT
  27.         canvas.MoveText(36, 788);                         // 36 788 Td
  28.         canvas.SetFontAndSize(BaseFont.CreateFont(), 12); // /F1 12 Tf
  29.         canvas.ShowText("HelloWorldDirect");              // (Hello World)Tj
  30.         canvas.EndText();                                 // ET
  31.         canvas.RestoreState();                            // Q
  32.       }
  33.     }
  34. // ===========================================================================
  35.   }
  36. }
HelloWorldColumn.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using iTextSharp.text;
  10. using iTextSharp.text.pdf;
  11.  
  12. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  13.   public class HelloWorldColumn : IWriter {
  14. // ===========================================================================
  15.     public void Write(Stream stream) {
  16.       // step 1
  17.       using (Document document = new Document()) {
  18.         // step 2
  19.         var writer = PdfWriter.GetInstance(document, stream);
  20.         // step 3
  21.         document.Open();
  22.         // step 4
  23.         // we set the compression to 0 so that we can read the PDF syntax
  24.         writer.CompressionLevel = 0;
  25.         // writes something to the direct content using a convenience method
  26.         Phrase hello = new Phrase("HelloWorldColumn");
  27.         PdfContentByte canvas = writer.DirectContentUnder;
  28.         ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT,
  29.           hello, 36, 788, 0
  30.         );
  31.       }
  32.     }
  33. // ===========================================================================
  34.   }
  35. }
HelloZip.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. /*
  13.  * word of warning - .NET ZIP data compression/archive
  14.  * format implementation is **severely** lacking:
  15.  * [1] only supported in .NET 3.0 and above
  16.  * [2] compression non-existent; see 'Remarks' section when
  17.  *     setting 'CompressionOption' parameter of Package.CreatePart()
  18.  *     http://msdn.microsoft.com/en-us/library/ms568067.aspx
  19.  *
  20.  * so instead we use the excellent DotNetZip library:
  21.  *
  22.  * http://dotnetzip.codeplex.com/
  23. */
  24. namespace kuujinbo.iTextInAction2Ed.Chapter01 {
  25.   public class HelloZip : IWriter {
  26. // ===========================================================================
  27. /*
  28.  * Creates a zip file with three PDF documents:
  29.  * hello1.pdf to hello3.pdf
  30.  */
  31.     public void Write(Stream stream) {
  32.       using (ZipFile zip = new ZipFile()) {
  33.         for (int i = 1; i
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