Chapter 14: The imaging model

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

10th October 2015
admin-marketing

Switch code for this example

GetContentStream.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 part4.chapter14;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import part1.chapter01.HelloWorld;
  14. import part1.chapter05.Hero1;
  15.  
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.pdf.PdfReader;
  18.  
  19. public class GetContentStream {
  20.     /** The content stream of a first PDF. */
  21.     public static final String RESULT1
  22.         = "results/part4/chapter14/contentstream1.txt";
  23.     /** The content stream of a second PDF. */
  24.     public static final String RESULT2
  25.         = "results/part4/chapter14/contentstream2.txt";
  26.    
  27.     /**
  28.      * Reads the content stream of the first page of a PDF into a text file.
  29.      * @param src    the path to a PDF file
  30.      * @param result the path to the resulting text file
  31.      * @throws IOException
  32.      */
  33.     public void readContent(String src, String result) throws IOException {
  34.         PdfReader reader = new PdfReader(src);
  35.         FileOutputStream out = new FileOutputStream(result);
  36.         out.write(reader.getPageContent(1));
  37.         out.flush();
  38.         out.close();
  39.         reader.close();
  40.     }
  41.    
  42.     /**
  43.      * Main method.
  44.      *
  45.      * @param    args    no arguments needed
  46.      * @throws DocumentException
  47.      * @throws IOException
  48.      */
  49.     public static void main(String[] args) throws IOException, DocumentException {
  50.         new HelloWorld().createPdf(HelloWorld.RESULT);
  51.         new Hero1().createPdf(Hero1.RESULT);
  52.         GetContentStream example = new GetContentStream();
  53.         example.readContent(HelloWorld.RESULT, RESULT1);
  54.         example.readContent(Hero1.RESULT, RESULT2);
  55.     }
  56. }
PathConstructionAndPainting.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Element;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.Rectangle;
  17. import com.itextpdf.text.pdf.ColumnText;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import com.itextpdf.text.pdf.GrayColor;
  21.  
  22. public class PathConstructionAndPainting {
  23.  
  24.     /** The resulting PDF. */
  25.     public static final String RESULT = "results/part4/chapter14/paths.pdf";
  26.  
  27.     /**
  28.      * Creates a PDF document.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      * @throws    DocumentException
  33.      * @throws    IOException
  34.      */
  35.     public void createPdf(String filename) throws IOException, DocumentException {
  36.         // step 1
  37.         Document document = new Document();
  38.         // step 2
  39.         PdfWriter writer = PdfWriter.getInstance(document,
  40.                 new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         PdfContentByte canvas = writer.getDirectContent();
  45.         // draw squares
  46.         createSquares(canvas, 50, 720, 80, 20);
  47.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  48.             new Phrase(
  49.                 "Methods moveTo(), lineTo(), stroke(), closePathStroke(), fill(), and closePathFill()"),
  50.                 50, 700, 0);
  51.         // draw Bezier curves
  52.         createBezierCurves(canvas, 70, 600, 80, 670, 140, 690, 160, 630, 160);
  53.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  54.             new Phrase("Different curveTo() methods, followed by stroke()"), 50, 580, 0);
  55.         // draw stars and circles
  56.         createStarsAndCircles(canvas, 50, 470, 40, 20);
  57.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  58.             new Phrase("Methods fill(), eoFill(), newPath(), fillStroke(), and eoFillStroke()"),
  59.                 50, 450, 0);
  60.         // draw different shapes using convenience methods
  61.         canvas.saveState();
  62.         canvas.setColorStroke(new GrayColor(0.2f));
  63.         canvas.setColorFill(new GrayColor(0.9f));
  64.         canvas.arc(50, 270, 150, 330, 45, 270);
  65.         canvas.ellipse(170, 270, 270, 330);
  66.         canvas.circle(320, 300, 30);
  67.         canvas.roundRectangle(370, 270, 80, 60, 20);
  68.         canvas.fillStroke();
  69.         canvas.restoreState();
  70.         Rectangle rect = new Rectangle(470, 270, 550, 330);
  71.         rect.setBorderWidthBottom(10);
  72.         rect.setBorderColorBottom(new GrayColor(0f));
  73.         rect.setBorderWidthLeft(4);
  74.         rect.setBorderColorLeft(new GrayColor(0.9f));
  75.         rect.setBackgroundColor(new GrayColor(0.4f));
  76.         canvas.rectangle(rect);
  77.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  78.             new Phrase("Convenience methods"), 50, 250, 0);
  79.         // step 5
  80.         document.close();
  81.     }
  82.    
  83.     /**
  84.      * Draws a row of squares.
  85.      * @param canvas the canvas to which the squares have to be drawn
  86.      * @param x      X coordinate to position the row
  87.      * @param y      Y coordinate to position the row
  88.      * @param side   the side of the square
  89.      * @param gutter the space between the squares
  90.      */
  91.     public void createSquares(PdfContentByte canvas,
  92.         float x, float y, float side, float gutter) {
  93.         canvas.saveState();
  94.         canvas.setColorStroke(new GrayColor(0.2f));
  95.         canvas.setColorFill(new GrayColor(0.9f));
  96.         canvas.moveTo(x, y);
  97.         canvas.lineTo(x + side, y);
  98.         canvas.lineTo(x + side, y + side);
  99.         canvas.lineTo(x, y + side);
  100.         canvas.stroke();
  101.         x = x + side + gutter;
  102.         canvas.moveTo(x, y);
  103.         canvas.lineTo(x + side, y);
  104.         canvas.lineTo(x + side, y + side);
  105.         canvas.lineTo(x, y + side);
  106.         canvas.closePathStroke();
  107.         x = x + side + gutter;
  108.         canvas.moveTo(x, y);
  109.         canvas.lineTo(x + side, y);
  110.         canvas.lineTo(x + side, y + side);
  111.         canvas.lineTo(x, y + side);
  112.         canvas.fill();
  113.         x = x + side + gutter;
  114.         canvas.moveTo(x, y);
  115.         canvas.lineTo(x + side, y);
  116.         canvas.lineTo(x + side, y + side);
  117.         canvas.lineTo(x, y + side);
  118.         canvas.fillStroke();
  119.         x = x + side + gutter;
  120.         canvas.moveTo(x, y);
  121.         canvas.lineTo(x + side, y);
  122.         canvas.lineTo(x + side, y + side);
  123.         canvas.lineTo(x, y + side);
  124.         canvas.closePathFillStroke();
  125.         canvas.restoreState();
  126.     }
  127.    
  128.     /**
  129.      * Draws a series of Bezier curves
  130.      * @param cb the canvas to which the curves have to be drawn
  131.      * @param x0 X coordinate of the start point
  132.      * @param y0 Y coordinate of the start point
  133.      * @param x1 X coordinate of the first control point
  134.      * @param y1 Y coordinate of the first control point
  135.      * @param x2 X coordinate of the second control point
  136.      * @param y2 Y coordinate of the second control point
  137.      * @param x3 X coordinate of the end point
  138.      * @param y3 Y coordinate of the end point
  139.      * @param distance the distance between the curves
  140.      */
  141.     public void createBezierCurves(PdfContentByte cb, float x0, float y0,
  142.         float x1, float y1, float x2, float y2, float x3, float y3, float distance) {
  143.         cb.moveTo(x0, y0);
  144.         cb.lineTo(x1, y1);
  145.         cb.moveTo(x2, y2);
  146.         cb.lineTo(x3, y3);
  147.         cb.moveTo(x0, y0);
  148.         cb.curveTo(x1, y1, x2, y2, x3, y3);
  149.         x0 += distance;
  150.         x1 += distance;
  151.         x2 += distance;
  152.         x3 += distance;
  153.         cb.moveTo(x2, y2);
  154.         cb.lineTo(x3, y3);
  155.         cb.moveTo(x0, y0);
  156.         cb.curveTo(x2, y2, x3, y3);
  157.         x0 += distance;
  158.         x1 += distance;
  159.         x2 += distance;
  160.         x3 += distance;
  161.         cb.moveTo(x0, y0);
  162.         cb.lineTo(x1, y1);
  163.         cb.moveTo(x0, y0);
  164.         cb.curveTo(x1, y1, x3, y3);
  165.         cb.stroke();
  166.  
  167.     }
  168.    
  169.     /**
  170.      * Draws a row of stars and circles.
  171.      * @param canvas the canvas to which the shapes have to be drawn
  172.      * @param x      X coordinate to position the row
  173.      * @param y      Y coordinate to position the row
  174.      * @param radius the radius of the circles
  175.      * @param gutter the space between the shapes
  176.      */
  177.     public static void createStarsAndCircles(PdfContentByte canvas,
  178.         float x, float y, float radius, float gutter) {
  179.         canvas.saveState();
  180.         canvas.setColorStroke(new GrayColor(0.2f));
  181.         canvas.setColorFill(new GrayColor(0.9f));
  182.         createStar(canvas, x, y);
  183.         createCircle(canvas, x + radius, y - 70, radius, true);
  184.         createCircle(canvas, x + radius, y - 70, radius / 2, true);
  185.         canvas.fill();
  186.         x += 2 * radius + gutter;
  187.         createStar(canvas, x, y);
  188.         createCircle(canvas, x + radius, y - 70, radius, true);
  189.         createCircle(canvas, x + radius, y - 70, radius / 2, true);
  190.         canvas.eoFill();
  191.         x += 2 * radius + gutter;
  192.         createStar(canvas, x, y);
  193.         canvas.newPath();
  194.         createCircle(canvas, x + radius, y - 70, radius, true);
  195.         createCircle(canvas, x + radius, y - 70, radius / 2, true);
  196.         x += 2 * radius + gutter;
  197.         createStar(canvas, x, y);
  198.         createCircle(canvas, x + radius, y - 70, radius, true);
  199.         createCircle(canvas, x + radius, y - 70, radius / 2, false);
  200.         canvas.fillStroke();
  201.         x += 2 * radius + gutter;
  202.         createStar(canvas, x, y);
  203.         createCircle(canvas, x + radius, y - 70, radius, true);
  204.         createCircle(canvas, x + radius, y - 70, radius / 2, true);
  205.         canvas.eoFillStroke();
  206.         canvas.restoreState();
  207.     }
  208.    
  209.     /**
  210.      * Creates a path for a five pointed star.
  211.      * This method doesn't fill or stroke the star!
  212.      * @param canvas the canvas for which the star is constructed
  213.      * @param x      the X coordinate of the center of the star
  214.      * @param y      the Y coordinate of the center of the star
  215.      */
  216.     public static void createStar(PdfContentByte canvas, float x, float y) {
  217.         canvas.moveTo(x + 10, y);
  218.         canvas.lineTo(x + 80, y + 60);
  219.         canvas.lineTo(x, y + 60);
  220.         canvas.lineTo(x + 70, y);
  221.         canvas.lineTo(x + 40, y + 90);
  222.         canvas.closePath();
  223.     }
  224.  
  225.     /**
  226.      * Creates a path for circle using Bezier curvers.
  227.      * The path can be constructed clockwise or counter-clockwise.
  228.      * This method doesn't fill or stroke the circle!
  229.      * @param canvas    the canvas for which the path is constructed
  230.      * @param x         the X coordinate of the center of the circle
  231.      * @param y         the Y coordinate of the center of the circle
  232.      * @param r         the radius
  233.      * @param clockwise true if the circle has to be constructed clockwise
  234.      */
  235.     public static void createCircle(PdfContentByte canvas, float x, float y,
  236.         float r, boolean clockwise) {
  237.         float b = 0.5523f;
  238.         if (clockwise) {
  239.             canvas.moveTo(x + r, y);
  240.             canvas.curveTo(x + r, y - r * b, x + r * b, y - r, x, y - r);
  241.             canvas.curveTo(x - r * b, y - r, x - r, y - r * b, x - r, y);
  242.             canvas.curveTo(x - r, y + r * b, x - r * b, y + r, x, y + r);
  243.             canvas.curveTo(x + r * b, y + r, x + r, y + r * b, x + r, y);
  244.         } else {
  245.             canvas.moveTo(x + r, y);
  246.             canvas.curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r);
  247.             canvas.curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y);
  248.             canvas.curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r);
  249.             canvas.curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y);
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Main method.
  255.      *
  256.      * @param    args    no arguments needed
  257.      * @throws DocumentException
  258.      * @throws IOException
  259.      */
  260.     public static void main(String[] args) throws IOException, DocumentException {
  261.         new PathConstructionAndPainting().createPdf(RESULT);
  262.     }
  263. }
GraphicsStateOperators.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfTemplate;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class GraphicsStateOperators {
  19.  
  20.     /** The resulting PDF. */
  21.     public static final String RESULT = "results/part4/chapter14/graphics_state.pdf";
  22.  
  23.     /**
  24.      * Creates a PDF document.
  25.      * @param filename the path to the new PDF document
  26.      * @throws DocumentException
  27.      * @throws IOException
  28.      * @throws    DocumentException
  29.      * @throws    IOException
  30.      */
  31.     public void createPdf(String filename) throws IOException, DocumentException {
  32.         // step 1
  33.         Document document = new Document();
  34.         // step 2
  35.         PdfWriter writer = PdfWriter.getInstance(document,
  36.                 new FileOutputStream(filename));
  37.         // step 3
  38.         document.open();
  39.         // step 4
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.        
  42.         // line widths
  43.         canvas.saveState();
  44.         for (int i = 25; i > 0; i--) {
  45.             canvas.setLineWidth((float) i / 10);
  46.             canvas.moveTo(50, 806 - (5 * i));
  47.             canvas.lineTo(320, 806 - (5 * i));
  48.             canvas.stroke();
  49.         }
  50.         canvas.restoreState();
  51.        
  52.         // line cap
  53.         canvas.moveTo(350, 800);
  54.         canvas.lineTo(350, 750);
  55.         canvas.moveTo(540, 800);
  56.         canvas.lineTo(540, 750);
  57.         canvas.stroke();
  58.        
  59.         canvas.saveState();
  60.         canvas.setLineWidth(8);
  61.         canvas.setLineCap(PdfContentByte.LINE_CAP_BUTT);
  62.         canvas.moveTo(350, 790);
  63.         canvas.lineTo(540, 790);
  64.         canvas.stroke();
  65.         canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  66.         canvas.moveTo(350, 775);
  67.         canvas.lineTo(540, 775);
  68.         canvas.stroke();
  69.         canvas.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
  70.         canvas.moveTo(350, 760);
  71.         canvas.lineTo(540, 760);
  72.         canvas.stroke();
  73.         canvas.restoreState();
  74.        
  75.         // join miter
  76.         canvas.saveState();
  77.         canvas.setLineWidth(8);
  78.         canvas.setLineJoin(PdfContentByte.LINE_JOIN_MITER);
  79.         canvas.moveTo(387, 700);
  80.         canvas.lineTo(402, 730);
  81.         canvas.lineTo(417, 700);
  82.         canvas.stroke();
  83.         canvas.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
  84.         canvas.moveTo(427, 700);
  85.         canvas.lineTo(442, 730);
  86.         canvas.lineTo(457, 700);
  87.         canvas.stroke();
  88.         canvas.setLineJoin(PdfContentByte.LINE_JOIN_BEVEL);
  89.         canvas.moveTo(467, 700);
  90.         canvas.lineTo(482, 730);
  91.         canvas.lineTo(497, 700);
  92.         canvas.stroke();
  93.         canvas.restoreState();
  94.  
  95.         // line dash
  96.         canvas.saveState();
  97.         canvas.setLineWidth(3);
  98.         canvas.moveTo(50, 660);
  99.         canvas.lineTo(320, 660);
  100.         canvas.stroke();
  101.         canvas.setLineDash(6, 0);
  102.         canvas.moveTo(50, 650);
  103.         canvas.lineTo(320, 650);
  104.         canvas.stroke();
  105.         canvas.setLineDash(6, 3);
  106.         canvas.moveTo(50, 640);
  107.         canvas.lineTo(320, 640);
  108.         canvas.stroke();
  109.         canvas.setLineDash(15, 10, 5);
  110.         canvas.moveTo(50, 630);
  111.         canvas.lineTo(320, 630);
  112.         canvas.stroke();
  113.         float[] dash1 = { 10, 5, 5, 5, 20 };
  114.         canvas.setLineDash(dash1, 5);
  115.         canvas.moveTo(50, 620);
  116.         canvas.lineTo(320, 620);
  117.         canvas.stroke();
  118.         float[] dash2 = { 9, 6, 0, 6 };
  119.         canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  120.         canvas.setLineDash(dash2, 0);
  121.         canvas.moveTo(50, 610);
  122.         canvas.lineTo(320, 610);
  123.         canvas.stroke();
  124.         canvas.restoreState();
  125.  
  126.         // miter limit
  127.         PdfTemplate hooks = canvas.createTemplate(300, 120);
  128.         hooks.setLineWidth(8);
  129.         hooks.moveTo(46, 50);
  130.         hooks.lineTo(65, 80);
  131.         hooks.lineTo(84, 50);
  132.         hooks.stroke();
  133.         hooks.moveTo(87, 50);
  134.         hooks.lineTo(105, 80);
  135.         hooks.lineTo(123, 50);
  136.         hooks.stroke();
  137.         hooks.moveTo(128, 50);
  138.         hooks.lineTo(145, 80);
  139.         hooks.lineTo(162, 50);
  140.         hooks.stroke();
  141.         hooks.moveTo(169, 50);
  142.         hooks.lineTo(185, 80);
  143.         hooks.lineTo(201, 50);
  144.         hooks.stroke();
  145.         hooks.moveTo(210, 50);
  146.         hooks.lineTo(225, 80);
  147.         hooks.lineTo(240, 50);
  148.         hooks.stroke();
  149.        
  150.         canvas.saveState();
  151.         canvas.setMiterLimit(2);
  152.         canvas.addTemplate(hooks, 300, 600);
  153.         canvas.restoreState();
  154.        
  155.         canvas.saveState();
  156.         canvas.setMiterLimit(2.1f);
  157.         canvas.addTemplate(hooks, 300, 550);
  158.         canvas.restoreState();
  159.         // step 5
  160.         document.close();
  161.     }
  162.        
  163.     /**
  164.      * Main method.
  165.      *
  166.      * @param    args    no arguments needed
  167.      * @throws DocumentException
  168.      * @throws IOException
  169.      */
  170.     public static void main(String[] args) throws IOException, DocumentException {
  171.         new GraphicsStateOperators().createPdf(RESULT);
  172.     }
  173. }
TransformationMatrix1.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfReader;
  17. import com.itextpdf.text.pdf.PdfTemplate;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class TransformationMatrix1 {
  21.  
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/transformation_matrix1.pdf";
  24.     /** A PDF with the iText logo that will be transformed. */
  25.     public static final String RESOURCE = "resources/pdfs/logo.pdf";
  26.    
  27.     /**
  28.      * Creates a PDF document.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public void createPdf(String filename) throws IOException, DocumentException {
  34.         // step 1
  35.         Rectangle rect = new Rectangle(-595, -842, 595, 842);
  36.         Document document = new Document(rect);
  37.         // step 2
  38.         PdfWriter writer = PdfWriter.getInstance(document,
  39.                 new FileOutputStream(filename));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         // draw the coordinate system
  44.         PdfContentByte canvas = writer.getDirectContent();
  45.         canvas.moveTo(-595, 0);
  46.         canvas.lineTo(595, 0);
  47.         canvas.moveTo(0, -842);
  48.         canvas.lineTo(0, 842);
  49.         canvas.stroke();
  50.         // Read the PDF containing the logo
  51.         PdfReader reader = new PdfReader(RESOURCE);
  52.         PdfTemplate template = writer.getImportedPage(reader, 1);
  53.         // add it at different positions using different transformations
  54.         canvas.saveState();
  55.         canvas.addTemplate(template, 0, 0);
  56.         canvas.concatCTM(0.5f, 0, 0, 0.5f, -595, 0);
  57.         canvas.addTemplate(template, 0, 0);
  58.         canvas.concatCTM(1, 0, 0, 1, 595, 595);
  59.         canvas.addTemplate(template, 0, 0);
  60.         canvas.restoreState();
  61.  
  62.         canvas.saveState();
  63.         canvas.concatCTM(1, 0, 0.4f, 1, -750, -650);
  64.         canvas.addTemplate(template, 0, 0);
  65.         canvas.restoreState();
  66.        
  67.         canvas.saveState();
  68.         canvas.concatCTM(0, -1, -1, 0, 650, 0);
  69.         canvas.addTemplate(template, 0, 0);
  70.         canvas.concatCTM(0.2f, 0, 0, 0.5f, 0, 300);
  71.         canvas.addTemplate(template, 0, 0);
  72.         canvas.restoreState();
  73.         // step 5
  74.         document.close();
  75.         reader.close();
  76.     }
  77.    
  78.     /**
  79.      * Main method.
  80.      *
  81.      * @param    args    no arguments needed
  82.      * @throws DocumentException
  83.      * @throws IOException
  84.      */
  85.     public static void main(String[] args) throws IOException, DocumentException {
  86.         new TransformationMatrix1().createPdf(RESULT);
  87.     }
  88. }
TransformationMatrix2.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfReader;
  17. import com.itextpdf.text.pdf.PdfTemplate;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class TransformationMatrix2 {
  21.  
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/transformation_matrix2.pdf";
  24.     /** A PDF with the iText logo that will be transformed. */
  25.     public static final String RESOURCE = "resources/pdfs/logo.pdf";
  26.    
  27.     /**
  28.      * Creates a PDF document.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public void createPdf(String filename) throws IOException, DocumentException {
  34.         // step 1
  35.         Rectangle rect = new Rectangle(-595, -842, 595, 842);
  36.         Document document = new Document(rect);
  37.         // step 2
  38.         PdfWriter writer = PdfWriter.getInstance(document,
  39.                 new FileOutputStream(filename));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         // draw the coordinate system
  44.         PdfContentByte canvas = writer.getDirectContent();
  45.         canvas.moveTo(-595, 0);
  46.         canvas.lineTo(595, 0);
  47.         canvas.moveTo(0, -842);
  48.         canvas.lineTo(0, 842);
  49.         canvas.stroke();
  50.        
  51.         // read the PDF with the logo
  52.         PdfReader reader = new PdfReader(RESOURCE);
  53.         PdfTemplate template = writer.getImportedPage(reader, 1);
  54.         // add it at different positions using different transformations
  55.         canvas.addTemplate(template, 0, 0);
  56.         canvas.addTemplate(template, 0.5f, 0, 0, 0.5f, -595, 0);
  57.         canvas.addTemplate(template, 0.5f, 0, 0, 0.5f, -297.5f, 297.5f);
  58.         canvas.addTemplate(template, 1, 0, 0.4f, 1, -750, -650);
  59.         canvas.addTemplate(template, 0, -1, -1, 0, 650, 0);
  60.         canvas.addTemplate(template, 0, -0.2f, -0.5f, 0, 350, 0);
  61.         // step 5
  62.         document.close();
  63.         reader.close();
  64.     }
  65.    
  66.     /**
  67.      * Main method.
  68.      *
  69.      * @param    args    no arguments needed
  70.      * @throws DocumentException
  71.      * @throws IOException
  72.      */
  73.     public static void main(String[] args) throws IOException, DocumentException {
  74.         new TransformationMatrix2().createPdf(RESULT);
  75.     }
  76. }
TransformationMatrix3.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.awt.geom.AffineTransform;
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Rectangle;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfReader;
  18. import com.itextpdf.text.pdf.PdfTemplate;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class TransformationMatrix3 {
  22.  
  23.     /** The resulting PDF. */
  24.     public static final String RESULT = "results/part4/chapter14/transformation_matrix3.pdf";
  25.     /** A PDF with the iText logo that will be transformed */
  26.     public static final String RESOURCE = "resources/pdfs/logo.pdf";
  27.    
  28.     /**
  29.      * Creates a PDF document.
  30.      * @param filename the path to the new PDF document
  31.      * @throws DocumentException
  32.      * @throws IOException
  33.      */
  34.     public void createPdf(String filename) throws IOException, DocumentException {
  35.         // step 1
  36.         Rectangle rect = new Rectangle(-595, -842, 595, 842);
  37.         Document document = new Document(rect);
  38.         // step 2
  39.         PdfWriter writer = PdfWriter.getInstance(document,
  40.                 new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         PdfContentByte canvas = writer.getDirectContent();
  45.         // draw coordinate system
  46.         canvas.moveTo(-595, 0);
  47.         canvas.lineTo(595, 0);
  48.         canvas.moveTo(0, -842);
  49.         canvas.lineTo(0, 842);
  50.         canvas.stroke();
  51.         // read the PDF with the logo
  52.         PdfReader reader = new PdfReader(RESOURCE);
  53.         PdfTemplate template = writer.getImportedPage(reader, 1);
  54.         // add it
  55.         canvas.saveState();
  56.         canvas.addTemplate(template, 0, 0);
  57.         AffineTransform af = new AffineTransform();
  58.         af.translate(-595, 0);
  59.         af.scale(0.5, 0.5);
  60.         canvas.transform(af);
  61.         canvas.addTemplate(template, 0, 0);
  62.         canvas.concatCTM(AffineTransform.getTranslateInstance(595, 595));
  63.         canvas.addTemplate(template, 0, 0);
  64.         canvas.restoreState();
  65.  
  66.         canvas.saveState();
  67.         af = new AffineTransform(1, 0, 0.4, 1, -750, -650);
  68.         canvas.addTemplate(template, af);
  69.         canvas.restoreState();
  70.        
  71.         canvas.saveState();
  72.         af = new AffineTransform(0, -1, -1, 0, 650, 0);
  73.         canvas.addTemplate(template, af);
  74.         af = new AffineTransform(0, -0.2f, -0.5f, 0, 350, 0);
  75.         canvas.addTemplate(template, af);
  76.         canvas.restoreState();
  77.         // step 5
  78.         document.close();
  79.         reader.close();
  80.     }
  81.    
  82.     /**
  83.      * Main method.
  84.      *
  85.      * @param    args    no arguments needed
  86.      * @throws DocumentException
  87.      * @throws IOException
  88.      */
  89.     public static void main(String[] args) throws IOException, DocumentException {
  90.         new TransformationMatrix3().createPdf(RESULT);
  91.     }
  92. }
TextStateOperators.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.BaseColor;
  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.GrayColor;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfTemplate;
  19. import com.itextpdf.text.pdf.PdfTextArray;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class TextStateOperators {
  23.  
  24.     /** The resulting PDF. */
  25.     public static final String RESULT = "results/part4/chapter14/text_operators.pdf";
  26.  
  27.     /**
  28.      * Creates a PDF document.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public void createPdf(String filename) throws IOException, DocumentException {
  34.         // step 1
  35.         Document document = new Document();
  36.         // step 2
  37.         PdfWriter writer = PdfWriter.getInstance(document,
  38.                 new FileOutputStream(filename));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         PdfContentByte canvas = writer.getDirectContent();
  43.         String text = "AWAY again";
  44.         BaseFont bf = BaseFont.createFont();
  45.         canvas.beginText();
  46.         // line 1
  47.         canvas.setFontAndSize(bf, 16);
  48.         canvas.moveText(36, 806);
  49.         canvas.moveTextWithLeading(0, -24);
  50.         canvas.showText(text);
  51.         // line 2
  52.         canvas.setWordSpacing(20);
  53.         canvas.newlineShowText(text);
  54.         // line 3
  55.         canvas.setCharacterSpacing(10);
  56.         canvas.newlineShowText(text);
  57.         canvas.setWordSpacing(0);
  58.         canvas.setCharacterSpacing(0);
  59.         // line 4
  60.         canvas.setHorizontalScaling(50);
  61.         canvas.newlineShowText(text);
  62.         canvas.setHorizontalScaling(100);
  63.         // line 5
  64.         canvas.newlineShowText(text);
  65.         canvas.setTextRise(15);
  66.         canvas.setFontAndSize(bf, 12);
  67.         canvas.setColorFill(BaseColor.RED);
  68.         canvas.showText("2");
  69.         canvas.setColorFill(GrayColor.GRAYBLACK);
  70.         // line 6
  71.         canvas.setLeading(56);
  72.         canvas.newlineShowText("Changing the leading: " + text);
  73.         canvas.setLeading(24);
  74.         canvas.newlineText();
  75.         // line 7
  76.         PdfTextArray array = new PdfTextArray("A");
  77.         array.add(120);
  78.         array.add("W");
  79.         array.add(120);
  80.         array.add("A");
  81.         array.add(95);
  82.         array.add("Y again");
  83.         canvas.showText(array);
  84.         canvas.endText();
  85.  
  86.         canvas.setColorFill(BaseColor.BLUE);
  87.         canvas.beginText();
  88.         canvas.setTextMatrix(360, 770);
  89.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
  90.         canvas.setFontAndSize(bf, 24);
  91.         canvas.showText(text);
  92.         canvas.endText();
  93.  
  94.         canvas.beginText();
  95.         canvas.setTextMatrix(360, 730);
  96.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
  97.         canvas.setFontAndSize(bf, 24);
  98.         canvas.showText(text);
  99.         canvas.endText();
  100.  
  101.         canvas.beginText();
  102.         canvas.setTextMatrix(360, 690);
  103.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
  104.         canvas.setFontAndSize(bf, 24);
  105.         canvas.showText(text);
  106.         canvas.endText();
  107.  
  108.         canvas.beginText();
  109.         canvas.setTextMatrix(360, 650);
  110.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
  111.         canvas.setFontAndSize(bf, 24);
  112.         canvas.showText(text);
  113.         canvas.endText();
  114.  
  115.         PdfTemplate template = canvas.createTemplate(200, 36);
  116.         template.setLineWidth(2);
  117.         for (int i = 0; i < 6; i++) {
  118.             template.moveTo(0, i * 6);
  119.             template.lineTo(200, i * 6);
  120.         }
  121.         template.stroke();
  122.        
  123.         canvas.saveState();
  124.         canvas.beginText();
  125.         canvas.setTextMatrix(360, 610);
  126.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP);
  127.         canvas.setFontAndSize(bf, 24);
  128.         canvas.showText(text);
  129.         canvas.endText();
  130.         canvas.addTemplate(template, 360, 610);
  131.         canvas.restoreState();
  132.  
  133.         canvas.saveState();
  134.         canvas.beginText();
  135.         canvas.setTextMatrix(360, 570);
  136.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP);
  137.         canvas.setFontAndSize(bf, 24);
  138.         canvas.showText(text);
  139.         canvas.endText();
  140.         canvas.addTemplate(template, 360, 570);
  141.         canvas.restoreState();
  142.  
  143.         canvas.saveState();
  144.         canvas.beginText();
  145.         canvas.setTextMatrix(360, 530);
  146.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP);
  147.         canvas.setFontAndSize(bf, 24);
  148.         canvas.showText(text);
  149.         canvas.endText();
  150.         canvas.addTemplate(template, 360, 530);
  151.         canvas.restoreState();
  152.        
  153.         canvas.saveState();
  154.         canvas.beginText();
  155.         canvas.setTextMatrix(360, 490);
  156.         canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP);
  157.         canvas.setFontAndSize(bf, 24);
  158.         canvas.showText(text);
  159.         canvas.endText();
  160.         canvas.addTemplate(template, 360, 490);
  161.         canvas.restoreState();
  162.  
  163.         // step 5
  164.         document.close();
  165.     }
  166.    
  167.     /**
  168.      * Main method.
  169.      *
  170.      * @param    args    no arguments needed
  171.      * @throws DocumentException
  172.      * @throws IOException
  173.      */
  174.     public static void main(String[] args) throws IOException, DocumentException {
  175.         new TextStateOperators().createPdf(RESULT);
  176.     }
  177.  
  178. }
TextMethods.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. package part4.chapter14;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class TextMethods {
  19.  
  20.     /** The resulting PDF. */
  21.     public static final String RESULT = "results/part4/chapter14/text_methods.pdf";
  22.  
  23.     /**
  24.      * Creates a PDF document.
  25.      * @param filename the path to the new PDF document
  26.      * @throws DocumentException
  27.      * @throws IOException
  28.      */
  29.     public void createPdf(String filename) throws IOException, DocumentException {
  30.         // step 1
  31.         Document document = new Document();
  32.         // step 2
  33.         PdfWriter writer = PdfWriter.getInstance(document,
  34.                 new FileOutputStream(filename));
  35.         // step 3
  36.         document.open();
  37.         // step 4
  38.         // draw helper lines
  39.         PdfContentByte cb = writer.getDirectContent();
  40.         cb.setLineWidth(0f);
  41.         cb.moveTo(150, 600);
  42.         cb.lineTo(150, 800);
  43.         cb.moveTo(50, 760);
  44.         cb.lineTo(250, 760);
  45.         cb.moveTo(50, 700);
  46.         cb.lineTo(250, 700);
  47.         cb.moveTo(50, 640);
  48.         cb.lineTo(250, 640);
  49.         cb.stroke();
  50.         // draw text
  51.         String text = "AWAY again ";
  52.         BaseFont bf = BaseFont.createFont();
  53.         cb.beginText();
  54.         cb.setFontAndSize(bf, 12);
  55.         cb.setTextMatrix(50, 800);
  56.         cb.showText(text);
  57.         cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 150, 760, 0);
  58.         cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 150, 700, 0);
  59.         cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 150, 640, 0);
  60.         cb.showTextAlignedKerned(PdfContentByte.ALIGN_LEFT, text + " Left", 150, 628, 0);
  61.         cb.setTextMatrix(0, 1, -1, 0, 300, 600);
  62.         cb.showText("Position 300,600, rotated 90 degrees.");
  63.         for (int i = 0; i < 360; i += 30) {
  64.             cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text, 400, 700, i);
  65.         }
  66.         cb.endText();
  67.         // step 5
  68.         document.close();
  69.     }
  70.    
  71.     /**
  72.      * Main method.
  73.      *
  74.      * @param    args    no arguments needed
  75.      * @throws DocumentException
  76.      * @throws IOException
  77.      */
  78.     public static void main(String[] args) throws IOException, DocumentException {
  79.         new TextMethods().createPdf(RESULT);
  80.     }
  81.  
  82. }
PearExample.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. package part4.chapter14;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.awt.geom.Area;
  16. import java.awt.geom.Ellipse2D;
  17.  
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20.  
  21. public class PearExample extends JPanel {
  22.    
  23.     /** Ellipse in Double precision */
  24.     Ellipse2D.Double circle;
  25.     /** Ellipse in Double precision */
  26.     Ellipse2D.Double oval;
  27.     /** Ellipse in Double precision */
  28.     Ellipse2D.Double leaf;
  29.     /** Ellipse in Double precision */
  30.     Ellipse2D.Double stem;
  31.  
  32.     /** A geometric area */
  33.     Area circ;
  34.     /** A geometric area */
  35.     Area ov;
  36.     /** A geometric area */
  37.     Area leaf1;
  38.     /** A geometric area */
  39.     Area leaf2;
  40.     /** A geometric area */
  41.     Area st1;
  42.     /** A geometric area */
  43.     Area st2;
  44.  
  45.     /**
  46.      * Initializes all the values needed to draw a Pear on a JPanel.
  47.      */
  48.     public PearExample() {
  49.         circle = new Ellipse2D.Double();
  50.         oval = new Ellipse2D.Double();
  51.         leaf = new Ellipse2D.Double();
  52.         stem = new Ellipse2D.Double();
  53.         circ = new Area(circle);
  54.         ov = new Area(oval);
  55.         leaf1 = new Area(leaf);
  56.         leaf2 = new Area(leaf);
  57.         st1 = new Area(stem);
  58.         st2 = new Area(stem);
  59.     }
  60.    
  61.     /**
  62.      * Paints a pear.
  63.      * @see javax.swing.JComponent#paint(java.awt.Graphics)
  64.      */
  65.     public void paint(Graphics g) {
  66.         Graphics2D g2 = (Graphics2D)g;
  67.         double ew = 75;
  68.         double eh = 75;
  69.  
  70.         g2.setColor(Color.GREEN);
  71.  
  72.         // Creates the first leaf by filling the intersection of two Area
  73.         // objects created from an ellipse.
  74.         leaf.setFrame(ew - 16, eh - 29, 15.0, 15.0);
  75.         leaf1 = new Area(leaf);
  76.         leaf.setFrame(ew - 14, eh - 47, 30.0, 30.0);
  77.         leaf2 = new Area(leaf);
  78.         leaf1.intersect(leaf2);
  79.         g2.fill(leaf1);
  80.  
  81.         // Creates the second leaf.
  82.         leaf.setFrame(ew + 1, eh - 29, 15.0, 15.0);
  83.         leaf1 = new Area(leaf);
  84.         leaf2.intersect(leaf1);
  85.         g2.fill(leaf2);
  86.  
  87.         g2.setColor(Color.BLACK);
  88.  
  89.         // Creates the stem by filling the Area resulting from the subtraction
  90.         // of two Area objects created from an ellipse.
  91.         stem.setFrame(ew, eh - 42, 40.0, 40.0);
  92.         st1 = new Area(stem);
  93.         stem.setFrame(ew + 3, eh - 47, 50.0, 50.0);
  94.         st2 = new Area(stem);
  95.         st1.subtract(st2);
  96.         g2.fill(st1);
  97.  
  98.         g2.setColor(Color.YELLOW);
  99.  
  100.         // Creates the pear itself by filling the Area resulting from the union
  101.         // of two Area objects created by two different ellipses.
  102.         circle.setFrame(ew - 25, eh, 50.0, 50.0);
  103.         oval.setFrame(ew - 19, eh - 20, 40.0, 70.0);
  104.         circ = new Area(circle);
  105.         ov = new Area(oval);
  106.         circ.add(ov);
  107.         g2.fill(circ);
  108.     }
  109.    
  110.     /**
  111.      * Opens a JFrame showing a Pear.
  112.      * @param args no arguments needed
  113.      */
  114.     public static void main(String[] args) {
  115.         PearExample pear = new PearExample();
  116.         JFrame f = new JFrame("Pear");
  117.         f.getContentPane().add( pear, "Center" );
  118.  
  119.         f.addWindowListener(new WindowAdapter() {
  120.             public void windowClosing(WindowEvent e) {
  121.                 System.exit(0);
  122.             }
  123.         });
  124.         f.setSize(new Dimension(160, 200));
  125.         f.setVisible(true);
  126.     }
  127.  
  128.     /** A serial version UID. */
  129.     private static final long serialVersionUID = 1251626928914650961L;
  130. }
PearToPdf.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. package part4.chapter14;
  8.  
  9. import java.awt.Graphics2D;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.awt.PdfGraphics2D;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Rectangle;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class PearToPdf {
  21.  
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/pear.pdf";
  24.  
  25.     /**
  26.      * Creates a PDF document.
  27.      * @param filename the path to the new PDF document
  28.      * @throws DocumentException
  29.      * @throws IOException
  30.      */
  31.     public void createPdf(String filename) throws IOException, DocumentException {
  32.         // step 1
  33.         Document document = new Document(new Rectangle(150, 150));
  34.         // step 2
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  36.         // step 3
  37.         document.open();
  38.         // step 4
  39.         PdfContentByte canvas = writer.getDirectContent();
  40.         Graphics2D g2 = new PdfGraphics2D(canvas, 150, 150);
  41.         PearExample pear = new PearExample();
  42.         pear.paint(g2);
  43.         g2.dispose();
  44.         // step 5
  45.         document.close();
  46.     }
  47.  
  48.     /**
  49.      * Main method.
  50.      *
  51.      * @param    args    no arguments needed
  52.      * @throws DocumentException
  53.      * @throws IOException
  54.      */
  55.     public static void main(String[] args) throws IOException, DocumentException {
  56.         new PearToPdf().createPdf(RESULT);
  57.     }
  58. }
DirectorCharts.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. package part4.chapter14;
  8.  
  9. import java.awt.Graphics2D;
  10. import java.awt.geom.Rectangle2D;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16.  
  17. import org.jfree.chart.ChartFactory;
  18. import org.jfree.chart.JFreeChart;
  19. import org.jfree.chart.plot.PlotOrientation;
  20. import org.jfree.data.category.DefaultCategoryDataset;
  21. import org.jfree.data.general.DefaultPieDataset;
  22.  
  23. import com.itextpdf.awt.PdfGraphics2D;
  24. import com.itextpdf.text.Document;
  25. import com.itextpdf.text.DocumentException;
  26. import com.itextpdf.text.PageSize;
  27. import com.itextpdf.text.pdf.PdfContentByte;
  28. import com.itextpdf.text.pdf.PdfTemplate;
  29. import com.itextpdf.text.pdf.PdfWriter;
  30. import com.lowagie.database.DatabaseConnection;
  31. import com.lowagie.database.HsqldbConnection;
  32. import com.lowagie.filmfestival.Director;
  33. import com.lowagie.filmfestival.PojoFactory;
  34.  
  35. public class DirectorCharts {
  36.     /** The resulting PDF. */
  37.     public static final String RESULT = "results/part4/chapter14/director_charts.pdf";
  38.     /** A query that needs to be visualized in a chart. */
  39.     public static final String QUERY1 =
  40.         "SELECT DISTINCT d.id, d.name, d.given_name, count(*) AS c "
  41.             + "FROM film_director d, film_movie_director md "
  42.             + "WHERE d.id = md.director_id "
  43.             + "GROUP BY d.id, d.name, d.given_name ORDER BY c DESC, name LIMIT 9";
  44.     /** A query that needs to be visualized in a chart. */
  45.     public static final String QUERY2 =
  46.         "SELECT DISTINCT d.id, d.name, d.given_name, count(*) AS c "
  47.             + "FROM film_director d, film_movie_director md "
  48.             + "WHERE d.id = md.director_id "
  49.             + "GROUP BY d.id, d.name, d.given_name ORDER BY c DESC, name LIMIT 17";
  50.    
  51.     /**
  52.      * Creates a PDF document.
  53.      * @param filename the path to the new PDF document
  54.      * @throws DocumentException
  55.      * @throws IOException
  56.      * @throws SQLException
  57.      */
  58.     public void createPdf(String filename) throws IOException, DocumentException, SQLException {
  59.         // step 1
  60.         Document document = new Document();
  61.         // step 2
  62.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  63.         // step 3
  64.         document.open();
  65.         // step 4
  66.         PdfContentByte cb = writer.getDirectContent();
  67.         float width = PageSize.A4.getWidth();
  68.         float height = PageSize.A4.getHeight() / 2;
  69.         // Pie chart
  70.         PdfTemplate pie = cb.createTemplate(width, height);
  71.         Graphics2D g2d1 = new PdfGraphics2D(pie, width, height);
  72.         Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height);
  73.         getPieChart().draw(g2d1, r2d1);
  74.         g2d1.dispose();
  75.         cb.addTemplate(pie, 0, height);
  76.         // Bar chart
  77.         PdfTemplate bar = cb.createTemplate(width, height);
  78.         Graphics2D g2d2 = new PdfGraphics2D(bar, width, height);
  79.         Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, width, height);
  80.         getBarChart().draw(g2d2, r2d2);
  81.         g2d2.dispose();
  82.         cb.addTemplate(bar, 0, 0);
  83.         // step 5
  84.         document.close();
  85.     }
  86.    
  87.     /**
  88.      * Main method.
  89.      *
  90.      * @param    args    no arguments needed
  91.      * @throws DocumentException
  92.      * @throws IOException
  93.      * @throws SQLException
  94.      */
  95.     public static void main(String[] args) throws IOException, DocumentException, SQLException {
  96.         new DirectorCharts().createPdf(RESULT);
  97.     }
  98.    
  99.     /**
  100.      * Gets an example pie chart.
  101.      *
  102.      * @return a pie chart
  103.      * @throws SQLException
  104.      * @throws IOException
  105.      */
  106.     public static JFreeChart getPieChart() throws SQLException, IOException {
  107.         DatabaseConnection connection = new HsqldbConnection("filmfestival");
  108.         Statement stm = connection.createStatement();
  109.         ResultSet rs = stm.executeQuery(QUERY1);
  110.         DefaultPieDataset dataset = new DefaultPieDataset();
  111.         Director director;
  112.         while (rs.next()) {
  113.             director = PojoFactory.getDirector(rs);
  114.             dataset.setValue(
  115.                 String.format("%s, %s", director.getName(), director.getGivenName()),
  116.                 rs.getInt("c"));
  117.         }
  118.         connection.close();
  119.         return ChartFactory.createPieChart("Movies / directors", dataset,
  120.                 true, true, false);
  121.     }
  122.    
  123.     /**
  124.      * Gets an example bar chart.
  125.      *
  126.      * @return a bar chart
  127.      * @throws SQLException
  128.      * @throws IOException
  129.      */
  130.     public static JFreeChart getBarChart() throws SQLException, IOException {
  131.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  132.         DatabaseConnection connection = new HsqldbConnection("filmfestival");
  133.         Statement stm = connection.createStatement();
  134.         ResultSet rs = stm.executeQuery(QUERY2);
  135.         Director director;
  136.         while (rs.next()) {
  137.             director = PojoFactory.getDirector(rs);
  138.             dataset.setValue(
  139.                 rs.getInt("c"),
  140.                 "movies",
  141.                 String.format("%s, %s", director.getName(), director.getGivenName()));
  142.         }
  143.         connection.close();
  144.         return ChartFactory.createBarChart("Movies / directors", "Director",
  145.                 "# Movies", dataset, PlotOrientation.HORIZONTAL, false,
  146.                 true, false);
  147.     }
  148. }
Graphics2DFonts.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. package part4.chapter14;
  8.  
  9. import java.awt.Font;
  10. import java.awt.Graphics2D;
  11. import java.awt.GraphicsEnvironment;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.PrintStream;
  15. import java.util.Map.Entry;
  16.  
  17. import com.itextpdf.awt.DefaultFontMapper;
  18. import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters;
  19. import com.itextpdf.awt.PdfGraphics2D;
  20. import com.itextpdf.text.Document;
  21. import com.itextpdf.text.DocumentException;
  22. import com.itextpdf.text.Rectangle;
  23. import com.itextpdf.text.pdf.PdfContentByte;
  24. import com.itextpdf.text.pdf.PdfWriter;
  25.  
  26. public class Graphics2DFonts {
  27.     /** A text file containing the available AWT fonts. */
  28.     public static final String RESULT1
  29.         = "results/part4/chapter14/awt_fonts.txt";
  30.     /** A text file containing the mapping of PDF and AWT fonts. */
  31.     public static final String RESULT2
  32.         = "results/part4/chapter14/pdf_fonts.txt";
  33.     /** The resulting PDF. */
  34.     public static final String RESULT3
  35.         = "results/part4/chapter14/fonts.pdf";
  36.    
  37.     /** A series of fonts that will be used. */
  38.     public static final Font[] FONTS = {
  39.         new Font("Serif", Font.PLAIN, 12),
  40.         new Font("Serif", Font.BOLD, 12),
  41.         new Font("Serif", Font.ITALIC, 12),
  42.         new Font("SansSerif", Font.PLAIN, 12),
  43.         new Font("Monospaced", Font.PLAIN, 12)
  44.     };
  45.  
  46.     /**
  47.      * Creates lists of fonts that can be used in AWT and PDF.
  48.      * Creates a PDF document.
  49.      * @param args no arguments needed
  50.      * @throws IOException
  51.      * @throws DocumentException
  52.      */
  53.     public static void main(String[] args) throws IOException, DocumentException {
  54.         // Creates a list of the available font families in Java AWT.
  55.         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  56.         String[] fontFamily = ge.getAvailableFontFamilyNames();
  57.         PrintStream out1 = new PrintStream(new FileOutputStream(RESULT1));
  58.         for (int i = 0; i < fontFamily.length; i++) {
  59.             out1.println(fontFamily[i]);
  60.         }
  61.         out1.flush();
  62.         out1.close();
  63.        
  64.         // Creates a mapper to map fonts available for PDF creation with AWT fonts
  65.         DefaultFontMapper mapper = new DefaultFontMapper();
  66.         mapper.insertDirectory("c:/windows/fonts/");
  67.         // Writes a text version of this mapper to a file
  68.         PrintStream out2 = new PrintStream(new FileOutputStream(RESULT2));
  69.         for (Entry entry : mapper.getMapper().entrySet()) {
  70.             out2.println(String.format("%s: %s", entry.getKey(), entry.getValue().fontName));
  71.         }
  72.         out2.flush();
  73.         out2.close();
  74.        
  75.         // Creates a PDF with the text "Hello World" in different fonts.
  76.         float width = 150;
  77.         float height = 150;
  78.         // step 1
  79.         Document document = new Document(new Rectangle(width, height));
  80.         // step 2
  81.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT3));
  82.         // step 3
  83.         document.open();
  84.         // step 4
  85.         PdfContentByte cb = writer.getDirectContent();
  86.         Graphics2D g2d = new PdfGraphics2D(cb, width, height, mapper);
  87.         for (int i = 0; i < FONTS.length; ) {
  88.             g2d.setFont(FONTS[i++]);
  89.             g2d.drawString("Hello world", 5, 24 * i);
  90.         }
  91.         g2d.dispose();
  92.         // step 5
  93.         document.close();
  94.     }
  95. }
TextExample1.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Font;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.text.AttributedString;
  16. import java.awt.font.TextAttribute;
  17.  
  18.  
  19. import javax.swing.JFrame;
  20. import javax.swing.JPanel;
  21.  
  22. public class TextExample1 extends JPanel {
  23.  
  24.     /** A String that needs to be displayed in a PDF document */
  25.     private static AttributedString akira;
  26.  
  27.     /**
  28.      * Creates a TextExample that will be used to demonstrate how text in AWT can be translated to PDF.
  29.      */
  30.     public TextExample1() {
  31.         akira = new AttributedString(
  32.             "Akira Kurosawa: \u9ed2\u6fa4 \u660e or \u9ed2\u6ca2 \u660e; " +
  33.             "23 March 1910 - 6 September 1998.");
  34.         akira.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.PLAIN, 12));
  35.         akira.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.BOLD, 12), 0, 15);
  36.         akira.addAttribute(TextAttribute.FONT, new Font("MS PGothic", Font.PLAIN, 12), 16, 20);
  37.         akira.addAttribute(TextAttribute.FONT, new Font("MS PGothic", Font.PLAIN, 12), 24, 28);
  38.     }
  39.  
  40.     /**
  41.      * Draws the String to a Graphics object.
  42.      * @see javax.swing.JComponent#paint(java.awt.Graphics)
  43.      */
  44.     public void paint(Graphics g) {
  45.         Graphics2D g2d = (Graphics2D) g;
  46.         g2d.drawString(akira.getIterator(), 10, 16);
  47.     }
  48.    
  49.     /**
  50.      * Creates a JFrame and draws a String to it.
  51.      * @param args no arguments needed
  52.      */
  53.     public static void main(String[] args) {
  54.         TextExample1 Kurosawa = new TextExample1();
  55.         JFrame f = new JFrame("Kurosawa");
  56.         f.getContentPane().add( Kurosawa, "Center" );
  57.         f.addWindowListener(new WindowAdapter() {
  58.             public void windowClosing(WindowEvent e) {
  59.                 System.exit(0);
  60.             }
  61.         });
  62.         f.setSize(new Dimension(500, 60));
  63.         f.setVisible(true);
  64.     }
  65.  
  66.     /** A serial version UID */
  67.     private static final long serialVersionUID = -3639324875232824761L;
  68. }
Text1ToPdf1.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. package part4.chapter14;
  8.  
  9. import java.awt.Graphics2D;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.awt.DefaultFontMapper;
  14. import com.itextpdf.awt.PdfGraphics2D;
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class Text1ToPdf1 {
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/text11.pdf";
  24.  
  25.     /**
  26.      * Creates a PDF document.
  27.      * This example doesn't work as expected.
  28.      * See Text1ToPdf2 to find out how to do it correctly.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public void createPdf(String filename) throws IOException, DocumentException {
  34.         // step 1
  35.         Document document = new Document(new Rectangle(600, 60));
  36.         // step 2
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  38.         // step 3
  39.         document.open();
  40.         // step 4
  41.         PdfContentByte canvas = writer.getDirectContent();
  42.         // Create a font mapper and add a font directory
  43.         DefaultFontMapper mapper = new DefaultFontMapper();
  44.         mapper.insertDirectory("c:/windows/fonts/");
  45.         // create the Graphics2D object
  46.         Graphics2D g2 = new PdfGraphics2D(canvas, 600, 60, mapper);
  47.         // write the text to the Graphics2D (will NOT work as expected!)
  48.         TextExample1 text = new TextExample1();
  49.         text.paint(g2);
  50.         g2.dispose();
  51.         // step 5
  52.         document.close();
  53.     }
  54.     /**
  55.      * Main method.
  56.      *
  57.      * @param    args    no arguments needed
  58.      * @throws DocumentException
  59.      * @throws IOException
  60.      */
  61.     public static void main(String[] args) throws IOException, DocumentException {
  62.         new Text1ToPdf1().createPdf(RESULT);
  63.     }
  64. }
Text1ToPdf2.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. package part4.chapter14;
  8.  
  9. import java.awt.Graphics2D;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.awt.DefaultFontMapper;
  14. import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters;
  15. import com.itextpdf.awt.PdfGraphics2D;
  16. import com.itextpdf.text.Document;
  17. import com.itextpdf.text.DocumentException;
  18. import com.itextpdf.text.Rectangle;
  19. import com.itextpdf.text.pdf.BaseFont;
  20. import com.itextpdf.text.pdf.PdfContentByte;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23. public class Text1ToPdf2 {
  24.     /** The resulting PDF. */
  25.     public static final String RESULT = "results/part4/chapter14/text12.pdf";
  26.  
  27.     /**
  28.      * Creates a PDF document.
  29.      * @param filename the path to the new PDF document
  30.      * @throws DocumentException
  31.      * @throws IOException
  32.      */
  33.     public void createPdf(String filename) throws IOException, DocumentException {
  34.         // step 1
  35.         Document document = new Document(new Rectangle(600, 60));
  36.         // step 2
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  38.         // step 3
  39.         document.open();
  40.         // step 4
  41.         PdfContentByte canvas = writer.getDirectContent();
  42.         // create a font mapper
  43.         DefaultFontMapper mapper = new DefaultFontMapper();
  44.         // map MS Gothic to the corresponding font program
  45.         BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1");
  46.         parameters.encoding = BaseFont.IDENTITY_H;
  47.         mapper.putName("MS PGothic", parameters );
  48.         // Create a Graphics2D object
  49.         Graphics2D g2 = new PdfGraphics2D(canvas, 600, 60, mapper);
  50.         // write the text to the Graphics2D
  51.         TextExample1 text = new TextExample1();
  52.         text.paint(g2);
  53.         g2.dispose();
  54.         // step 5
  55.         document.close();
  56.     }
  57.  
  58.     /**
  59.      * Main method.
  60.      *
  61.      * @param    args    no arguments needed
  62.      * @throws DocumentException
  63.      * @throws IOException
  64.      */
  65.     public static void main(String[] args) throws IOException, DocumentException {
  66.         new Text1ToPdf2().createPdf(RESULT);
  67.     }
  68. }
TextExample2.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Font;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.text.AttributedCharacterIterator;
  16. import java.text.AttributedString;
  17. import java.awt.font.FontRenderContext;
  18. import java.awt.font.LineBreakMeasurer;
  19. import java.awt.font.TextAttribute;
  20. import java.awt.font.TextLayout;
  21.  
  22.  
  23. import javax.swing.JFrame;
  24. import javax.swing.JPanel;
  25.  
  26. public class TextExample2 extends JPanel {
  27.  
  28.     /** A String that needs to be displayed in a PDF document */
  29.     private static AttributedString akira;
  30.  
  31.     /** A String that needs to be displayed in a PDF document */
  32.     public static final String AKIRA =
  33.         "Akira Kurosawa (\u9ed2\u6fa4 \u660e or \u9ed2\u6ca2 \u660e, " +
  34.         "Kurosawa Akira, 23 March 1910 - 6 September 1998) was a " +
  35.         "Japanese film director, producer, screenwriter and editor. " +
  36.         "In a career that spanned 50 years, Kurosawa directed 30 films. " +
  37.         "He is widely regarded as one of the most important and " +
  38.         "influential filmmakers in film history.";
  39.  
  40.     /**
  41.      * Creates a TextExample that will be used to demonstrate how text in AWT can be translated to PDF.
  42.      */
  43.     public TextExample2() {
  44.         akira = new AttributedString(AKIRA);
  45.         akira.addAttribute(TextAttribute.FONT, new Font("Arial Unicode MS", Font.PLAIN, 12));
  46.     }
  47.  
  48.     /**
  49.      * Draws the String to a Graphics object.
  50.      * @see javax.swing.JComponent#paint(java.awt.Graphics)
  51.      */
  52.     public void paint(Graphics g) {
  53.         LineBreakMeasurer lineMeasurer = null;
  54.         int paragraphStart = 0;
  55.         int paragraphEnd = 0;
  56.         Graphics2D g2d = (Graphics2D) g;
  57.         g2d.setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));
  58.         if (lineMeasurer == null) {
  59.             AttributedCharacterIterator paragraph = akira.getIterator();
  60.             paragraphStart = paragraph.getBeginIndex();
  61.             paragraphEnd = paragraph.getEndIndex();
  62.             FontRenderContext frc = g2d.getFontRenderContext();
  63.             lineMeasurer = new LineBreakMeasurer(paragraph, frc);
  64.         }
  65.         float breakWidth = (float)getSize().width;
  66.         float drawPosY = 0;
  67.         lineMeasurer.setPosition(paragraphStart);
  68.         int start = 0;
  69.         int end = 0;
  70.         while (lineMeasurer.getPosition() < paragraphEnd) {
  71.             TextLayout layout = lineMeasurer.nextLayout(breakWidth);
  72.             drawPosY += layout.getAscent();
  73.             end = start + layout.getCharacterCount();
  74.             g2d.drawString(AKIRA.substring(start, end), 0, drawPosY);
  75.             start = end;
  76.             drawPosY += layout.getDescent() + layout.getLeading();
  77.         }
  78.     }
  79.    
  80.     /**
  81.      * Creates a JFrame and draws a String to it.
  82.      * @param args no arguments needed
  83.      */
  84.     public static void main(String[] args) {
  85.         TextExample2 Kurosawa = new TextExample2();
  86.         JFrame f = new JFrame("Kurosawa");
  87.         f.getContentPane().add( Kurosawa, "Center" );
  88.  
  89.         f.addWindowListener(new WindowAdapter() {
  90.             public void windowClosing(WindowEvent e) {
  91.                 System.exit(0);
  92.             }
  93.         });
  94.         f.setSize(new Dimension(300, 150));
  95.         f.setVisible(true);
  96.     }
  97.  
  98.     /** A serial version UID. */
  99.     private static final long serialVersionUID = -3639324875232824761L;
  100. }
Text2ToPdf1.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Font;
  11. import java.awt.Graphics2D;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. import com.itextpdf.awt.FontMapper;
  16. import com.itextpdf.awt.PdfGraphics2D;
  17. import com.itextpdf.text.Document;
  18. import com.itextpdf.text.DocumentException;
  19. import com.itextpdf.text.Rectangle;
  20. import com.itextpdf.text.pdf.BaseFont;
  21. import com.itextpdf.text.pdf.PdfContentByte;
  22. import com.itextpdf.text.pdf.PdfWriter;
  23.  
  24. public class Text2ToPdf1 {
  25.     /** The resulting PDF. */
  26.     public static final String RESULT = "results/part4/chapter14/text21.pdf";
  27.  
  28.     /**
  29.      * Creates a PDF document.
  30.      * @param filename the path to the new PDF document
  31.      * @throws DocumentException
  32.      * @throws IOException
  33.      */
  34.     public void createPdf(String filename) throws IOException, DocumentException {
  35.         // step 1
  36.         Document document = new Document(new Rectangle(300, 150));
  37.         // step 2
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  39.         // step 3
  40.         document.open();
  41.         // step 4
  42.         PdfContentByte canvas = writer.getDirectContent();
  43.         // Create a custom font mapper that forces the use of arial unicode
  44.         FontMapper arialuni = new FontMapper() {
  45.             public BaseFont awtToPdf(Font font) {
  46.                 try {
  47.                     return BaseFont.createFont(
  48.                             "c:/windows/fonts/arialuni.ttf",
  49.                             BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  50.                 } catch (DocumentException e) {
  51.                     e.printStackTrace();
  52.                 } catch (IOException e) {
  53.                     e.printStackTrace();
  54.                 }
  55.                 return null;
  56.             }
  57.  
  58.             public Font pdfToAwt(BaseFont font, int size) {
  59.                 return null;
  60.             }
  61.            
  62.         };
  63.         // Create a Graphics2D object
  64.         Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, arialuni);
  65.         // Draw text to the Graphics2D
  66.         TextExample2 text = new TextExample2();
  67.         text.setSize(new Dimension(300, 150));
  68.         text.paint(g2);
  69.         g2.dispose();
  70.         // step 5
  71.         document.close();
  72.     }
  73.     /**
  74.      * Main method.
  75.      *
  76.      * @param    args    no arguments needed
  77.      * @throws DocumentException
  78.      * @throws IOException
  79.      */
  80.     public static void main(String[] args) throws IOException, DocumentException {
  81.         new Text2ToPdf1().createPdf(RESULT);
  82.     }
  83. }
Text2ToPdf2.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Graphics2D;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.awt.AsianFontMapper;
  15. import com.itextpdf.awt.PdfGraphics2D;
  16. import com.itextpdf.text.Document;
  17. import com.itextpdf.text.DocumentException;
  18. import com.itextpdf.text.Rectangle;
  19. import com.itextpdf.text.pdf.PdfContentByte;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class Text2ToPdf2 {
  23.     /** The resulting PDF. */
  24.     public static final String RESULT = "results/part4/chapter14/text22.pdf";
  25.  
  26.     /**
  27.      * Creates a PDF document.
  28.      * @param filename the path to the new PDF document
  29.      * @throws DocumentException
  30.      * @throws IOException
  31.      */
  32.     public void createPdf(String filename) throws IOException, DocumentException {
  33.         // step 1
  34.         Document document = new Document(new Rectangle(300, 150));
  35.         // step 2
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  37.         // step 3
  38.         document.open();
  39.         // step 4
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.         Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, new AsianFontMapper(
  42.                 AsianFontMapper.JapaneseFont_Min, AsianFontMapper.JapaneseEncoding_H));
  43.         TextExample2 text = new TextExample2();
  44.         text.setSize(new Dimension(300, 150));
  45.         text.paint(g2);
  46.         g2.dispose();
  47.         // step 5
  48.         document.close();
  49.     }
  50.     /**
  51.      * Main method.
  52.      *
  53.      * @param    args    no arguments needed
  54.      * @throws DocumentException
  55.      * @throws IOException
  56.      */
  57.     public static void main(String[] args) throws IOException, DocumentException {
  58.         new Text2ToPdf2().createPdf(RESULT);
  59.     }
  60. }
Text2ToPdf3.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Graphics2D;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.awt.PdfGraphics2D;
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class Text2ToPdf3 {
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/text23.pdf";
  24.  
  25.     /**
  26.      * Creates a PDF document.
  27.      * @param filename the path to the new PDF document
  28.      * @throws DocumentException
  29.      * @throws IOException
  30.      */
  31.     public void createPdf(String filename) throws IOException, DocumentException {
  32.         // step 1
  33.         Document document = new Document(new Rectangle(300, 150));
  34.         // step 2
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  36.         // step 3
  37.         document.open();
  38.         // step 4
  39.         PdfContentByte canvas = writer.getDirectContent();
  40.         Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, true);
  41.         TextExample2 text = new TextExample2();
  42.         text.setSize(new Dimension(300, 150));
  43.         text.paint(g2);
  44.         g2.dispose();
  45.         // step 5
  46.         document.close();
  47.     }
  48.     /**
  49.      * Main method.
  50.      *
  51.      * @param    args    no arguments needed
  52.      * @throws DocumentException
  53.      * @throws IOException
  54.      */
  55.     public static void main(String[] args) throws IOException, DocumentException {
  56.         new Text2ToPdf3().createPdf(RESULT);
  57.     }
  58. }
TextExample3.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Font;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.text.AttributedCharacterIterator;
  16. import java.text.AttributedString;
  17. import java.awt.font.FontRenderContext;
  18. import java.awt.font.LineBreakMeasurer;
  19. import java.awt.font.TextAttribute;
  20. import java.awt.font.TextLayout;
  21.  
  22.  
  23. import javax.swing.JFrame;
  24. import javax.swing.JPanel;
  25.  
  26. public class TextExample3 extends JPanel {
  27.  
  28.     /** A String that needs to be displayed in a PDF document */
  29.     private static AttributedString akira;
  30.  
  31.     /**
  32.      * Creates a TextExample that will be used to demonstrate how text in AWT can be translated to PDF.
  33.      */
  34.     public TextExample3() {
  35.         akira = new AttributedString(
  36.             "Akira Kurosawa (\u9ed2\u6fa4 \u660e or \u9ed2\u6ca2 \u660e, " +
  37.             "Kurosawa Akira, 23 March 1910 - 6 September 1998) was a " +
  38.             "Japanese film director, producer, screenwriter and editor. " +
  39.             "In a career that spanned 50 years, Kurosawa directed 30 films. " +
  40.             "He is widely regarded as one of the most important and " +
  41.             "influential filmmakers in film history.");
  42.         akira.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.PLAIN, 12));
  43.         akira.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.BOLD, 12), 0, 14);
  44.         akira.addAttribute(TextAttribute.FONT, new Font("MS PGothic", Font.PLAIN, 12), 16, 20);
  45.         akira.addAttribute(TextAttribute.FONT, new Font("MS PGothic", Font.PLAIN, 12), 24, 28);
  46.     }
  47.  
  48.     /**
  49.      * Draws the String to a Graphics object.
  50.      * @see javax.swing.JComponent#paint(java.awt.Graphics)
  51.      */
  52.     public void paint(Graphics g) {
  53.         LineBreakMeasurer lineMeasurer = null;
  54.         int paragraphStart = 0;
  55.         int paragraphEnd = 0;
  56.         Graphics2D g2d = (Graphics2D) g;
  57.         if (lineMeasurer == null) {
  58.             AttributedCharacterIterator paragraph = akira.getIterator();
  59.             paragraphStart = paragraph.getBeginIndex();
  60.             paragraphEnd = paragraph.getEndIndex();
  61.             FontRenderContext frc = g2d.getFontRenderContext();
  62.             lineMeasurer = new LineBreakMeasurer(paragraph, frc);
  63.         }
  64.         float breakWidth = (float)getSize().width;
  65.         float drawPosY = 0;
  66.         lineMeasurer.setPosition(paragraphStart);
  67.         while (lineMeasurer.getPosition() < paragraphEnd) {
  68.             TextLayout layout = lineMeasurer.nextLayout(breakWidth);
  69.             drawPosY += layout.getAscent();
  70.             layout.draw(g2d, 0, drawPosY);
  71.             drawPosY += layout.getDescent() + layout.getLeading();
  72.         }
  73.     }
  74.    
  75.     /**
  76.      * Creates a JFrame and draws a String to it.
  77.      * @param args no arguments needed
  78.      */
  79.     public static void main(String[] args) {
  80.         TextExample3 Kurosawa = new TextExample3();
  81.         JFrame f = new JFrame("Kurosawa");
  82.         f.getContentPane().add( Kurosawa, "Center" );
  83.  
  84.         f.addWindowListener(new WindowAdapter() {
  85.             public void windowClosing(WindowEvent e) {
  86.                 System.exit(0);
  87.             }
  88.         });
  89.         f.setSize(new Dimension(300, 150));
  90.         f.setVisible(true);
  91.     }
  92.  
  93.     /** A serial version UID. */
  94.     private static final long serialVersionUID = -3639324875232824761L;
  95. }
Text3ToPdf.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Graphics2D;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.awt.PdfGraphics2D;
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class Text3ToPdf {
  22.     /** The resulting PDF. */
  23.     public static final String RESULT = "results/part4/chapter14/text3.pdf";
  24.  
  25.     /**
  26.      * Creates a PDF document.
  27.      * @param filename the path to the new PDF document
  28.      * @throws DocumentException
  29.      * @throws IOException
  30.      */
  31.     public void createPdf(String filename) throws IOException, DocumentException {
  32.         // step 1
  33.         Document document = new Document(new Rectangle(300, 150));
  34.         // step 2
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  36.         // step 3
  37.         document.open();
  38.         // step 4
  39.         PdfContentByte canvas = writer.getDirectContent();
  40.         Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150);
  41.         TextExample3 text = new TextExample3();
  42.         text.setSize(new Dimension(300, 150));
  43.         text.paint(g2);
  44.         g2.dispose();
  45.         // step 5
  46.         document.close();
  47.     }
  48.     /**
  49.      * Main method.
  50.      *
  51.      * @param    args    no arguments needed
  52.      * @throws DocumentException
  53.      * @throws IOException
  54.      */
  55.     public static void main(String[] args) throws IOException, DocumentException {
  56.         new Text3ToPdf().createPdf(RESULT);
  57.     }
  58. }
TextExample4.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JTextPane;
  15. import javax.swing.text.BadLocationException;
  16. import javax.swing.text.Style;
  17. import javax.swing.text.StyleConstants;
  18. import javax.swing.text.StyleContext;
  19. import javax.swing.text.StyledDocument;
  20.  
  21. public class TextExample4 {
  22.  
  23.     /** A String that needs to be displayed in a PDF document */
  24.     public static final String[] AKIRA = {
  25.         "Akira Kurosawa", " (", "\u9ed2\u6fa4 \u660e", " or ",
  26.         "\u9ed2\u6ca2 \u660e", ", Kurosawa Akira, 23 March 1910" +
  27.         " - 6 September 1998) was a Japanese film director," +
  28.         " producer, screenwriter and editor. In a career that" +
  29.         " spanned 50 years, Kurosawa directed 30 films. " +
  30.         "He is widely regarded as one of the most important and " +
  31.         "influential filmmakers in film history." };
  32.  
  33.     /** Styles that will be used in the AKIRA String. */
  34.     public static final String[] STYLES =  {
  35.       "bold", "regular", "japanese", "regular", "japanese", "regular" };
  36.    
  37.     /**
  38.      * Creates a JTextPane with the String we want to display in a PDF document.
  39.      * @return a JTextPane
  40.      * @throws BadLocationException
  41.      */
  42.     public static JTextPane createTextPane() throws BadLocationException {
  43.         JTextPane textPane = new JTextPane();
  44.         StyledDocument doc = textPane.getStyledDocument();
  45.         initStyles(doc);
  46.         for (int i=0; i < AKIRA.length; i++) {
  47.             doc.insertString(doc.getLength(), AKIRA[i],
  48.             doc.getStyle(STYLES[i]));
  49.         }
  50.         return textPane;
  51.     }
  52.  
  53.     /**
  54.      * Initialization of the styles that are used in the String.
  55.      * @param doc    a StyledDocument
  56.      */
  57.     public static void initStyles(StyledDocument doc) {
  58.         Style def =
  59.             StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  60.         StyleConstants.setFontFamily(def, "SansSerif");
  61.         Style regular = doc.addStyle("regular", def);
  62.         Style bold = doc.addStyle("bold", regular);
  63.         StyleConstants.setBold(bold, true);
  64.         Style japanese = doc.addStyle("japanese", def);
  65.         StyleConstants.setFontFamily(japanese, "MS PGothic");
  66.     }
  67.    
  68.     /**
  69.      * Creates a JFrame and draws a String to it.
  70.      * @param args no arguments needed
  71.      * @throws BadLocationException
  72.      */
  73.     public static void main(String[] args) throws BadLocationException {
  74.         JFrame f = new JFrame("Kurosawa");
  75.         f.getContentPane().add( createTextPane(), "Center" );
  76.  
  77.         f.addWindowListener(new WindowAdapter() {
  78.             public void windowClosing(WindowEvent e) {
  79.                 System.exit(0);
  80.             }
  81.         });
  82.         f.setSize(new Dimension(300, 150));
  83.         f.setVisible(true);
  84.     }
  85.  
  86. }
Text4ToPdf.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. package part4.chapter14;
  8.  
  9. import java.awt.Dimension;
  10. import java.awt.Graphics2D;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import javax.swing.JTextPane;
  15. import javax.swing.text.BadLocationException;
  16.  
  17. import com.itextpdf.awt.DefaultFontMapper;
  18. import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters;
  19. import com.itextpdf.awt.PdfGraphics2D;
  20. import com.itextpdf.text.Document;
  21. import com.itextpdf.text.DocumentException;
  22. import com.itextpdf.text.Rectangle;
  23. import com.itextpdf.text.pdf.BaseFont;
  24. import com.itextpdf.text.pdf.PdfContentByte;
  25. import com.itextpdf.text.pdf.PdfWriter;
  26.  
  27. public class Text4ToPdf {
  28.  
  29.     /** The resulting PDF. */
  30.     public static final String RESULT = "results/part4/chapter14/text4.pdf";
  31.  
  32.     /**
  33.      * Creates a PDF document.
  34.      * @param filename the path to the new PDF document
  35.      * @throws DocumentException
  36.      * @throws IOException
  37.      * @throws BadLocationException
  38.      */
  39.     public void createPdf(String filename) throws IOException, DocumentException, BadLocationException {
  40.         // step 1
  41.         Document document = new Document(new Rectangle(300, 150));
  42.         // step 2
  43.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  44.         // step 3
  45.         document.open();
  46.         // step 4
  47.         PdfContentByte canvas = writer.getDirectContent();
  48.         // create a default font mapper
  49.         DefaultFontMapper mapper = new DefaultFontMapper();
  50.         // and map MS Gothic to the corresponding font program
  51.         BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1");
  52.         parameters.encoding = BaseFont.IDENTITY_H;
  53.         mapper.putName("MS PGothic", parameters );
  54.         Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, mapper);
  55.         // create the text pane and print it.
  56.         JTextPane text = TextExample4.createTextPane();
  57.         text.setSize(new Dimension(300, 150));
  58.         text.print(g2);
  59.         g2.dispose();
  60.         // step 5
  61.         document.close();
  62.     }
  63.     /**
  64.      * Main method.
  65.      *
  66.      * @param    args    no arguments needed
  67.      * @throws DocumentException
  68.      * @throws IOException
  69.      * @throws BadLocationException
  70.      */
  71.     public static void main(String[] args) throws IOException, DocumentException, BadLocationException {
  72.         new Text4ToPdf().createPdf(RESULT);
  73.     }
  74. }
GetContentStream.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 System.Text;
  12. using Ionic.Zip;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15. using kuujinbo.iTextInAction2Ed.Chapter01;
  16. using kuujinbo.iTextInAction2Ed.Chapter05;
  17.  
  18. namespace kuujinbo.iTextInAction2Ed.Chapter14 {
  19.   public class GetContentStream : IWriter {
  20. // ===========================================================================
  21.     /** The content stream of a first PDF. */
  22.     public const String RESULT1 = "contentstream1.txt";
  23.     /** The content stream of a second PDF. */
  24.     public const String RESULT2 = "contentstream2.txt";
  25. // ---------------------------------------------------------------------------    
  26.     public void Write(Stream stream) {
  27.       HelloWorld hello = new HelloWorld();
  28.       Hero1 hero = new Hero1();
  29.       using (ZipFile zip = new ZipFile()) {
  30.         byte[] pdfHello = Utility.PdfBytes(hello);
  31.         byte[] pdfHero = Utility.PdfBytes(hero);
  32.         zip.AddEntry(Utility.ResultFileName(
  33.           hello.ToString() + ".pdf"), pdfHello
  34.         );      
  35.         zip.AddEntry(Utility.ResultFileName(
  36.           hero.ToString() + ".pdf"), pdfHero
  37.         );
  38.         GetContentStream example = new GetContentStream();
  39.         zip.AddEntry(RESULT1, example.ReadContent(pdfHello));
  40.         zip.AddEntry(RESULT2, example.ReadContent(pdfHero));
  41.         zip.Save(stream);            
  42.       }    
  43.     }
  44. // ---------------------------------------------------------------------------    
  45.     /**
  46.      * Reads the content stream of the first page of a PDF into a text file.
  47.      * @param src the PDF file
  48.      */
  49.     public string ReadContent(byte[] src) {
  50.       PdfReader reader = new PdfReader(src);
  51.       byte[] pc = reader.GetPageContent(1);
  52.       return Encoding.UTF8.GetString(pc, 0, pc.Length);
  53.     }
  54. // ===========================================================================
  55.   }
  56. }
PathConstructionAndPainting.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.Chapter14 {
  15.   public class PathConstructionAndPainting : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         PdfContentByte canvas = writer.DirectContent;
  26.         // draw squares
  27.         CreateSquares(canvas, 50, 720, 80, 20);
  28.         ColumnText.ShowTextAligned(
  29.           canvas, Element.ALIGN_LEFT,
  30.           new Phrase(
  31.             "Methods MoveTo(), LineTo(), stroke(), closePathStroke(), Fill(), and closePathFill()"
  32.           ),
  33.           50, 700, 0
  34.         );
  35.         // draw Bezier curves
  36.         createBezierCurves(canvas, 70, 600, 80, 670, 140, 690, 160, 630, 160);
  37.         ColumnText.ShowTextAligned(
  38.           canvas, Element.ALIGN_LEFT,
  39.           new Phrase("Different CurveTo() methods, followed by stroke()"),
  40.           50, 580, 0
  41.         );
  42.         // draw stars and circles
  43.         CreateStarsAndCircles(canvas, 50, 470, 40, 20);
  44.         ColumnText.ShowTextAligned(
  45.           canvas, Element.ALIGN_LEFT,
  46.           new Phrase(
  47.             "Methods Fill(), eoFill(), NewPath(), FillStroke(), and EoFillStroke()"
  48.           ),
  49.           50, 450, 0
  50.         );
  51.         // draw different shapes using convenience methods
  52.         canvas.SaveState();
  53.         canvas.SetColorStroke(new GrayColor(0.2f));
  54.         canvas.SetColorFill(new GrayColor(0.9f));
  55.         canvas.Arc(50, 270, 150, 330, 45, 270);
  56.         canvas.Ellipse(170, 270, 270, 330);
  57.         canvas.Circle(320, 300, 30);
  58.         canvas.RoundRectangle(370, 270, 80, 60, 20);
  59.         canvas.FillStroke();
  60.         canvas.RestoreState();
  61.         Rectangle rect = new Rectangle(470, 270, 550, 330);
  62.         rect.BorderWidthBottom = 10;
  63.         rect.BorderColorBottom = new GrayColor(0f);
  64.         rect.BorderWidthLeft = 4;
  65.         rect.BorderColorLeft = new GrayColor(0.9f);
  66.         rect.BackgroundColor = new GrayColor(0.4f);
  67.         canvas.Rectangle(rect);
  68.         ColumnText.ShowTextAligned(
  69.           canvas, Element.ALIGN_LEFT,
  70.           new Phrase("Convenience methods"), 50, 250, 0
  71.         );
  72.       }
  73.     }
  74. // ---------------------------------------------------------------------------    
  75.     /**
  76.      * Draws a row of squares.
  77.      * @param canvas the canvas to which the squares have to be drawn
  78.      * @param x      X coordinate to position the row
  79.      * @param y      Y coordinate to position the row
  80.      * @param side   the side of the square
  81.      * @param gutter the space between the squares
  82.      */
  83.     public void CreateSquares(PdfContentByte canvas,
  84.       float x, float y, float side, float gutter) {
  85.       canvas.SaveState();
  86.       canvas.SetColorStroke(new GrayColor(0.2f));
  87.       canvas.SetColorFill(new GrayColor(0.9f));
  88.       canvas.MoveTo(x, y);
  89.       canvas.LineTo(x + side, y);
  90.       canvas.LineTo(x + side, y + side);
  91.       canvas.LineTo(x, y + side);
  92.       canvas.Stroke();
  93.       x = x + side + gutter;
  94.       canvas.MoveTo(x, y);
  95.       canvas.LineTo(x + side, y);
  96.       canvas.LineTo(x + side, y + side);
  97.       canvas.LineTo(x, y + side);
  98.       canvas.ClosePathStroke();
  99.       x = x + side + gutter;
  100.       canvas.MoveTo(x, y);
  101.       canvas.LineTo(x + side, y);
  102.       canvas.LineTo(x + side, y + side);
  103.       canvas.LineTo(x, y + side);
  104.       canvas.Fill();
  105.       x = x + side + gutter;
  106.       canvas.MoveTo(x, y);
  107.       canvas.LineTo(x + side, y);
  108.       canvas.LineTo(x + side, y + side);
  109.       canvas.LineTo(x, y + side);
  110.       canvas.FillStroke();
  111.       x = x + side + gutter;
  112.       canvas.MoveTo(x, y);
  113.       canvas.LineTo(x + side, y);
  114.       canvas.LineTo(x + side, y + side);
  115.       canvas.LineTo(x, y + side);
  116.       canvas.ClosePathFillStroke();
  117.       canvas.RestoreState();
  118.     }
  119. // ---------------------------------------------------------------------------    
  120.     /**
  121.      * Draws a series of Bezier curves
  122.      * @param cb the canvas to which the curves have to be drawn
  123.      * @param x0 X coordinate of the start point
  124.      * @param y0 Y coordinate of the start point
  125.      * @param x1 X coordinate of the first control point
  126.      * @param y1 Y coordinate of the first control point
  127.      * @param x2 X coordinate of the second control point
  128.      * @param y2 Y coordinate of the second control point
  129.      * @param x3 X coordinate of the end point
  130.      * @param y3 Y coordinate of the end point
  131.      * @param distance the distance between the curves
  132.      */
  133.     public void createBezierCurves(PdfContentByte cb, float x0, float y0,
  134.         float x1, float y1, float x2, float y2, float x3,
  135.         float y3, float distance)
  136.     {
  137.       cb.MoveTo(x0, y0);
  138.       cb.LineTo(x1, y1);
  139.       cb.MoveTo(x2, y2);
  140.       cb.LineTo(x3, y3);
  141.       cb.MoveTo(x0, y0);
  142.       cb.CurveTo(x1, y1, x2, y2, x3, y3);
  143.       x0 += distance;
  144.       x1 += distance;
  145.       x2 += distance;
  146.       x3 += distance;
  147.       cb.MoveTo(x2, y2);
  148.       cb.LineTo(x3, y3);
  149.       cb.MoveTo(x0, y0);
  150.       cb.CurveTo(x2, y2, x3, y3);
  151.       x0 += distance;
  152.       x1 += distance;
  153.       x2 += distance;
  154.       x3 += distance;
  155.       cb.MoveTo(x0, y0);
  156.       cb.LineTo(x1, y1);
  157.       cb.MoveTo(x0, y0);
  158.       cb.CurveTo(x1, y1, x3, y3);
  159.       cb.Stroke();
  160.     }
  161. // ---------------------------------------------------------------------------    
  162.     /**
  163.      * Draws a row of stars and circles.
  164.      * @param canvas the canvas to which the shapes have to be drawn
  165.      * @param x      X coordinate to position the row
  166.      * @param y      Y coordinate to position the row
  167.      * @param radius the radius of the circles
  168.      * @param gutter the space between the shapes
  169.      */
  170.     public static void CreateStarsAndCircles(PdfContentByte canvas,
  171.         float x, float y, float radius, float gutter)
  172.     {
  173.       canvas.SaveState();
  174.       canvas.SetColorStroke(new GrayColor(0.2f));
  175.       canvas.SetColorFill(new GrayColor(0.9f));
  176.       CreateStar(canvas, x, y);
  177.       CreateCircle(canvas, x + radius, y - 70, radius, true);
  178.       CreateCircle(canvas, x + radius, y - 70, radius / 2, true);
  179.       canvas.Fill();
  180.       x += 2 * radius + gutter;
  181.       CreateStar(canvas, x, y);
  182.       CreateCircle(canvas, x + radius, y - 70, radius, true);
  183.       CreateCircle(canvas, x + radius, y - 70, radius / 2, true);
  184.       canvas.EoFill();
  185.       x += 2 * radius + gutter;
  186.       CreateStar(canvas, x, y);
  187.       canvas.NewPath();
  188.       CreateCircle(canvas, x + radius, y - 70, radius, true);
  189.       CreateCircle(canvas, x + radius, y - 70, radius / 2, true);
  190.       x += 2 * radius + gutter;
  191.       CreateStar(canvas, x, y);
  192.       CreateCircle(canvas, x + radius, y - 70, radius, true);
  193.       CreateCircle(canvas, x + radius, y - 70, radius / 2, false);
  194.       canvas.FillStroke();
  195.       x += 2 * radius + gutter;
  196.       CreateStar(canvas, x, y);
  197.       CreateCircle(canvas, x + radius, y - 70, radius, true);
  198.       CreateCircle(canvas, x + radius, y - 70, radius / 2, true);
  199.       canvas.EoFillStroke();
  200.       canvas.RestoreState();
  201.     }
  202. // ---------------------------------------------------------------------------    
  203.     /**
  204.      * Creates a path for a five pointed star.
  205.      * This method doesn't fill or stroke the star!
  206.      * @param canvas the canvas for which the star is constructed
  207.      * @param x      the X coordinate of the center of the star
  208.      * @param y      the Y coordinate of the center of the star
  209.      */
  210.     public static void CreateStar(PdfContentByte canvas, float x, float y) {
  211.       canvas.MoveTo(x + 10, y);
  212.       canvas.LineTo(x + 80, y + 60);
  213.       canvas.LineTo(x, y + 60);
  214.       canvas.LineTo(x + 70, y);
  215.       canvas.LineTo(x + 40, y + 90);
  216.       canvas.ClosePath();
  217.     }
  218. // ---------------------------------------------------------------------------
  219.     /**
  220.      * Creates a path for circle using Bezier curvers.
  221.      * The path can be constructed clockwise or counter-clockwise.
  222.      * This method doesn't Fill or stroke the circle!
  223.      * @param canvas    the canvas for which the path is constructed
  224.      * @param x         the X coordinate of the center of the circle
  225.      * @param y         the Y coordinate of the center of the circle
  226.      * @param r         the radius
  227.      * @param clockwise true if the circle has to be constructed clockwise
  228.      */
  229.     public static void CreateCircle(PdfContentByte canvas, float x, float y,
  230.       float r, bool clockwise)
  231.     {
  232.       float b = 0.5523f;
  233.       if (clockwise) {
  234.         canvas.MoveTo(x + r, y);
  235.         canvas.CurveTo(x + r, y - r * b, x + r * b, y - r, x, y - r);
  236.         canvas.CurveTo(x - r * b, y - r, x - r, y - r * b, x - r, y);
  237.         canvas.CurveTo(x - r, y + r * b, x - r * b, y + r, x, y + r);
  238.         canvas.CurveTo(x + r * b, y + r, x + r, y + r * b, x + r, y);
  239.       } else {
  240.         canvas.MoveTo(x + r, y);
  241.         canvas.CurveTo(x + r, y + r * b, x + r * b, y + r, x, y + r);
  242.         canvas.CurveTo(x - r * b, y + r, x - r, y + r * b, x - r, y);
  243.         canvas.CurveTo(x - r, y - r * b, x - r * b, y - r, x, y - r);
  244.         canvas.CurveTo(x + r * b, y - r, x + r, y - r * b, x + r, y);
  245.       }
  246.     }    
  247. // ===========================================================================
  248.   }
  249. }
GraphicsStateOperators.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.Chapter14 {
  15.   public class GraphicsStateOperators : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         PdfContentByte canvas = writer.DirectContent;
  26.         // line widths
  27.         canvas.SaveState();
  28.         for (int i = 25; i > 0; i--) {
  29.           canvas.SetLineWidth((float) i / 10);
  30.           canvas.MoveTo(50, 806 - (5 * i));
  31.           canvas.LineTo(320, 806 - (5 * i));
  32.           canvas.Stroke();
  33.         }
  34.         canvas.RestoreState();
  35.        
  36.         // line cap
  37.         canvas.MoveTo(350, 800);
  38.         canvas.LineTo(350, 750);
  39.         canvas.MoveTo(540, 800);
  40.         canvas.LineTo(540, 750);
  41.         canvas.Stroke();
  42.        
  43.         canvas.SaveState();
  44.         canvas.SetLineWidth(8);
  45.         canvas.SetLineCap(PdfContentByte.LINE_CAP_BUTT);
  46.         canvas.MoveTo(350, 790);
  47.         canvas.LineTo(540, 790);
  48.         canvas.Stroke();
  49.         canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
  50.         canvas.MoveTo(350, 775);
  51.         canvas.LineTo(540, 775);
  52.         canvas.Stroke();
  53.         canvas.SetLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
  54.         canvas.MoveTo(350, 760);
  55.         canvas.LineTo(540, 760);
  56.         canvas.Stroke();
  57.         canvas.RestoreState();
  58.        
  59.         // join miter
  60.         canvas.SaveState();
  61.         canvas.SetLineWidth(8);
  62.         canvas.SetLineJoin(PdfContentByte.LINE_JOIN_MITER);
  63.         canvas.MoveTo(387, 700);
  64.         canvas.LineTo(402, 730);
  65.         canvas.LineTo(417, 700);
  66.         canvas.Stroke();
  67.         canvas.SetLineJoin(PdfContentByte.LINE_JOIN_ROUND);
  68.         canvas.MoveTo(427, 700);
  69.         canvas.LineTo(442, 730);
  70.         canvas.LineTo(457, 700);
  71.         canvas.Stroke();
  72.         canvas.SetLineJoin(PdfContentByte.LINE_JOIN_BEVEL);
  73.         canvas.MoveTo(467, 700);
  74.         canvas.LineTo(482, 730);
  75.         canvas.LineTo(497, 700);
  76.         canvas.Stroke();
  77.         canvas.RestoreState();
  78.  
  79.         // line dash
  80.         canvas.SaveState();
  81.         canvas.SetLineWidth(3);
  82.         canvas.MoveTo(50, 660);
  83.         canvas.LineTo(320, 660);
  84.         canvas.Stroke();
  85.         canvas.SetLineDash(6, 0);
  86.         canvas.MoveTo(50, 650);
  87.         canvas.LineTo(320, 650);
  88.         canvas.Stroke();
  89.         canvas.SetLineDash(6, 3);
  90.         canvas.MoveTo(50, 640);
  91.         canvas.LineTo(320, 640);
  92.         canvas.Stroke();
  93.         canvas.SetLineDash(15, 10, 5);
  94.         canvas.MoveTo(50, 630);
  95.         canvas.LineTo(320, 630);
  96.         canvas.Stroke();
  97.         float[] dash1 = { 10, 5, 5, 5, 20 };
  98.         canvas.SetLineDash(dash1, 5);
  99.         canvas.MoveTo(50, 620);
  100.         canvas.LineTo(320, 620);
  101.         canvas.Stroke();
  102.         float[] dash2 = { 9, 6, 0, 6 };
  103.         canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
  104.         canvas.SetLineDash(dash2, 0);
  105.         canvas.MoveTo(50, 610);
  106.         canvas.LineTo(320, 610);
  107.         canvas.Stroke();
  108.         canvas.RestoreState();
  109.  
  110.         // miter limit
  111.         PdfTemplate hooks = canvas.CreateTemplate(300, 120);
  112.         hooks.SetLineWidth(8);
  113.         hooks.MoveTo(46, 50);
  114.         hooks.LineTo(65, 80);
  115.         hooks.LineTo(84, 50);
  116.         hooks.Stroke();
  117.         hooks.MoveTo(87, 50);
  118.         hooks.LineTo(105, 80);
  119.         hooks.LineTo(123, 50);
  120.         hooks.Stroke();
  121.         hooks.MoveTo(128, 50);
  122.         hooks.LineTo(145, 80);
  123.         hooks.LineTo(162, 50);
  124.         hooks.Stroke();
  125.         hooks.MoveTo(169, 50);
  126.         hooks.LineTo(185, 80);
  127.         hooks.LineTo(201, 50);
  128.         hooks.Stroke();
  129.         hooks.MoveTo(210, 50);
  130.         hooks.LineTo(225, 80);
  131.         hooks.LineTo(240, 50);
  132.         hooks.Stroke();
  133.        
  134.         canvas.SaveState();
  135.         canvas.SetMiterLimit(2);
  136.         canvas.AddTemplate(hooks, 300, 600);
  137.         canvas.RestoreState();
  138.        
  139.         canvas.SaveState();
  140.         canvas.SetMiterLimit(2.1f);
  141.         canvas.AddTemplate(hooks, 300, 550);
  142.         canvas.RestoreState();
  143.       }
  144.     }
  145. // ===========================================================================
  146.   }
  147. }
TransformationMatrix1.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 Ionic.Zip;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter14 {
  16.   public class TransformationMatrix1 : IWriter {
  17. // ===========================================================================
  18.     /** The resulting PDF. */
  19.     public const String RESULT = "transformation_matrix1.pdf";
  20.     /** A PDF with the iText logo that will be transformed. */
  21.     public readonly string RESOURCE = Path.Combine(
  22.       Utility.ResourcePdf, "logo.pdf"
  23.     );
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       using (ZipFile zip = new ZipFile()) {
  27.         TransformationMatrix1 t = new TransformationMatrix1();
  28.         zip.AddFile(RESOURCE, "");      
  29.         zip.AddEntry(RESULT, t.CreatePdf());
  30.         zip.Save(stream);            
  31.       }
  32.     }
  33. // ---------------------------------------------------------------------------
  34.     /**
  35.      * Creates a PDF document.
  36.      */
  37.     public byte[] CreatePdf() {
  38.       // step 1
  39.       Rectangle rect = new Rectangle(-595, -842, 595, 842);
  40.       using (MemoryStream ms = new MemoryStream()) {
  41.         using (Document document = new Document(rect)) {
  42.           // step 2
  43.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  44.           // step 3
  45.           document.Open();
  46.           // step 4
  47.           PdfContentByte canvas = writer.DirectContent;
  48.           canvas.MoveTo(-595, 0);
  49.           canvas.LineTo(595, 0);
  50.           canvas.MoveTo(0, -842);
  51.           canvas.LineTo(0, 842);
  52.           canvas.Stroke();
  53.           // Read the PDF containing the logo
  54.           PdfReader reader = new PdfReader(RESOURCE);
  55.           PdfTemplate template = writer.GetImportedPage(reader, 1);
  56.           // add it at different positions using different transformations
  57.           canvas.SaveState();
  58.           canvas.AddTemplate(template, 0, 0);
  59.           canvas.ConcatCTM(0.5f, 0, 0, 0.5f, -595, 0);
  60.           canvas.AddTemplate(template, 0, 0);
  61.           canvas.ConcatCTM(1, 0, 0, 1, 595, 595);
  62.           canvas.AddTemplate(template, 0, 0);
  63.           canvas.RestoreState();
  64.  
  65.           canvas.SaveState();
  66.           canvas.ConcatCTM(1, 0, 0.4f, 1, -750, -650);
  67.           canvas.AddTemplate(template, 0, 0);
  68.           canvas.RestoreState();
  69.          
  70.           canvas.SaveState();
  71.           canvas.ConcatCTM(0, -1, -1, 0, 650, 0);
  72.           canvas.AddTemplate(template, 0, 0);
  73.           canvas.ConcatCTM(0.2f, 0, 0, 0.5f, 0, 300);
  74.           canvas.AddTemplate(template, 0, 0);
  75.           canvas.RestoreState();
  76.         }
  77.         return ms.ToArray();
  78.       }
  79.     }
  80. // ===========================================================================
  81.   }
  82. }
TransformationMatrix2.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 Ionic.Zip;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter14 {
  16.   public class TransformationMatrix2 : IWriter {
  17. // ===========================================================================
  18.     /** The resulting PDF. */
  19.     public const String RESULT = "transformation_matrix2.pdf";
  20.     /** A PDF with the iText logo that will be transformed. */
  21.     public readonly string RESOURCE = Path.Combine(
  22.       Utility.ResourcePdf, "logo.pdf"
  23.     );
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       using (ZipFile zip = new ZipFile()) {
  27.         TransformationMatrix2 t = new TransformationMatrix2();
  28.         zip.AddFile(RESOURCE, "");      
  29.         zip.AddEntry(RESULT, t.CreatePdf());
  30.         zip.Save(stream);            
  31.       }
  32.     }    
  33. // ---------------------------------------------------------------------------
  34.     /**
  35.      * Creates a PDF document.
  36.      */
  37.     public byte[] CreatePdf() {
  38.       Rectangle rect = new Rectangle(-595, -842, 595, 842);
  39.       using (MemoryStream ms = new MemoryStream()) {
  40.         using (Document document = new Document(rect)) {
  41.           // step 2
  42.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  43.           // step 3
  44.           document.Open();
  45.           // step 4
  46.           PdfContentByte canvas = writer.DirectContent;
  47.           canvas.MoveTo(-595, 0);
  48.           canvas.LineTo(595, 0);
  49.           canvas.MoveTo(0, -842);
  50.           canvas.LineTo(0, 842);
  51.           canvas.Stroke();
  52.          
  53.           // read the PDF with the logo
  54.           PdfReader reader = new PdfReader(RESOURCE);
  55.           PdfTemplate template = writer.GetImportedPage(reader, 1);
  56.           // add it at different positions using different transformations
  57.           canvas.AddTemplate(template, 0, 0);
  58.           canvas.AddTemplate(template, 0.5f, 0, 0, 0.5f, -595, 0);
  59.           canvas.AddTemplate(template, 0.5f, 0, 0, 0.5f, -297.5f, 297.5f);
  60.           canvas.AddTemplate(template, 1, 0, 0.4f, 1, -750, -650);
  61.           canvas.AddTemplate(template, 0, -1, -1, 0, 650, 0);
  62.           canvas.AddTemplate(template, 0, -0.2f, -0.5f, 0, 350, 0);
  63.         }
  64.         return ms.ToArray();
  65.       }
  66.     }
  67. // ===========================================================================
  68.   }
  69. }
TransformationMatrix3.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.awt.geom;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13.  
  14. namespace kuujinbo.iTextInAction2Ed.Chapter14 {
  15.   public class TransformationMatrix3 : IWriter {
  16. // ===========================================================================
  17.     /** The resulting PDF. */
  18.     public const String RESULT = "transformation_matrix3.pdf";
  19.     /** A PDF with the iText logo that will be transformed. */
  20.     public readonly string RESOURCE = Path.Combine(
  21.       Utility.ResourcePdf, "logo.pdf"
  22.     );
  23. // ---------------------------------------------------------------------------
  24.     public void Write(Stream stream) {
  25.       using (ZipFile zip = new ZipFile()) {
  26.         TransformationMatrix3 t = new TransformationMatrix3();
  27.         zip.AddFile(RESOURCE, "");      
  28.         zip.AddEntry(RESULT, t.CreatePdf());
  29.         zip.Save(stream);            
  30.       }
  31.     }    
  32. // ---------------------------------------------------------------------------    
  33.     /**
  34.      * Creates a PDF document.
  35.      */
  36.     public byte[] CreatePdf() {
  37.       // step 1
  38.       Rectangle rect = new Rectangle(-595, -842, 595, 842);
  39.       using (MemoryStream ms = new MemoryStream()) {
  40.         using (Document document = new Document(rect)) {
  41.           // step 2
  42.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  43.           // step 3
  44.           document.Open();
  45.           // step 4
  46.           PdfContentByte canvas = writer.DirectContent;
  47.           // draw coordinate system
  48.           canvas.MoveTo(-595, 0);
  49.           canvas.LineTo(595, 0);
  50.           canvas.MoveTo(0, -842);
  51.           canvas.LineTo(0, 842);
  52.           canvas.Stroke();
  53.           // read the PDF with the logo
  54.           PdfReader reader = new PdfReader(RESOURCE);
  55.           PdfTemplate template = writer.GetImportedPage(reader, 1);
  56.           // add it
  57.           canvas.SaveState();
  58.           canvas.AddTemplate(template, 0, 0);
  59.           AffineTransform af = new AffineTransform();
  60.           af.Translate(-595, 0);
  61.           af.Scale(0.5f, 0.5f);
  62.           canvas.Transform(af);
  63.           canvas.AddTemplate(template, 0, 0);
  64.           canvas.ConcatCTM(AffineTransform.GetTranslateInstance(595, 595));
  65.           canvas.AddTemplate(template, 0, 0);
  66.           canvas.RestoreState();
  67.  
  68.           canvas.SaveState();
  69.           af = new AffineTransform(1f, 0f, 0.4f, 1f, -750f, -650f);
  70.           canvas.AddTemplate(template, af);
  71.           canvas.RestoreState();
  72.          
  73.           canvas.SaveState();
  74.           af = new AffineTransform(0, -1, -1, 0, 650, 0);
  75.           canvas.AddTemplate(template, af);
  76.           af = new AffineTransform(0, -0.2f, -0.5f, 0, 350, 0);
  77.           canvas.AddTemplate(template, af);
  78.           canvas.RestoreState();        
  79.         }
  80.         return ms.ToArray();
  81.       }
  82.     }
  83. // ===========================================================================
  84.   }
  85. }
TextStateOperators.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.Chapter14 {
  15.   public class TextStateOperators : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         PdfContentByte canvas = writer.DirectContent;
  26.         String text = "AWAY again";
  27.         BaseFont bf = BaseFont.CreateFont();
  28.         canvas.BeginText();
  29.         // line 1
  30.         canvas.SetFontAndSize(bf, 16);
  31.         canvas.MoveText(36, 806);
  32.         canvas.MoveTextWithLeading(0, -24);
  33.         canvas.ShowText(text);
  34.         // line 2
  35.         canvas.SetWordSpacing(20);
  36.         canvas.NewlineShowText(text);
  37.         // line 3
  38.         canvas.SetCharacterSpacing(10);
  39.         canvas.NewlineShowText(text);
  40.         canvas.SetWordSpacing(0);
  41.         canvas.SetCharacterSpacing(0);
  42.         // line 4
  43.         canvas.SetHorizontalScaling(50);
  44.         canvas.NewlineShowText(text);
  45.         canvas.SetHorizontalScaling(100);
  46.         // line 5
  47.         canvas.NewlineShowText(text);
  48.         canvas.SetTextRise(15);
  49.         canvas.SetFontAndSize(bf, 12);
  50.         canvas.SetColorFill(BaseColor.RED);
  51.         canvas.ShowText("2");
  52.         canvas.SetColorFill(GrayColor.GRAYBLACK);
  53.         // line 6
  54.         canvas.SetLeading(56);
  55.         canvas.NewlineShowText("Changing the leading: " + text);
  56.         canvas.SetLeading(24);
  57.         canvas.NewlineText();
  58.         // line 7
  59.         PdfTextArray array = new PdfTextArray("A");
  60.         array.Add(120);
  61.         array.Add("W");
  62.         array.Add(120);
  63.         array.Add("A");
  64.         array.Add(95);
  65.         array.Add("Y again");
  66.         canvas.ShowText(array);
  67.         canvas.EndText();
  68.  
  69.         canvas.SetColorFill(BaseColor.BLUE);
  70.         canvas.BeginText();
  71.         canvas.SetTextMatrix(360, 770);
  72.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
  73.         canvas.SetFontAndSize(bf, 24);
  74.         canvas.ShowText(text);
  75.         canvas.EndText();
  76.  
  77.         canvas.BeginText();
  78.         canvas.SetTextMatrix(360, 730);
  79.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
  80.         canvas.SetFontAndSize(bf, 24);
  81.         canvas.ShowText(text);
  82.         canvas.EndText();
  83.  
  84.         canvas.BeginText();
  85.         canvas.SetTextMatrix(360, 690);
  86.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
  87.         canvas.SetFontAndSize(bf, 24);
  88.         canvas.ShowText(text);
  89.         canvas.EndText();
  90.  
  91.         canvas.BeginText();
  92.         canvas.SetTextMatrix(360, 650);
  93.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
  94.         canvas.SetFontAndSize(bf, 24);
  95.         canvas.ShowText(text);
  96.         canvas.EndText();
  97.  
  98.         PdfTemplate template = canvas.CreateTemplate(200, 36);
  99.         template.SetLineWidth(2);
  100.         for (int i = 0; i < 6; i++) {
  101.             template.MoveTo(0, i * 6);
  102.             template.LineTo(200, i * 6);
  103.         }
  104.         template.Stroke();
  105.        
  106.         canvas.SaveState();
  107.         canvas.BeginText();
  108.         canvas.SetTextMatrix(360, 610);
  109.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP);
  110.         canvas.SetFontAndSize(bf, 24);
  111.         canvas.ShowText(text);
  112.         canvas.EndText();
  113.         canvas.AddTemplate(template, 360, 610);
  114.         canvas.RestoreState();
  115.  
  116.         canvas.SaveState();
  117.         canvas.BeginText();
  118.         canvas.SetTextMatrix(360, 570);
  119.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP);
  120.         canvas.SetFontAndSize(bf, 24);
  121.         canvas.ShowText(text);
  122.         canvas.EndText();
  123.         canvas.AddTemplate(template, 360, 570);
  124.         canvas.RestoreState();
  125.  
  126.         canvas.SaveState();
  127.         canvas.BeginText();
  128.         canvas.SetTextMatrix(360, 530);
  129.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP);
  130.         canvas.SetFontAndSize(bf, 24);
  131.         canvas.ShowText(text);
  132.         canvas.EndText();
  133.         canvas.AddTemplate(template, 360, 530);
  134.         canvas.RestoreState();
  135.        
  136.         canvas.SaveState();
  137.         canvas.BeginText();
  138.         canvas.SetTextMatrix(360, 490);
  139.         canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP);
  140.         canvas.SetFontAndSize(bf, 24);
  141.         canvas.ShowText(text);
  142.         canvas.EndText();
  143.         canvas.AddTemplate(template, 360, 490);
  144.         canvas.RestoreState();
  145.  
  146.       }
  147.     }
  148. // ===========================================================================
  149.   }
  150. }
TextMethods.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.Chapter14 {
  15.   public class TextMethods : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       // step 1
  19.       using (Document document = new Document()) {
  20.         // step 2
  21.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  22.         // step 3
  23.         document.Open();
  24.         // step 4
  25.         // draw helper lines
  26.         PdfContentByte cb = writer.DirectContent;
  27.         cb.SetLineWidth(0f);
  28.         cb.MoveTo(150, 600);
  29.         cb.LineTo(150, 800);
  30.         cb.MoveTo(50, 760);
  31.         cb.LineTo(250, 760);
  32.         cb.MoveTo(50, 700);
  33.         cb.LineTo(250, 700);
  34.         cb.MoveTo(50, 640);
  35.         cb.LineTo(250, 640);
  36.         cb.Stroke();
  37.         // draw text
  38.         String text = "AWAY again ";
  39.         BaseFont bf = BaseFont.CreateFont();
  40.         cb.BeginText();
  41.         cb.SetFontAndSize(bf, 12);
  42.         cb.SetTextMatrix(50, 800);
  43.         cb.ShowText(text);
  44.         cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 150, 760, 0);
  45.         cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 150, 700, 0);
  46.         cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 150, 640, 0);
  47.         cb.ShowTextAlignedKerned(PdfContentByte.ALIGN_LEFT, text + " Left", 150, 628, 0);
  48.         cb.SetTextMatrix(0, 1, -1, 0, 300, 600);
  49.         cb.ShowText("Position 300,600, rotated 90 degrees.");
  50.         for (int i = 0; i < 360; i += 30) {
  51.             cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 400, 700, i);
  52.         }
  53.         cb.EndText();
  54.       }
  55.     }
  56. // ===========================================================================
  57.   }
  58. }
PearExample.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.Chapter14 {
  15.   public class PearExample : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
PearToPdf.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.Chapter14 {
  15.   public class PearToPdf : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
DirectorCharts.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.Chapter14 {
  15.   public class DirectorCharts : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Graphics2DFonts.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.Chapter14 {
  15.   public class Graphics2DFonts : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
TextExample1.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.Chapter14 {
  15.   public class TextExample1 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text1ToPdf1.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.Chapter14 {
  15.   public class Text1ToPdf1 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text1ToPdf2.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.Chapter14 {
  15.   public class Text1ToPdf2 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
TextExample2.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.Chapter14 {
  15.   public class TextExample2 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text2ToPdf1.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.Chapter14 {
  15.   public class Text2ToPdf1 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text2ToPdf2.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.Chapter14 {
  15.   public class Text2ToPdf2 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text2ToPdf3.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.Chapter14 {
  15.   public class Text2ToPdf3 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
TextExample3.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.Chapter14 {
  15.   public class TextExample3 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text3ToPdf.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.Chapter14 {
  15.   public class Text3ToPdf : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
TextExample4.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.Chapter14 {
  15.   public class TextExample4 : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
Text4ToPdf.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.Chapter14 {
  15.   public class Text4ToPdf : IWriter {
  16. // ===========================================================================
  17.     public void Write(Stream stream) {
  18.       throw new NotImplementedException(
  19.         "iTextSharp does not implement Java Graphics2D class"
  20.       );
  21.     }
  22. // ===========================================================================
  23.   }
  24. }
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