Chapter 10: Brightening up your PDF with color and images

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

10th October 2015
admin-marketing

Switch code for this example

DeviceColor.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.pdf.CMYKColor;
  17. import com.itextpdf.text.pdf.GrayColor;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class DeviceColor {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter10/device_color.pdf";
  26.    
  27.     /**
  28.      * Draws a colored rectangle.
  29.      * @param canvas the canvas to draw on
  30.      * @param color the Color
  31.      * @param x the X coordinate
  32.      * @param y the Y coordinate
  33.      * @param width the width of the rectangle
  34.      * @param height the height of the rectangle
  35.      */
  36.     public void colorRectangle(PdfContentByte canvas,
  37.         BaseColor color, float x, float y, float width, float height) {
  38.         canvas.saveState();
  39.         canvas.setColorFill(color);
  40.         canvas.rectangle(x, y, width, height);
  41.         canvas.fillStroke();
  42.         canvas.restoreState();
  43.     }
  44.  
  45.     /**
  46.      * Creates a PDF document.
  47.      * @param filename the path to the new PDF document
  48.      * @throws    DocumentException
  49.      * @throws    IOException
  50.      */
  51.     public void createPdf(String filename) throws IOException, DocumentException {
  52.         // step 1
  53.         Document document = new Document();
  54.         // step 2
  55.         PdfWriter writer
  56.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  57.         // step 3
  58.         document.open();
  59.         // step 4
  60.         PdfContentByte canvas = writer.getDirectContent();
  61.         // RGB Colors
  62.         colorRectangle(canvas, new BaseColor(0x00, 0x00, 0x00), 36, 770, 36, 36);
  63.         colorRectangle(canvas, new BaseColor(0x00, 0x00, 0xFF), 90, 770, 36, 36);
  64.         colorRectangle(canvas, new BaseColor(0x00, 0xFF, 0x00), 144, 770, 36, 36);
  65.         colorRectangle(canvas, new BaseColor(0xFF, 0x00, 0x00), 198, 770, 36, 36);
  66.         colorRectangle(canvas, new BaseColor(0f, 1f, 1f), 252, 770, 36, 36);
  67.         colorRectangle(canvas, new BaseColor(1f, 0f, 1f), 306, 770, 36, 36);
  68.         colorRectangle(canvas, new BaseColor(1f, 1f, 0f), 360, 770, 36, 36);
  69.         colorRectangle(canvas, BaseColor.BLACK, 416, 770, 36, 36);
  70.         colorRectangle(canvas, BaseColor.LIGHT_GRAY, 470, 770, 36, 36);
  71.         // CMYK Colors
  72.         colorRectangle(canvas, new CMYKColor(0x00, 0x00, 0x00, 0x00), 36, 716, 36, 36);
  73.         colorRectangle(canvas, new CMYKColor(0x00, 0x00, 0xFF, 0x00), 90, 716, 36, 36);
  74.         colorRectangle(canvas, new CMYKColor(0x00, 0x00, 0xFF, 0xFF), 144, 716, 36, 36);
  75.         colorRectangle(canvas, new CMYKColor(0x00, 0xFF, 0x00, 0x00), 198, 716, 36, 36);
  76.         colorRectangle(canvas, new CMYKColor(0f, 1f, 0f, 0.5f), 252, 716, 36, 36);
  77.         colorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 0f), 306, 716, 36, 36);
  78.         colorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 0.5f), 360, 716, 36, 36);
  79.         colorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 1f), 416, 716, 36, 36);
  80.         colorRectangle(canvas, new CMYKColor(0f, 0f, 0f, 1f), 470, 716, 36, 36);
  81.         // Gray color
  82.         colorRectangle(canvas, new GrayColor(0x20), 36, 662, 36, 36);
  83.         colorRectangle(canvas, new GrayColor(0x40), 90, 662, 36, 36);
  84.         colorRectangle(canvas, new GrayColor(0x60), 144, 662, 36, 36);
  85.         colorRectangle(canvas, new GrayColor(0.5f), 198, 662, 36, 36);
  86.         colorRectangle(canvas, new GrayColor(0.625f), 252, 662, 36, 36);
  87.         colorRectangle(canvas, new GrayColor(0.75f), 306, 662, 36, 36);
  88.         colorRectangle(canvas, new GrayColor(0.825f), 360, 662, 36, 36);
  89.         colorRectangle(canvas, GrayColor.GRAYBLACK, 416, 662, 36, 36);
  90.         colorRectangle(canvas, GrayColor.GRAYWHITE, 470, 662, 36, 36);
  91.         // Alternative ways to color the rectangle
  92.         canvas.setRGBColorFill(0x00, 0x80, 0x80);
  93.         canvas.rectangle(36, 608, 36, 36);
  94.         canvas.fillStroke();
  95.         canvas.setRGBColorFillF(0.5f, 0.25f, 0.60f);
  96.         canvas.rectangle(90, 608, 36, 36);
  97.         canvas.fillStroke();
  98.         canvas.setGrayFill(0.5f);
  99.         canvas.rectangle(144, 608, 36, 36);
  100.         canvas.fillStroke();
  101.         canvas.setCMYKColorFill(0xFF, 0xFF, 0x00, 0x80);
  102.         canvas.rectangle(198, 608, 36, 36);
  103.         canvas.fillStroke();
  104.         canvas.setCMYKColorFillF(0f, 1f, 1f, 0.5f);
  105.         canvas.rectangle(252, 608, 36, 36);
  106.         canvas.fillStroke();
  107.  
  108.         // step 5
  109.         document.close();
  110.     }
  111.  
  112.     /**
  113.      * Main method.
  114.      *
  115.      * @param    args    no arguments needed
  116.      * @throws DocumentException
  117.      * @throws IOException
  118.      * @throws DocumentException
  119.      * @throws IOException
  120.      */
  121.     public static void main(String[] args) throws IOException, DocumentException {
  122.         new DeviceColor().createPdf(RESULT);
  123.     }
  124. }
SeparationColor.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.pdf.CMYKColor;
  17. import com.itextpdf.text.pdf.GrayColor;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfSpotColor;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import com.itextpdf.text.pdf.SpotColor;
  22.  
  23. public class SeparationColor extends DeviceColor {
  24.    
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part3/chapter10/separation_color.pdf";
  28.  
  29.   /**
  30.    * Creates a PDF document.
  31.    * @param filename the path to the new PDF document
  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
  40.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         PdfContentByte canvas = writer.getDirectContent();
  45.         PdfSpotColor psc_g = new PdfSpotColor(
  46.             "iTextSpotColorGray", new GrayColor(0.9f));
  47.         PdfSpotColor psc_rgb = new PdfSpotColor(
  48.             "iTextSpotColorRGB", new BaseColor(0x64, 0x95, 0xed));
  49.         PdfSpotColor psc_cmyk = new PdfSpotColor(
  50.             "iTextSpotColorCMYK", new CMYKColor(0.3f, .9f, .3f, .1f));
  51.  
  52.         colorRectangle(canvas, new SpotColor(psc_g, 0.5f), 36, 770, 36, 36);
  53.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.1f), 90, 770, 36, 36);
  54.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.2f), 144, 770, 36, 36);
  55.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.3f), 198, 770, 36, 36);
  56.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.4f), 252, 770, 36, 36);
  57.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.5f), 306, 770, 36, 36);
  58.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.6f), 360, 770, 36, 36);
  59.         colorRectangle(canvas, new SpotColor(psc_rgb, 0.7f), 416, 770, 36, 36);
  60.         colorRectangle(canvas, new SpotColor(psc_cmyk, 0.25f), 470, 770, 36, 36);
  61.        
  62.         canvas.setColorFill(psc_g, 0.5f);
  63.         canvas.rectangle(36, 716, 36, 36);
  64.         canvas.fillStroke();
  65.         canvas.setColorFill(psc_g, 0.9f);
  66.         canvas.rectangle(90, 716, 36, 36);
  67.         canvas.fillStroke();
  68.         canvas.setColorFill(psc_rgb, 0.5f);
  69.         canvas.rectangle(144, 716, 36, 36);
  70.         canvas.fillStroke();
  71.         canvas.setColorFill(psc_rgb, 0.9f);
  72.         canvas.rectangle(198, 716, 36, 36);
  73.         canvas.fillStroke();
  74.         canvas.setColorFill(psc_cmyk, 0.5f);
  75.         canvas.rectangle(252, 716, 36, 36);
  76.         canvas.fillStroke();
  77.         canvas.setColorFill(psc_cmyk, 0.9f);
  78.         canvas.rectangle(306, 716, 36, 36);
  79.         canvas.fillStroke();
  80.  
  81.         // step 5
  82.         document.close();
  83.     }
  84.  
  85.     /**
  86.      * Main method.
  87.      *
  88.      * @param    args    no arguments needed
  89.      * @throws DocumentException
  90.      * @throws IOException
  91.      * @throws DocumentException
  92.      * @throws IOException
  93.      * @throws DocumentException
  94.      * @throws IOException
  95.      */
  96.     public static void main(String[] args) throws IOException, DocumentException {
  97.         new SeparationColor().createPdf(RESULT);
  98.     }
  99. }
TilingPatternColor.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Image;
  17. import com.itextpdf.text.pdf.PatternColor;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfPatternPainter;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class TilingPatternColor extends DeviceColor {
  23.  
  24.     /** The resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part3/chapter10/tiling_pattern.pdf";
  27.  
  28.     /** An image that will be used for a pattern color. */
  29.     public static final String RESOURCE
  30.         = "resources/img/info.png";
  31.  
  32.     /**
  33.      * Creates a PDF document.
  34.      * @param filename the path to the new PDF document
  35.      * @throws    DocumentException
  36.      * @throws    IOException
  37.      */
  38.     public void createPdf(String filename) throws IOException, DocumentException {
  39.         // step 1
  40.         Document document = new Document();
  41.         // step 2
  42.         PdfWriter writer
  43.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  44.         // step 3
  45.         document.open();
  46.         // step 4
  47.         PdfContentByte canvas = writer.getDirectContent();
  48.         PdfPatternPainter square
  49.             = canvas.createPattern(15, 15);
  50.         square.setColorFill(new BaseColor(0xFF, 0xFF, 0x00));
  51.         square.setColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  52.         square.rectangle(5, 5, 5, 5);
  53.         square.fillStroke();
  54.        
  55.         PdfPatternPainter ellipse
  56.             = canvas.createPattern(15, 10, 20, 25);
  57.         ellipse.setColorFill(new BaseColor(0xFF, 0xFF, 0x00));
  58.         ellipse.setColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  59.         ellipse.ellipse(2f, 2f, 13f, 8f);
  60.         ellipse.fillStroke();
  61.        
  62.         PdfPatternPainter circle
  63.             = canvas.createPattern(15, 15, 10, 20, BaseColor.BLUE);
  64.         circle.circle(7.5f, 7.5f, 2.5f);
  65.         circle.fill();
  66.        
  67.         PdfPatternPainter line
  68.             = canvas.createPattern(5, 10, null);
  69.         line.setLineWidth(1);
  70.         line.moveTo(3, -1);
  71.         line.lineTo(3, 11);
  72.         line.stroke();
  73.        
  74.         Image img = Image.getInstance(RESOURCE);
  75.         img.scaleAbsolute(20, 20);
  76.         img.setAbsolutePosition(0, 0);
  77.         PdfPatternPainter img_pattern
  78.             = canvas.createPattern(20, 20, 20, 20);
  79.         img_pattern.addImage(img);
  80.         img_pattern.setPatternMatrix(-0.5f, 0f, 0f, 0.5f, 0f, 0f);
  81.        
  82.         colorRectangle(canvas, new PatternColor(square), 36, 696, 126, 126);
  83.         colorRectangle(canvas, new PatternColor(ellipse), 180, 696, 126, 126);
  84.         colorRectangle(canvas, new PatternColor(circle), 324, 696, 126, 126);
  85.         colorRectangle(canvas, new PatternColor(line), 36, 552, 126, 126);
  86.         colorRectangle(canvas, new PatternColor(img_pattern), 36, 408, 126, 126);
  87.  
  88.         canvas.setPatternFill(line, BaseColor.RED);
  89.         canvas.ellipse(180, 552, 306, 678);
  90.         canvas.fillStroke();
  91.         canvas.setPatternFill(circle, BaseColor.GREEN);
  92.         canvas.ellipse(324, 552, 450, 678);
  93.         canvas.fillStroke();
  94.        
  95.         canvas.setPatternFill(img_pattern);
  96.         canvas.ellipse(180, 408, 450, 534);
  97.         canvas.fillStroke();
  98.  
  99.         // step 5
  100.         document.close();
  101.     }
  102.    
  103.     /**
  104.      * Main method.
  105.      *
  106.      * @param    args    no arguments needed
  107.      * @throws DocumentException
  108.      * @throws IOException
  109.      * @throws DocumentException
  110.      * @throws IOException
  111.      */
  112.     public static void main(String[] args) throws IOException, DocumentException {
  113.         new TilingPatternColor().createPdf(RESULT);
  114.     }
  115. }
ShadingPatternColor.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfShading;
  18. import com.itextpdf.text.pdf.PdfShadingPattern;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import com.itextpdf.text.pdf.ShadingColor;
  21.  
  22. public class ShadingPatternColor extends DeviceColor {
  23.  
  24.     /** The resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part3/chapter10/shading_pattern.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();
  37.         // step 2
  38.         PdfWriter writer
  39.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         PdfContentByte canvas = writer.getDirectContent();
  44.         PdfShading axial = PdfShading.simpleAxial(writer, 36, 716, 396,
  45.                 788, BaseColor.ORANGE, BaseColor.BLUE);
  46.         canvas.paintShading(axial);
  47.         document.newPage();
  48.         PdfShading radial = PdfShading.simpleRadial(writer,
  49.             200, 700, 50, 300, 700, 100,
  50.             new BaseColor(0xFF, 0xF7, 0x94),
  51.             new BaseColor(0xF7, 0x8A, 0x6B),
  52.             false, false);
  53.         canvas.paintShading(radial);
  54.  
  55.         PdfShadingPattern shading = new PdfShadingPattern(axial);
  56.         colorRectangle(canvas, new ShadingColor(shading), 150, 420, 126, 126);
  57.         canvas.setShadingFill(shading);
  58.         canvas.rectangle(300, 420, 126, 126);
  59.         canvas.fillStroke();
  60.         // step 5
  61.         document.close();
  62.     }
  63.    
  64.     /**
  65.      * Main method.
  66.      *
  67.      * @param    args    no arguments needed
  68.      * @throws DocumentException
  69.      * @throws IOException
  70.      * @throws DocumentException
  71.      * @throws IOException
  72.      */
  73.     public static void main(String[] args) throws IOException, DocumentException {
  74.         new ShadingPatternColor().createPdf(RESULT);
  75.     }
  76. }
Transparency1.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Element;
  17. import com.itextpdf.text.Phrase;
  18. import com.itextpdf.text.pdf.ColumnText;
  19. import com.itextpdf.text.pdf.GrayColor;
  20. import com.itextpdf.text.pdf.PdfContentByte;
  21. import com.itextpdf.text.pdf.PdfGState;
  22. import com.itextpdf.text.pdf.PdfTemplate;
  23. import com.itextpdf.text.pdf.PdfTransparencyGroup;
  24. import com.itextpdf.text.pdf.PdfWriter;
  25.  
  26. public class Transparency1 {
  27.  
  28.     /** The resulting PDF file. */
  29.     public static final String RESULT
  30.         = "results/part3/chapter10/transparency1.pdf";
  31.    
  32.     public static void main(String[] args) {
  33.         // step 1
  34.         Document document = new Document();
  35.         try {
  36.             // step 2
  37.             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  38.             // step 3
  39.             document.open();
  40.             // step 4
  41.             PdfContentByte cb = writer.getDirectContent();
  42.             float gap = (document.getPageSize().getWidth() - 400) / 3;
  43.  
  44.             pictureBackdrop(gap, 500, cb);
  45.             pictureBackdrop(200 + 2 * gap, 500, cb);
  46.             pictureBackdrop(gap, 500 - 200 - gap, cb);
  47.             pictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb);
  48.  
  49.             pictureCircles(gap, 500, cb);
  50.             cb.saveState();
  51.             PdfGState gs1 = new PdfGState();
  52.             gs1.setFillOpacity(0.5f);
  53.             cb.setGState(gs1);
  54.             pictureCircles(200 + 2 * gap, 500, cb);
  55.             cb.restoreState();
  56.  
  57.             cb.saveState();
  58.             PdfTemplate tp = cb.createTemplate(200, 200);
  59.             PdfTransparencyGroup group = new PdfTransparencyGroup();
  60.             tp.setGroup(group);
  61.             pictureCircles(0, 0, tp);
  62.             cb.setGState(gs1);
  63.             cb.addTemplate(tp, gap, 500 - 200 - gap);
  64.             cb.restoreState();
  65.  
  66.             cb.saveState();
  67.             tp = cb.createTemplate(200, 200);
  68.             tp.setGroup(group);
  69.             PdfGState gs2 = new PdfGState();
  70.             gs2.setFillOpacity(0.5f);
  71.             gs2.setBlendMode(PdfGState.BM_HARDLIGHT);
  72.             tp.setGState(gs2);
  73.             pictureCircles(0, 0, tp);
  74.             cb.addTemplate(tp, 200 + 2 * gap, 500 - 200 - gap);
  75.             cb.restoreState();
  76.  
  77.             cb.resetRGBColorFill();
  78.             ColumnText ct = new ColumnText(cb);
  79.             Phrase ph = new Phrase("Ungrouped objects\nObject opacity = 1.0");
  80.             ct.setSimpleColumn(ph, gap, 0, gap + 200, 500, 18, Element.ALIGN_CENTER);
  81.             ct.go();
  82.  
  83.             ph = new Phrase("Ungrouped objects\nObject opacity = 0.5");
  84.             ct.setSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200, 500,
  85.                 18, Element.ALIGN_CENTER);
  86.             ct.go();
  87.  
  88.             ph = new Phrase(
  89.                 "Transparency group\nObject opacity = 1.0\nGroup opacity = 0.5\nBlend mode = Normal");
  90.             ct.setSimpleColumn(ph, gap, 0, gap + 200, 500 - 200 - gap, 18, Element.ALIGN_CENTER);
  91.             ct.go();
  92.  
  93.             ph = new Phrase(
  94.                 "Transparency group\nObject opacity = 0.5\nGroup opacity = 1.0\nBlend mode = HardLight");
  95.             ct.setSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200, 500 - 200 - gap,
  96.                 18, Element.ALIGN_CENTER);
  97.             ct.go();
  98.         } catch (DocumentException de) {
  99.             System.err.println(de.getMessage());
  100.         } catch (IOException ioe) {
  101.             System.err.println(ioe.getMessage());
  102.         }
  103.  
  104.         // step 5: we close the document
  105.         document.close();
  106.     }
  107.  
  108.     /**
  109.      * Prints a square and fills half of it with a gray rectangle.
  110.      *
  111.      * @param x
  112.      * @param y
  113.      * @param cb
  114.      * @throws Exception
  115.      */
  116.     public static void pictureBackdrop(float x, float y, PdfContentByte cb) {
  117.         cb.setColorStroke(GrayColor.GRAYBLACK);
  118.         cb.setColorFill(new GrayColor(0.8f));
  119.         cb.rectangle(x, y, 100, 200);
  120.         cb.fill();
  121.         cb.setLineWidth(2);
  122.         cb.rectangle(x, y, 200, 200);
  123.         cb.stroke();
  124.     }
  125.  
  126.     /**
  127.      * Prints 3 circles in different colors that intersect with eachother.
  128.      *
  129.      * @param x
  130.      * @param y
  131.      * @param cb
  132.      * @throws Exception
  133.      */
  134.     public static void pictureCircles(float x, float y, PdfContentByte cb) {
  135.         cb.setColorFill(BaseColor.RED);
  136.         cb.circle(x + 70, y + 70, 50);
  137.         cb.fill();
  138.         cb.setColorFill(BaseColor.YELLOW);
  139.         cb.circle(x + 100, y + 130, 50);
  140.         cb.fill();
  141.         cb.setColorFill(BaseColor.BLUE);
  142.         cb.circle(x + 130, y + 70, 50);
  143.         cb.fill();
  144.     }
  145.  
  146. }
Transparency2.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.pdf.CMYKColor;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfGState;
  19. import com.itextpdf.text.pdf.PdfShading;
  20. import com.itextpdf.text.pdf.PdfShadingPattern;
  21. import com.itextpdf.text.pdf.PdfTemplate;
  22. import com.itextpdf.text.pdf.PdfTransparencyGroup;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. public class Transparency2 {
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String RESULT
  29.         = "results/part3/chapter10/transparency2.pdf";
  30.    
  31.     public static void main(String[] args) {
  32.         // step 1
  33.         Document document = new Document();
  34.         try {
  35.             // step 2
  36.             PdfWriter writer = PdfWriter.getInstance(
  37.                     document,
  38.                     new FileOutputStream(RESULT));
  39.             // step 3
  40.             document.open();
  41.             // step 4
  42.             PdfContentByte cb = writer.getDirectContent();
  43.             float gap = (document.getPageSize().getWidth() - 400) / 3;
  44.  
  45.             pictureBackdrop(gap, 500, cb, writer);
  46.             pictureBackdrop(200 + 2 * gap, 500, cb, writer);
  47.             pictureBackdrop(gap, 500 - 200 - gap, cb, writer);
  48.             pictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb, writer);
  49.             PdfTemplate tp;
  50.             PdfTransparencyGroup group;
  51.  
  52.             tp = cb.createTemplate(200, 200);
  53.             pictureCircles(0, 0, tp);
  54.             group = new PdfTransparencyGroup();
  55.             group.setIsolated(true);
  56.             group.setKnockout(true);
  57.             tp.setGroup(group);
  58.             cb.addTemplate(tp, gap, 500);
  59.  
  60.             tp = cb.createTemplate(200, 200);
  61.             pictureCircles(0, 0, tp);
  62.             group = new PdfTransparencyGroup();
  63.             group.setIsolated(true);
  64.             group.setKnockout(false);
  65.             tp.setGroup(group);
  66.             cb.addTemplate(tp, 200 + 2 * gap, 500);
  67.  
  68.             tp = cb.createTemplate(200, 200);
  69.             pictureCircles(0, 0, tp);
  70.             group = new PdfTransparencyGroup();
  71.             group.setIsolated(false);
  72.             group.setKnockout(true);
  73.             tp.setGroup(group);
  74.             cb.addTemplate(tp, gap, 500 - 200 - gap);
  75.  
  76.             tp = cb.createTemplate(200, 200);
  77.             pictureCircles(0, 0, tp);
  78.             group = new PdfTransparencyGroup();
  79.             group.setIsolated(false);
  80.             group.setKnockout(false);
  81.             tp.setGroup(group);
  82.             cb.addTemplate(tp, 200 + 2 * gap, 500 - 200 - gap);
  83.         } catch (DocumentException de) {
  84.             System.err.println(de.getMessage());
  85.         } catch (IOException ioe) {
  86.             System.err.println(ioe.getMessage());
  87.         }
  88.  
  89.         // step 5: we close the document
  90.         document.close();
  91.     }
  92.  
  93.     /**
  94.      * Prints a square and fills half of it with a gray rectangle.
  95.      *
  96.      * @param x
  97.      * @param y
  98.      * @param cb
  99.      * @throws Exception
  100.      */
  101.     public static void pictureBackdrop(float x, float y, PdfContentByte cb,
  102.             PdfWriter writer) {
  103.         PdfShading axial = PdfShading.simpleAxial(writer, x, y, x + 200, y,
  104.                 BaseColor.YELLOW, BaseColor.RED);
  105.         PdfShadingPattern axialPattern = new PdfShadingPattern(axial);
  106.         cb.setShadingFill(axialPattern);
  107.         cb.setColorStroke(BaseColor.BLACK);
  108.         cb.setLineWidth(2);
  109.         cb.rectangle(x, y, 200, 200);
  110.         cb.fillStroke();
  111.     }
  112.  
  113.     /**
  114.      * Prints 3 circles in different colors that intersect with eachother.
  115.      *
  116.      * @param x
  117.      * @param y
  118.      * @param cb
  119.      * @throws Exception
  120.      */
  121.     public static void pictureCircles(float x, float y, PdfContentByte cb) {
  122.         PdfGState gs = new PdfGState();
  123.         gs.setBlendMode(PdfGState.BM_MULTIPLY);
  124.         gs.setFillOpacity(1f);
  125.         cb.setGState(gs);
  126.         cb.setColorFill(new CMYKColor(0f, 0f, 0f, 0.15f));
  127.         cb.circle(x + 75, y + 75, 70);
  128.         cb.fill();
  129.         cb.circle(x + 75, y + 125, 70);
  130.         cb.fill();
  131.         cb.circle(x + 125, y + 75, 70);
  132.         cb.fill();
  133.         cb.circle(x + 125, y + 125, 70);
  134.         cb.fill();
  135.     }
  136.  
  137. }
ImageTypes.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 part3.chapter10;
  9.  
  10. import java.awt.Toolkit;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Image;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.Barcode;
  19. import com.itextpdf.text.pdf.BarcodeEAN;
  20. import com.itextpdf.text.pdf.BarcodePDF417;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23. public class ImageTypes {
  24.  
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part3/chapter10/image_types.pdf";
  28.     /** Paths to images. */
  29.     public static final String[] RESOURCES = {
  30.         "bruno_ingeborg.jpg",
  31.         "map.jp2",
  32.         "info.png",
  33.         "close.bmp",
  34.         "movie.gif",
  35.         "butterfly.wmf",
  36.         "animated_fox_dog.gif",
  37.         "marbles.tif",
  38.         "amb.jb2"
  39.     };
  40.     /** Path to an image. */
  41.     public static final String RESOURCE = "resources/img/hitchcock.png";
  42.  
  43.     /**
  44.      * Creates a PDF document.
  45.      * @param filename the path to the new PDF document
  46.      * @throws    DocumentException
  47.      * @throws    IOException
  48.      */
  49.     public void createPdf(String filename) throws IOException, DocumentException {
  50.         // step 1
  51.         Document document = new Document();
  52.         // step 2
  53.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  54.         // step 3
  55.         document.open();
  56.         // step 4
  57.         // Adding a series of images
  58.         Image img;
  59.         for (int i = 0; i < RESOURCES.length; i++) {
  60.             img = Image.getInstance(String.format("resources/img/%s", RESOURCES[i]));
  61.             if (img.getScaledWidth() > 300 || img.getScaledHeight() > 300) {
  62.                 img.scaleToFit(300, 300);
  63.             }
  64.             document.add(new Paragraph(
  65.                     String.format("%s is an image of type %s", RESOURCES[i], img.getClass().getName())));
  66.             document.add(img);
  67.         }
  68.         // Adding a java.awt.Image
  69.         java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(RESOURCE);
  70.         img = com.itextpdf.text.Image.getInstance(awtImage, null);
  71.         document.add(new Paragraph(
  72.                 String.format("%s is an image of type %s", "java.awt.Image", img.getClass().getName())));
  73.         document.add(img);
  74.         // Adding a barcode
  75.         BarcodeEAN codeEAN = new BarcodeEAN();
  76.         codeEAN.setCodeType(Barcode.EAN13);
  77.         codeEAN.setCode("9781935182610");
  78.         img = codeEAN.createImageWithBarcode(writer.getDirectContent(), null, null);
  79.         document.add(new Paragraph(
  80.                 String.format("%s is an image of type %s", "barcode", img.getClass().getName())));
  81.         document.add(img);
  82.         // Adding a matrix code
  83.         BarcodePDF417 pdf417 = new BarcodePDF417();
  84.         String text = "iText in Action, a book by Bruno Lowagie.";
  85.         pdf417.setText(text);
  86.         img = pdf417.getImage();
  87.         document.add(new Paragraph(
  88.                 String.format("%s is an image of type %s", "barcode", img.getClass().getName())));
  89.         document.add(img);
  90.  
  91.         // step 5
  92.         document.close();
  93.     }
  94.  
  95.  
  96.     /**
  97.      * Main method.
  98.      *
  99.      * @param    args    no arguments needed
  100.      * @throws DocumentException
  101.      * @throws IOException
  102.      * @throws DocumentException
  103.      * @throws IOException
  104.      */
  105.     public static void main(String[] args) throws IOException, DocumentException {
  106.         new ImageTypes().createPdf(RESULT);
  107.     }
  108. }
RawImage.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class RawImage {
  19.  
  20.     /** The resulting PDF file. */
  21.     public static final String RESULT
  22.         = "results/part3/chapter10/gradient.pdf";
  23.    
  24.     /**
  25.      * Creates a PDF document.
  26.      * @param filename the path to the new PDF document
  27.      * @throws    DocumentException
  28.      * @throws    IOException
  29.      */
  30.     public void createPdf(String filename) throws IOException, DocumentException {
  31.         // step 1
  32.         Document document = new Document();
  33.         // step 2
  34.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  35.         // step 3
  36.         document.open();
  37.         // step 4
  38.         // Image in color space DeviceGray
  39.         byte gradient[] = new byte[256];
  40.         for (int i = 0; i < 256; i++)
  41.             gradient[i] = (byte) i;
  42.         Image img1 = Image.getInstance(256, 1, 1, 8, gradient);
  43.         img1.scaleAbsolute(256, 50);
  44.         document.add(img1);
  45.         // Image in color space RGB
  46.         byte cgradient[] = new byte[256 * 3];
  47.         for (int i = 0; i < 256; i++) {
  48.             cgradient[i * 3] = (byte) (255 - i);
  49.             cgradient[i * 3 + 1] = (byte) (255 - i);
  50.             cgradient[i * 3 + 2] = (byte) i;
  51.         }
  52.         Image img2 = Image.getInstance(256, 1, 3, 8, cgradient);
  53.         img2.scaleAbsolute(256, 50);
  54.         document.add(img2);
  55.         Image img3 = Image.getInstance(16, 16, 3, 8, cgradient);
  56.         img3.scaleAbsolute(64, 64);
  57.         document.add(img3);
  58.         // step 5
  59.         document.close();
  60.     }
  61.    
  62.     /**
  63.      * Main method.
  64.      *
  65.      * @param    args    no arguments needed
  66.      * @throws DocumentException
  67.      * @throws IOException
  68.      * @throws DocumentException
  69.      * @throws IOException
  70.      */
  71.     public static void main(String[] args) throws IOException, DocumentException {
  72.         new RawImage().createPdf(RESULT);
  73.     }
  74. }
Barcodes.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Element;
  17. import com.itextpdf.text.Image;
  18. import com.itextpdf.text.Paragraph;
  19. import com.itextpdf.text.Rectangle;
  20. import com.itextpdf.text.pdf.Barcode;
  21. import com.itextpdf.text.pdf.Barcode128;
  22. import com.itextpdf.text.pdf.Barcode39;
  23. import com.itextpdf.text.pdf.BarcodeCodabar;
  24. import com.itextpdf.text.pdf.BarcodeDatamatrix;
  25. import com.itextpdf.text.pdf.BarcodeEAN;
  26. import com.itextpdf.text.pdf.BarcodeEANSUPP;
  27. import com.itextpdf.text.pdf.BarcodeInter25;
  28. import com.itextpdf.text.pdf.BarcodePDF417;
  29. import com.itextpdf.text.pdf.BarcodePostnet;
  30. import com.itextpdf.text.pdf.BarcodeQRCode;
  31. import com.itextpdf.text.pdf.PdfContentByte;
  32. import com.itextpdf.text.pdf.PdfWriter;
  33.  
  34. public class Barcodes {
  35.  
  36.     /** The resulting PDF. */
  37.     private static final String RESULT = "results/part3/chapter10/barcodes.pdf";
  38.  
  39.     /**
  40.      * Generates a PDF file with different types of barcodes.
  41.      *
  42.      * @param args
  43.      *            no arguments needed here
  44.      * @throws DocumentException
  45.      * @throws IOException
  46.      */
  47.     public static void main(String[] args) throws IOException,
  48.             DocumentException {
  49.         new Barcodes().createPdf(RESULT);
  50.     }
  51.  
  52.     /**
  53.      * Creates a PDF document.
  54.      * @param filename the path to the new PDF document
  55.      * @throws    DocumentException
  56.      * @throws    IOException
  57.      */
  58.     public void createPdf(String filename) throws IOException, DocumentException {
  59.         // step 1
  60.         Document document = new Document(new Rectangle(340, 842));
  61.         // step 2
  62.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  63.         // step 3
  64.         document.open();
  65.         // step 4
  66.         PdfContentByte cb = writer.getDirectContent();
  67.  
  68.         // EAN 13
  69.         document.add(new Paragraph("Barcode EAN.UCC-13"));
  70.         BarcodeEAN codeEAN = new BarcodeEAN();
  71.         codeEAN.setCode("4512345678906");
  72.         document.add(new Paragraph("default:"));
  73.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  74.         codeEAN.setGuardBars(false);
  75.         document.add(new Paragraph("without guard bars:"));
  76.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  77.         codeEAN.setBaseline(-1f);
  78.         codeEAN.setGuardBars(true);
  79.         document.add(new Paragraph("text above:"));
  80.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  81.         codeEAN.setBaseline(codeEAN.getSize());
  82.  
  83.         // UPC A
  84.         document.add(new Paragraph("Barcode UCC-12 (UPC-A)"));
  85.         codeEAN.setCodeType(Barcode.UPCA);
  86.         codeEAN.setCode("785342304749");
  87.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  88.  
  89.         // EAN 8
  90.         document.add(new Paragraph("Barcode EAN.UCC-8"));
  91.         codeEAN.setCodeType(Barcode.EAN8);
  92.         codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
  93.         codeEAN.setCode("34569870");
  94.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  95.  
  96.         // UPC E
  97.         document.add(new Paragraph("Barcode UPC-E"));
  98.         codeEAN.setCodeType(Barcode.UPCE);
  99.         codeEAN.setCode("03456781");
  100.         document.add(codeEAN.createImageWithBarcode(cb, null, null));
  101.         codeEAN.setBarHeight(codeEAN.getSize() * 3f);
  102.  
  103.         // EANSUPP
  104.         document.add(new Paragraph("Bookland"));
  105.         document.add(new Paragraph("ISBN 0-321-30474-8"));
  106.         codeEAN.setCodeType(Barcode.EAN13);
  107.         codeEAN.setCode("9781935182610");
  108.         BarcodeEAN codeSUPP = new BarcodeEAN();
  109.         codeSUPP.setCodeType(Barcode.SUPP5);
  110.         codeSUPP.setCode("55999");
  111.         codeSUPP.setBaseline(-2);
  112.         BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
  113.         document.add(eanSupp.createImageWithBarcode(cb, null, BaseColor.BLUE));
  114.  
  115.         // CODE 128
  116.         document.add(new Paragraph("Barcode 128"));
  117.         Barcode128 code128 = new Barcode128();
  118.         code128.setCode("0123456789 hello");
  119.         document.add(code128.createImageWithBarcode(cb, null, null));
  120.         code128.setCode("0123456789\uffffMy Raw Barcode (0 - 9)");
  121.         code128.setCodeType(Barcode.CODE128_RAW);
  122.         document.add(code128.createImageWithBarcode(cb, null, null));
  123.  
  124.         // Data for the barcode :
  125.         String code402 = "24132399420058289";
  126.         String code90 = "3700000050";
  127.         String code421 = "422356";
  128.         StringBuffer data = new StringBuffer(code402);
  129.         data.append(Barcode128.FNC1);
  130.         data.append(code90);
  131.         data.append(Barcode128.FNC1);
  132.         data.append(code421);
  133.         Barcode128 shipBarCode = new Barcode128();
  134.         shipBarCode.setX(0.75f);
  135.         shipBarCode.setN(1.5f);
  136.         shipBarCode.setSize(10f);
  137.         shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
  138.         shipBarCode.setBaseline(10f);
  139.         shipBarCode.setBarHeight(50f);
  140.         shipBarCode.setCode(data.toString());
  141.         document.add(shipBarCode.createImageWithBarcode(cb, BaseColor.BLACK,
  142.                 BaseColor.BLUE));
  143.  
  144.         // it is composed of 3 blocks whith AI 01, 3101 and 10
  145.         Barcode128 uccEan128 = new Barcode128();
  146.         uccEan128.setCodeType(Barcode.CODE128_UCC);
  147.         uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
  148.         document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
  149.                 BaseColor.BLACK));
  150.         uccEan128.setCode("0191234567890121310100035510ABC123");
  151.         document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
  152.                 BaseColor.RED));
  153.         uccEan128.setCode("(01)28880123456788");
  154.         document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
  155.                 BaseColor.BLACK));
  156.  
  157.         // INTER25
  158.         document.add(new Paragraph("Barcode Interleaved 2 of 5"));
  159.         BarcodeInter25 code25 = new BarcodeInter25();
  160.         code25.setGenerateChecksum(true);
  161.         code25.setCode("41-1200076041-001");
  162.         document.add(code25.createImageWithBarcode(cb, null, null));
  163.         code25.setCode("411200076041001");
  164.         document.add(code25.createImageWithBarcode(cb, null, null));
  165.         code25.setCode("0611012345678");
  166.         code25.setChecksumText(true);
  167.         document.add(code25.createImageWithBarcode(cb, null, null));
  168.  
  169.         // POSTNET
  170.         document.add(new Paragraph("Barcode Postnet"));
  171.         BarcodePostnet codePost = new BarcodePostnet();
  172.         document.add(new Paragraph("ZIP"));
  173.         codePost.setCode("01234");
  174.         document.add(codePost.createImageWithBarcode(cb, null, null));
  175.         document.add(new Paragraph("ZIP+4"));
  176.         codePost.setCode("012345678");
  177.         document.add(codePost.createImageWithBarcode(cb, null, null));
  178.         document.add(new Paragraph("ZIP+4 and dp"));
  179.         codePost.setCode("01234567890");
  180.         document.add(codePost.createImageWithBarcode(cb, null, null));
  181.  
  182.         document.add(new Paragraph("Barcode Planet"));
  183.         BarcodePostnet codePlanet = new BarcodePostnet();
  184.         codePlanet.setCode("01234567890");
  185.         codePlanet.setCodeType(Barcode.PLANET);
  186.         document.add(codePlanet.createImageWithBarcode(cb, null, null));
  187.  
  188.         // CODE 39
  189.         document.add(new Paragraph("Barcode 3 of 9"));
  190.         Barcode39 code39 = new Barcode39();
  191.         code39.setCode("ITEXT IN ACTION");
  192.         document.add(code39.createImageWithBarcode(cb, null, null));
  193.  
  194.         document.add(new Paragraph("Barcode 3 of 9 extended"));
  195.         Barcode39 code39ext = new Barcode39();
  196.         code39ext.setCode("iText in Action");
  197.         code39ext.setStartStopText(false);
  198.         code39ext.setExtended(true);
  199.         document.add(code39ext.createImageWithBarcode(cb, null, null));
  200.  
  201.         // CODABAR
  202.         document.add(new Paragraph("Codabar"));
  203.         BarcodeCodabar codabar = new BarcodeCodabar();
  204.         codabar.setCode("A123A");
  205.         codabar.setStartStopText(true);
  206.         document.add(codabar.createImageWithBarcode(cb, null, null));
  207.  
  208.         // PDF417
  209.         document.add(new Paragraph("Barcode PDF417"));
  210.         BarcodePDF417 pdf417 = new BarcodePDF417();
  211.         String text = "Call me Ishmael. Some years ago--never mind how long "
  212.             + "precisely --having little or no money in my purse, and nothing "
  213.             + "particular to interest me on shore, I thought I would sail about "
  214.             + "a little and see the watery part of the world.";
  215.         pdf417.setText(text);
  216.         Image img = pdf417.getImage();
  217.         img.scalePercent(50, 50 * pdf417.getYHeight());
  218.         document.add(img);
  219.  
  220.         document.add(new Paragraph("Barcode Datamatrix"));
  221.         BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
  222.         datamatrix.generate(text);
  223.         img = datamatrix.createImage();
  224.         document.add(img);
  225.  
  226.         document.add(new Paragraph("Barcode QRCode"));
  227.         BarcodeQRCode qrcode = new BarcodeQRCode("Moby Dick by Herman Melville", 1, 1, null);
  228.         img = qrcode.getImage();
  229.         document.add(img);
  230.        
  231.         // step 5
  232.         document.close();
  233.     }
  234. }
CompressImage.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class CompressImage {
  19.  
  20.     /** The resulting PDF file. */
  21.     public static final String RESULT1
  22.         = "results/part3/chapter10/uncompressed.pdf";
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT2
  25.         = "results/part3/chapter10/compressed.pdf";
  26.     /** One of the resources. */
  27.     public static final String RESOURCE
  28.         = "resources/img/butterfly.bmp";
  29.  
  30.     /**
  31.      * Creates a PDF document.
  32.      * @param filename the path to the new PDF document
  33.      * @throws    DocumentException
  34.      * @throws    IOException
  35.      */
  36.     public void createPdf(String filename, boolean compress) throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document();
  39.         // step 2
  40.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         Image img = Image.getInstance(RESOURCE);
  45.         if (compress)
  46.             img.setCompressionLevel(9);
  47.         document.add(img);
  48.         // step 5
  49.         document.close();
  50.     }
  51.  
  52.     /**
  53.      * Main method.
  54.      *
  55.      * @param    args    no arguments needed
  56.      * @throws DocumentException
  57.      * @throws IOException
  58.      * @throws DocumentException
  59.      * @throws IOException
  60.      */
  61.     public static void main(String[] args) throws IOException, DocumentException {
  62.         new CompressImage().createPdf(RESULT1, false);
  63.         new CompressImage().createPdf(RESULT2, true);
  64.     }
  65. }
CompressAwt.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 part3.chapter10;
  9.  
  10. import java.awt.Toolkit;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Image;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class CompressAwt {
  21.  
  22.     /** The resulting PDF file. */
  23.     public static final String RESULT1
  24.         = "results/part3/chapter10/hitchcock100.pdf";
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT2
  27.         = "results/part3/chapter10/hitchcock20.pdf";
  28.     /** The resulting PDF file. */
  29.     public static final String RESULT3
  30.         = "results/part3/chapter10/hitchcock10.pdf";
  31.     /** One of the resources. */
  32.     public static final String RESOURCE
  33.         = "resources/img/hitchcock.png";
  34.  
  35.     /**
  36.      * Creates a PDF document.
  37.      * @param filename the path to the new PDF document
  38.      * @throws    DocumentException
  39.      * @throws    IOException
  40.      */
  41.     public void createPdf(String filename, float quality) throws IOException, DocumentException {
  42.         // step 1
  43.         Document document = new Document(new Rectangle(200, 280));
  44.         // step 2
  45.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(RESOURCE);
  50.         Image img = Image.getInstance(writer, awtImage, quality);
  51.         img.setAbsolutePosition(15, 15);
  52.         document.add(img);
  53.         // step 5
  54.         document.close();
  55.     }
  56.    
  57.     /**
  58.      * Main method.
  59.      *
  60.      * @param    args    no arguments needed
  61.      * @throws DocumentException
  62.      * @throws IOException
  63.      * @throws DocumentException
  64.      * @throws IOException
  65.      */
  66.     public static void main(String[] args) throws IOException, DocumentException {
  67.         new CompressAwt().createPdf(RESULT1, 1);
  68.         new CompressAwt().createPdf(RESULT2, 0.2f);
  69.         new CompressAwt().createPdf(RESULT3, 0.1f);
  70.     }
  71. }
PagedImages.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import com.itextpdf.text.pdf.RandomAccessFileOrArray;
  18. import com.itextpdf.text.pdf.codec.GifImage;
  19. import com.itextpdf.text.pdf.codec.JBIG2Image;
  20. import com.itextpdf.text.pdf.codec.TiffImage;
  21.  
  22. public class PagedImages {
  23.    
  24.     /** The resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part3/chapter10/tiff_jbig2_gif.pdf";
  27.     /** One of the resources. */
  28.     public static final String RESOURCE1
  29.         = "resources/img/marbles.tif";
  30.     /** One of the resources. */
  31.     public static final String RESOURCE2
  32.         = "resources/img/amb.jb2";
  33.     /** One of the resources. */
  34.     public static final String RESOURCE3
  35.         = "resources/img/animated_fox_dog.gif";
  36.  
  37.     /**
  38.      * Creates a PDF document.
  39.      * @param filename the path to the new PDF document
  40.      * @throws    DocumentException
  41.      * @throws    IOException
  42.      */
  43.     public void createPdf(String filename) throws IOException, DocumentException {
  44.         // step 1
  45.         Document document = new Document();
  46.         // step 2
  47.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         addTif(document, RESOURCE1);
  52.         document.newPage();
  53.         addJBIG2(document, RESOURCE2);
  54.         document.newPage();
  55.         addGif(document, RESOURCE3);
  56.         // step 5
  57.         document.close();
  58.     }
  59.    
  60.     /**
  61.      * Adds the different pages inside a single TIFF file to a document.
  62.      * @param document the document to which the pages have to be added
  63.      * @param path     the path to the TIFF file
  64.      * @throws DocumentException
  65.      * @throws IOException
  66.      */
  67.     public static void addTif(Document document, String path) throws DocumentException, IOException {
  68.         RandomAccessFileOrArray ra = new RandomAccessFileOrArray(path);
  69.         int n = TiffImage.getNumberOfPages(ra);
  70.         Image img;
  71.         for (int i = 1; i
TransparentImage.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class TransparentImage {
  19.  
  20.     /** The resulting PDF file. */
  21.     public static final String RESULT
  22.         = "results/part3/chapter10/transparent_image.pdf";
  23.     /** One of the resources. */
  24.     public static final String RESOURCE1
  25.         = "resources/img/bruno.jpg";
  26.     /** One of the resources. */
  27.     public static final String RESOURCE2
  28.         = "resources/img/info.png";
  29.     /** One of the resources. */
  30.     public static final String RESOURCE3
  31.         = "resources/img/1t3xt.gif";
  32.     /** One of the resources. */
  33.     public static final String RESOURCE4
  34.         = "resources/img/logo.gif";
  35.  
  36.  
  37.     /**
  38.      * Creates a PDF document.
  39.      * @param filename the path to the new PDF document
  40.      * @throws    DocumentException
  41.      * @throws    IOException
  42.      */
  43.     public void createPdf(String filename) throws IOException, DocumentException {
  44.         Image img1 = Image.getInstance(RESOURCE1);
  45.         // step 1
  46.         Document document = new Document(img1);
  47.         // step 2
  48.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  49.         // step 3
  50.         document.open();
  51.         // step 4
  52.         img1.setAbsolutePosition(0, 0);
  53.         document.add(img1);
  54.         Image img2 = Image.getInstance(RESOURCE2);
  55.         img2.setAbsolutePosition(0, 260);
  56.         document.add(img2);
  57.         Image img3 = Image.getInstance(RESOURCE3);
  58.         img3.setTransparency(new int[]{ 0x00, 0x10 });
  59.         img3.setAbsolutePosition(0, 0);
  60.         document.add(img3);
  61.         Image img4 = Image.getInstance(RESOURCE4);
  62.         img4.setTransparency(new int[]{ 0xF0, 0xFF });
  63.         img4.setAbsolutePosition(50, 50);
  64.         document.add(img4);
  65.  
  66.         // step 5
  67.         document.close();
  68.     }
  69.    
  70.     /**
  71.      * Main method.
  72.      *
  73.      * @param    args    no arguments needed
  74.      * @throws DocumentException
  75.      * @throws IOException
  76.      * @throws DocumentException
  77.      * @throws IOException
  78.      */
  79.     public static void main(String[] args) throws IOException, DocumentException {
  80.         new TransparentImage().createPdf(RESULT);
  81.     }
  82. }
TransparentAwt.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 part3.chapter10;
  9.  
  10. import java.awt.Color;
  11. import java.awt.Toolkit;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.Image;
  18. import com.itextpdf.text.PageSize;
  19. import com.itextpdf.text.Paragraph;
  20. import com.itextpdf.text.Rectangle;
  21. import com.itextpdf.text.pdf.GrayColor;
  22. import com.itextpdf.text.pdf.PdfWriter;
  23.  
  24. public class TransparentAwt {
  25.  
  26.     /** The resulting PDF file. */
  27.     public static final String RESULT
  28.         = "results/part3/chapter10/transparent_hitchcock.pdf";
  29.     /** One of the resources. */
  30.     public static final String RESOURCE
  31.         = "resources/img/hitchcock.gif";
  32.  
  33.     /**
  34.      * Creates a PDF document.
  35.      * @param filename the path to the new PDF document
  36.      * @throws    DocumentException
  37.      * @throws    IOException
  38.      */
  39.     public void createPdf(String filename) throws IOException, DocumentException {
  40.         // step 1
  41.         Rectangle r = new Rectangle(PageSize.A4);
  42.         r.setBackgroundColor(new GrayColor(0.8f));
  43.         Document document = new Document(r);
  44.         // step 2
  45.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(RESOURCE);
  50.         document.add(new Paragraph("Hitchcock in Red."));
  51.         Image img1 = Image.getInstance(awtImage, null);
  52.         document.add(img1);
  53.         document.add(new Paragraph("Hitchcock in Black and White."));
  54.         Image img2 = Image.getInstance(awtImage, null, true);
  55.         document.add(img2);
  56.         document.newPage();
  57.         document.add(new Paragraph("Hitchcock in Red and Yellow."));
  58.         Image img3 = Image.getInstance(awtImage, new Color(0xFF, 0xFF, 0x00));
  59.         document.add(img3);
  60.         document.add(new Paragraph("Hitchcock in Black and White."));
  61.         Image img4 = Image.getInstance(awtImage, new Color(0xFF, 0xFF, 0x00), true);
  62.         document.add(img4);
  63.         // step 5
  64.         document.close();
  65.     }
  66.    
  67.     /**
  68.      * Main method.
  69.      *
  70.      * @param    args    no arguments needed
  71.      * @throws DocumentException
  72.      * @throws IOException
  73.      * @throws DocumentException
  74.      * @throws IOException
  75.      */
  76.     public static void main(String[] args) throws IOException, DocumentException {
  77.         new TransparentAwt().createPdf(RESULT);
  78.     }
  79. }
ImageMask.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. public class ImageMask {
  19.  
  20.     /** The resulting PDF file. */
  21.     public static final String RESULT1
  22.         = "results/part3/chapter10/hardmask.pdf";
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT2
  25.         = "results/part3/chapter10/softmask.pdf";
  26.     /** One of the resources. */
  27.     public static final String RESOURCE
  28.         = "resources/img/bruno.jpg";
  29.  
  30.     /**
  31.      * Creates a PDF document.
  32.      * @param filename the path to the new PDF document
  33.      * @throws    DocumentException
  34.      * @throws    IOException
  35.      */
  36.     public void createPdf(String filename, Image mask) throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document();
  39.         // step 2
  40.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         Image img = Image.getInstance(RESOURCE);
  45.         img.setImageMask(mask);
  46.         document.add(img);
  47.         // step 5
  48.         document.close();
  49.     }
  50.    
  51.     /**
  52.      * Main method.
  53.      *
  54.      * @param    args    no arguments needed
  55.      * @throws DocumentException
  56.      * @throws IOException
  57.      * @throws DocumentException
  58.      * @throws IOException
  59.      */
  60.     public static void main(String[] args) throws IOException, DocumentException {
  61.         byte circledata[] = { (byte) 0x3c, (byte) 0x7e, (byte) 0xff,
  62.                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7e,
  63.                 (byte) 0x3c };
  64.         Image mask = Image.getInstance(8, 8, 1, 1, circledata);
  65.         mask.makeMask();
  66.         mask.setInverted(true);
  67.         new ImageMask().createPdf(RESULT1, mask);
  68.         byte gradient[] = new byte[256];
  69.         for (int i = 0; i < 256; i++)
  70.             gradient[i] = (byte) i;
  71.         mask = Image.getInstance(256, 1, 1, 8, gradient);
  72.         mask.makeMask();
  73.         new ImageMask().createPdf(RESULT2, mask);
  74.     }
  75. }
TemplateClip.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfTemplate;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class TemplateClip {
  20.  
  21.     /** The resulting PDF file. */
  22.     public static final String RESULT
  23.         = "results/part3/chapter10/template_clip.pdf";
  24.     /** One of the resources. */
  25.     public static final String RESOURCE
  26.         = "resources/img/bruno_ingeborg.jpg";
  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();
  37.         // step 2
  38.         PdfWriter writer
  39.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         Image img = Image.getInstance(RESOURCE);
  44.         float w = img.getScaledWidth();
  45.         float h = img.getScaledHeight();
  46.         PdfTemplate t = writer.getDirectContent().createTemplate(850, 600);
  47.         t.addImage(img, w, 0, 0, h, 0, -600);
  48.         Image clipped = Image.getInstance(t);
  49.         clipped.scalePercent(50);
  50.         document.add(clipped);
  51.         // step 5
  52.         document.close();
  53.     }
  54.    
  55.     /**
  56.      * Main method.
  57.      *
  58.      * @param    args    no arguments needed
  59.      * @throws DocumentException
  60.      * @throws IOException
  61.      * @throws DocumentException
  62.      * @throws IOException
  63.      */
  64.     public static void main(String[] args) throws IOException, DocumentException {
  65.         new TemplateClip().createPdf(RESULT);
  66.     }
  67. }
ClippingPath.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.pdf.PdfTemplate;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class ClippingPath {
  20.  
  21.     /** The resulting PDF file. */
  22.     public static final String RESULT
  23.         = "results/part3/chapter10/clipping_path.pdf";
  24.     /** One of the resources. */
  25.     public static final String RESOURCE
  26.         = "resources/img/bruno_ingeborg.jpg";
  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();
  37.         // step 2
  38.         PdfWriter writer
  39.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  40.         // step 3
  41.         document.open();
  42.         // step 4
  43.         Image img = Image.getInstance(RESOURCE);
  44.         float w = img.getScaledWidth();
  45.         float h = img.getScaledHeight();
  46.         PdfTemplate t = writer.getDirectContent().createTemplate(850, 600);
  47.         t.ellipse(0, 0, 850, 600);
  48.         t.clip();
  49.         t.newPath();
  50.         t.addImage(img, w, 0, 0, h, 0, -600);
  51.         Image clipped = Image.getInstance(t);
  52.         clipped.scalePercent(50);
  53.         document.add(clipped);
  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.      * @throws DocumentException
  65.      * @throws IOException
  66.      */
  67.     public static void main(String[] args) throws IOException, DocumentException {
  68.         new ClippingPath().createPdf(RESULT);
  69.     }
  70. }
TransparentOverlay.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 part3.chapter10;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.Rectangle;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfDictionary;
  19. import com.itextpdf.text.pdf.PdfGState;
  20. import com.itextpdf.text.pdf.PdfName;
  21. import com.itextpdf.text.pdf.PdfTemplate;
  22. import com.itextpdf.text.pdf.PdfTransparencyGroup;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. public class TransparentOverlay {
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String RESULT
  29.         = "results/part3/chapter10/transparent_overlay.pdf";
  30.     /** One of the resources. */
  31.     public static final String RESOURCE
  32.         = "resources/img/bruno_ingeborg.jpg";
  33.    
  34.     /**
  35.      * Creates a PDF document.
  36.      * @param filename the path to the new PDF document
  37.      * @throws    DocumentException
  38.      * @throws    IOException
  39.      */
  40.     public void createPdf(String filename) throws IOException, DocumentException {
  41.         // step 1
  42.         Document document = new Document(new Rectangle(850, 600));
  43.         // step 2
  44.         PdfWriter writer
  45.           = PdfWriter.getInstance(document, new FileOutputStream(filename));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         PdfContentByte canvas = writer.getDirectContent();
  50.        
  51.         // add the clipped image
  52.         Image img = Image.getInstance(RESOURCE);
  53.         float w = img.getScaledWidth();
  54.         float h = img.getScaledHeight();
  55.         canvas.ellipse(1, 1, 848, 598);
  56.         canvas.clip();
  57.         canvas.newPath();
  58.         canvas.addImage(img, w, 0, 0, h, 0, -600);
  59.  
  60.         // Create a transparent PdfTemplate
  61.         PdfTemplate t2 = writer.getDirectContent().createTemplate(850, 600);
  62.         PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
  63.         transGroup.put( PdfName.CS, PdfName.DEVICEGRAY);
  64.         transGroup.setIsolated(true);
  65.         transGroup.setKnockout(false);
  66.         t2.setGroup(transGroup);
  67.  
  68.         // Add transparent ellipses to the template
  69.         int gradationStep = 30;
  70.         float[] gradationRatioList = new float[gradationStep];
  71.         for(int i = 0; i < gradationStep; i++) {
  72.             gradationRatioList[i] = 1 - (float)Math.sin(Math.toRadians(90.0f / gradationStep * (i + 1)));
  73.         }
  74.         for(int i = 1; i < gradationStep + 1; i++) {
  75.             t2.setLineWidth(5 * (gradationStep + 1 - i));
  76.             t2.setGrayStroke(gradationRatioList[gradationStep - i]);
  77.             t2.ellipse(0, 0, 850, 600);
  78.             t2.stroke();
  79.         }
  80.        
  81.         // Create an image mask for the direct content
  82.         PdfDictionary maskDict = new PdfDictionary();
  83.         maskDict.put(PdfName.TYPE, PdfName.MASK );
  84.         maskDict.put(PdfName.S, new PdfName("Luminosity"));
  85.         maskDict.put(new PdfName("G"), t2.getIndirectReference());
  86.         PdfGState gState = new PdfGState();
  87.         gState.put(PdfName.SMASK, maskDict );
  88.         canvas.setGState(gState);
  89.        
  90.         canvas.addTemplate(t2, 0, 0);
  91.  
  92.         // step 5
  93.         document.close();
  94.     }
  95.    
  96.     /**
  97.      * Main method.
  98.      *
  99.      * @param    args    no arguments needed
  100.      * @throws DocumentException
  101.      * @throws IOException
  102.      */
  103.     public static void main(String[] args) throws IOException, DocumentException {
  104.         new TransparentOverlay().createPdf(RESULT);
  105.     }
  106. }
DeviceColor.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class DeviceColor : IWriter {
  18. // ===========================================================================
  19.     public virtual void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte canvas = writer.DirectContent;
  28.         // RGB Colors
  29.         ColorRectangle(canvas, new BaseColor(0x00, 0x00, 0x00),
  30.           36, 770, 36, 36
  31.         );
  32.         ColorRectangle(canvas, new BaseColor(0x00, 0x00, 0xFF),
  33.           90, 770, 36, 36
  34.         );
  35.         ColorRectangle(canvas, new BaseColor(0x00, 0xFF, 0x00),
  36.           144, 770, 36, 36
  37.         );
  38.         ColorRectangle(canvas, new BaseColor(0xFF, 0x00, 0x00),
  39.           198, 770, 36, 36
  40.         );
  41.         ColorRectangle(canvas, new BaseColor(0f, 1f, 1f), 252, 770, 36, 36);
  42.         ColorRectangle(canvas, new BaseColor(1f, 0f, 1f), 306, 770, 36, 36);
  43.         ColorRectangle(canvas, new BaseColor(1f, 1f, 0f), 360, 770, 36, 36);
  44.         ColorRectangle(canvas, BaseColor.BLACK, 416, 770, 36, 36);
  45.         ColorRectangle(canvas, BaseColor.LIGHT_GRAY, 470, 770, 36, 36);
  46.         // CMYK Colors
  47.         ColorRectangle(canvas, new CMYKColor(0x00, 0x00, 0x00, 0x00),
  48.           36, 716, 36, 36
  49.         );
  50.         ColorRectangle(canvas, new CMYKColor(0x00, 0x00, 0xFF, 0x00),
  51.           90, 716, 36, 36
  52.         );
  53.         ColorRectangle(canvas, new CMYKColor(0x00, 0x00, 0xFF, 0xFF),
  54.           144, 716, 36, 36
  55.         );
  56.         ColorRectangle(canvas, new CMYKColor(0x00, 0xFF, 0x00, 0x00),
  57.           198, 716, 36, 36
  58.         );
  59.         ColorRectangle(canvas, new CMYKColor(0f, 1f, 0f, 0.5f),
  60.           252, 716, 36, 36
  61.         );
  62.         ColorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 0f),
  63.           306, 716, 36, 36
  64.         );
  65.         ColorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 0.5f),
  66.           360, 716, 36, 36
  67.         );
  68.         ColorRectangle(canvas, new CMYKColor(1f, 0f, 0f, 1f),
  69.           416, 716, 36, 36
  70.         );
  71.         ColorRectangle(canvas, new CMYKColor(0f, 0f, 0f, 1f),
  72.           470, 716, 36, 36
  73.         );
  74.         // Gray color
  75.         ColorRectangle(canvas, new GrayColor(0x20), 36, 662, 36, 36);
  76.         ColorRectangle(canvas, new GrayColor(0x40), 90, 662, 36, 36);
  77.         ColorRectangle(canvas, new GrayColor(0x60), 144, 662, 36, 36);
  78.         ColorRectangle(canvas, new GrayColor(0.5f), 198, 662, 36, 36);
  79.         ColorRectangle(canvas, new GrayColor(0.625f), 252, 662, 36, 36);
  80.         ColorRectangle(canvas, new GrayColor(0.75f), 306, 662, 36, 36);
  81.         ColorRectangle(canvas, new GrayColor(0.825f), 360, 662, 36, 36);
  82.         ColorRectangle(canvas, GrayColor.GRAYBLACK, 416, 662, 36, 36);
  83.         ColorRectangle(canvas, GrayColor.GRAYWHITE, 470, 662, 36, 36);
  84.         // Alternative ways to color the rectangle
  85.         canvas.SetRGBColorFill(0x00, 0x80, 0x80);
  86.         canvas.Rectangle(36, 608, 36, 36);
  87.         canvas.FillStroke();
  88.         canvas.SetRGBColorFillF(0.5f, 0.25f, 0.60f);
  89.         canvas.Rectangle(90, 608, 36, 36);
  90.         canvas.FillStroke();
  91.         canvas.SetGrayFill(0.5f);
  92.         canvas.Rectangle(144, 608, 36, 36);
  93.         canvas.FillStroke();
  94.         canvas.SetCMYKColorFill(0xFF, 0xFF, 0x00, 0x80);
  95.         canvas.Rectangle(198, 608, 36, 36);
  96.         canvas.FillStroke();
  97.         canvas.SetCMYKColorFillF(0f, 1f, 1f, 0.5f);
  98.         canvas.Rectangle(252, 608, 36, 36);
  99.         canvas.FillStroke();
  100.       }
  101.     }
  102. // ---------------------------------------------------------------------------    
  103.     /**
  104.      * Draws a colored rectangle.
  105.      * @param canvas the canvas to draw on
  106.      * @param color the Color
  107.      * @param x the X coordinate
  108.      * @param y the Y coordinate
  109.      * @param width the width of the rectangle
  110.      * @param height the height of the rectangle
  111.      */
  112.     public void ColorRectangle(PdfContentByte canvas, BaseColor color,
  113.         float x, float y, float width, float height)
  114.     {
  115.       canvas.SaveState();
  116.       canvas.SetColorFill(color);
  117.       canvas.Rectangle(x, y, width, height);
  118.       canvas.FillStroke();
  119.       canvas.RestoreState();
  120.     }
  121. // ===========================================================================
  122.   }
  123. }
SeparationColor.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class SeparationColor : DeviceColor {
  18. // ===========================================================================
  19.     public override void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte canvas = writer.DirectContent;
  28.         PdfSpotColor psc_g = new PdfSpotColor(
  29.           "iTextSpotColorGray", new GrayColor(0.9f)
  30.         );
  31.         PdfSpotColor psc_rgb = new PdfSpotColor(
  32.           "iTextSpotColorRGB", new BaseColor(0x64, 0x95, 0xed)
  33.         );
  34.         PdfSpotColor psc_cmyk = new PdfSpotColor(
  35.           "iTextSpotColorCMYK", new CMYKColor(0.3f, .9f, .3f, .1f)
  36.         );
  37.         ColorRectangle(canvas, new SpotColor(psc_g, 0.5f),
  38.           36, 770, 36, 36
  39.         );
  40.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.1f),
  41.           90, 770, 36, 36
  42.         );
  43.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.2f),
  44.           144, 770, 36, 36
  45.         );
  46.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.3f),
  47.           198, 770, 36, 36
  48.         );
  49.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.4f),
  50.           252, 770, 36, 36
  51.         );
  52.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.5f),
  53.           306, 770, 36, 36
  54.         );
  55.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.6f),
  56.           360, 770, 36, 36
  57.         );
  58.         ColorRectangle(canvas, new SpotColor(psc_rgb, 0.7f),
  59.           416, 770, 36, 36
  60.         );
  61.         ColorRectangle(canvas, new SpotColor(psc_cmyk, 0.25f),
  62.           470, 770, 36, 36
  63.         );
  64.         canvas.SetColorFill(psc_g, 0.5f);
  65.         canvas.Rectangle(36, 716, 36, 36);
  66.         canvas.FillStroke();
  67.         canvas.SetColorFill(psc_g, 0.9f);
  68.         canvas.Rectangle(90, 716, 36, 36);
  69.         canvas.FillStroke();
  70.         canvas.SetColorFill(psc_rgb, 0.5f);
  71.         canvas.Rectangle(144, 716, 36, 36);
  72.         canvas.FillStroke();
  73.         canvas.SetColorFill(psc_rgb, 0.9f);
  74.         canvas.Rectangle(198, 716, 36, 36);
  75.         canvas.FillStroke();
  76.         canvas.SetColorFill(psc_cmyk, 0.5f);
  77.         canvas.Rectangle(252, 716, 36, 36);
  78.         canvas.FillStroke();
  79.         canvas.SetColorFill(psc_cmyk, 0.9f);
  80.         canvas.Rectangle(306, 716, 36, 36);
  81.         canvas.FillStroke();
  82.       }
  83.     }
  84. // ===========================================================================
  85.   }
  86. }
TilingPatternColor.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class TilingPatternColor : DeviceColor {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const string RESULT = "tiling_pattern.pdf";
  21.     /** An image that will be used for a pattern color. */
  22.     public static string RESOURCE = Path.Combine(Utility.ResourceImage, "info.png");
  23. // ---------------------------------------------------------------------------
  24.     public override void Write(Stream stream) {
  25.       using (ZipFile zip = new ZipFile()) {
  26.         TilingPatternColor t = new TilingPatternColor();
  27.         zip.AddEntry(RESULT, t.CreatePdf());
  28.         zip.AddFile(RESOURCE, "");
  29.         zip.Save(stream);            
  30.       }
  31.     }
  32. // ---------------------------------------------------------------------------
  33.     /**
  34.      * Creates a PDF document.
  35.      */
  36.     public byte[] CreatePdf()  {
  37.       using (MemoryStream ms = new MemoryStream()) {
  38.         // step 1
  39.         using (Document document = new Document()) {
  40.           // step 2
  41.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  42.           // step 3
  43.           document.Open();
  44.           // step 4
  45.           PdfContentByte canvas = writer.DirectContent;
  46.           PdfPatternPainter square
  47.               = canvas.CreatePattern(15, 15);
  48.           square.SetColorFill(new BaseColor(0xFF, 0xFF, 0x00));
  49.           square.SetColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  50.           square.Rectangle(5, 5, 5, 5);
  51.           square.FillStroke();
  52.          
  53.           PdfPatternPainter ellipse
  54.               = canvas.CreatePattern(15, 10, 20, 25);
  55.           ellipse.SetColorFill(new BaseColor(0xFF, 0xFF, 0x00));
  56.           ellipse.SetColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  57.           ellipse.Ellipse(2f, 2f, 13f, 8f);
  58.           ellipse.FillStroke();
  59.          
  60.           PdfPatternPainter circle
  61.               = canvas.CreatePattern(15, 15, 10, 20, BaseColor.BLUE);
  62.           circle.Circle(7.5f, 7.5f, 2.5f);
  63.           circle.Fill();
  64.          
  65.           PdfPatternPainter line
  66.               = canvas.CreatePattern(5, 10, null);
  67.           line.SetLineWidth(1);
  68.           line.MoveTo(3, -1);
  69.           line.LineTo(3, 11);
  70.           line.Stroke();
  71.          
  72.           Image img = Image.GetInstance(RESOURCE);
  73.           img.ScaleAbsolute(20, 20);
  74.           img.SetAbsolutePosition(0, 0);
  75.           PdfPatternPainter img_pattern
  76.               = canvas.CreatePattern(20, 20, 20, 20);
  77.           img_pattern.AddImage(img);
  78.           img_pattern.SetPatternMatrix(-0.5f, 0f, 0f, 0.5f, 0f, 0f);
  79.          
  80.           ColorRectangle(canvas, new PatternColor(square), 36, 696, 126, 126);
  81.           ColorRectangle(canvas, new PatternColor(ellipse), 180, 696, 126, 126);
  82.           ColorRectangle(canvas, new PatternColor(circle), 324, 696, 126, 126);
  83.           ColorRectangle(canvas, new PatternColor(line), 36, 552, 126, 126);
  84.           ColorRectangle(canvas, new PatternColor(img_pattern), 36, 408, 126, 126);
  85.  
  86.           canvas.SetPatternFill(line, BaseColor.RED);
  87.           canvas.Ellipse(180, 552, 306, 678);
  88.           canvas.FillStroke();
  89.           canvas.SetPatternFill(circle, BaseColor.GREEN);
  90.           canvas.Ellipse(324, 552, 450, 678);
  91.           canvas.FillStroke();
  92.          
  93.           canvas.SetPatternFill(img_pattern);
  94.           canvas.Ellipse(180, 408, 450, 534);
  95.           canvas.FillStroke();
  96.         }
  97.         return ms.ToArray();
  98.       }
  99.     }
  100. // ===========================================================================
  101.   }
  102. }
ShadingPatternColor.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class ShadingPatternColor : DeviceColor {
  18. // ===========================================================================
  19.     public override void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte canvas = writer.DirectContent;
  28.         PdfShading axial = PdfShading.SimpleAxial(writer, 36, 716, 396,
  29.           788, BaseColor.ORANGE, BaseColor.BLUE
  30.         );
  31.         canvas.PaintShading(axial);
  32.         document.NewPage();
  33.         PdfShading radial = PdfShading.SimpleRadial(writer,
  34.           200, 700, 50, 300, 700, 100,
  35.           new BaseColor(0xFF, 0xF7, 0x94),
  36.           new BaseColor(0xF7, 0x8A, 0x6B),
  37.           false, false
  38.         );
  39.         canvas.PaintShading(radial);
  40.  
  41.         PdfShadingPattern shading = new PdfShadingPattern(axial);
  42.         ColorRectangle(canvas, new ShadingColor(shading), 150, 420, 126, 126);
  43.         canvas.SetShadingFill(shading);
  44.         canvas.Rectangle(300, 420, 126, 126);
  45.         canvas.FillStroke();
  46.       }
  47.     }
  48. // ===========================================================================
  49.   }
  50. }
Transparency1.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class Transparency1 : IWriter {
  18. // ===========================================================================
  19.     public void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte cb = writer.DirectContent;
  28.         float gap = (document.PageSize.Width - 400) / 3;
  29.  
  30.         PictureBackdrop(gap, 500, cb);
  31.         PictureBackdrop(200 + 2 * gap, 500, cb);
  32.         PictureBackdrop(gap, 500 - 200 - gap, cb);
  33.         PictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb);
  34.  
  35.         PictureCircles(gap, 500, cb);
  36.         cb.SaveState();
  37.         PdfGState gs1 = new PdfGState();
  38.         gs1.FillOpacity = 0.5f;
  39.         cb.SetGState(gs1);
  40.         PictureCircles(200 + 2 * gap, 500, cb);
  41.         cb.RestoreState();
  42.  
  43.         cb.SaveState();
  44.         PdfTemplate tp = cb.CreateTemplate(200, 200);
  45.         PdfTransparencyGroup group = new PdfTransparencyGroup();
  46.         tp.Group = group;
  47.         PictureCircles(0, 0, tp);
  48.         cb.SetGState(gs1);
  49.         cb.AddTemplate(tp, gap, 500 - 200 - gap);
  50.         cb.RestoreState();
  51.  
  52.         cb.SaveState();
  53.         tp = cb.CreateTemplate(200, 200);
  54.         tp.Group = group;
  55.         PdfGState gs2 = new PdfGState();
  56.         gs2.FillOpacity = 0.5f;
  57.         gs2.BlendMode = PdfGState.BM_HARDLIGHT;
  58.         tp.SetGState(gs2);
  59.         PictureCircles(0, 0, tp);
  60.         cb.AddTemplate(tp, 200 + 2 * gap, 500 - 200 - gap);
  61.         cb.RestoreState();
  62.  
  63.         cb.ResetRGBColorFill();
  64.         ColumnText ct = new ColumnText(cb);
  65.         Phrase ph = new Phrase("Ungrouped objects\nObject opacity = 1.0");
  66.         ct.SetSimpleColumn(ph, gap, 0, gap + 200, 500, 18,
  67.           Element.ALIGN_CENTER
  68.         );
  69.         ct.Go();
  70.  
  71.         ph = new Phrase("Ungrouped objects\nObject opacity = 0.5");
  72.         ct.SetSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200, 500,
  73.           18, Element.ALIGN_CENTER
  74.         );
  75.         ct.Go();
  76.  
  77.         ph = new Phrase(
  78.           "Transparency group\nObject opacity = 1.0\nGroup opacity = 0.5\nBlend mode = Normal"
  79.         );
  80.         ct.SetSimpleColumn(ph, gap, 0, gap + 200, 500 - 200 - gap, 18,
  81.           Element.ALIGN_CENTER
  82.         );
  83.         ct.Go();
  84.  
  85.         ph = new Phrase(
  86.           "Transparency group\nObject opacity = 0.5\nGroup opacity = 1.0\nBlend mode = HardLight"
  87.         );
  88.         ct.SetSimpleColumn(ph, 200 + 2 * gap, 0, 200 + 2 * gap + 200,
  89.           500 - 200 - gap, 18, Element.ALIGN_CENTER
  90.         );
  91.         ct.Go();
  92.       }
  93.     }
  94. // ---------------------------------------------------------------------------
  95.     /**
  96.      * Prints a square and fills half of it with a gray rectangle.
  97.      *
  98.      * @param x
  99.      * @param y
  100.      * @param cb
  101.      * @throws Exception
  102.      */
  103.     public static void PictureBackdrop(float x, float y, PdfContentByte cb) {
  104.       cb.SetColorStroke(GrayColor.GRAYBLACK);
  105.       cb.SetColorFill(new GrayColor(0.8f));
  106.       cb.Rectangle(x, y, 100, 200);
  107.       cb.Fill();
  108.       cb.SetLineWidth(2);
  109.       cb.Rectangle(x, y, 200, 200);
  110.       cb.Stroke();
  111.     }
  112. // ---------------------------------------------------------------------------
  113.     /**
  114.      * Prints 3 circles in different colors that intersect with eachother.
  115.      *
  116.      * @param x
  117.      * @param y
  118.      * @param cb
  119.      * @throws Exception
  120.      */
  121.     public static void PictureCircles(float x, float y, PdfContentByte cb) {
  122.       cb.SetColorFill(BaseColor.RED);
  123.       cb.Circle(x + 70, y + 70, 50);
  124.       cb.Fill();
  125.       cb.SetColorFill(BaseColor.YELLOW);
  126.       cb.Circle(x + 100, y + 130, 50);
  127.       cb.Fill();
  128.       cb.SetColorFill(BaseColor.BLUE);
  129.       cb.Circle(x + 130, y + 70, 50);
  130.       cb.Fill();
  131.     }
  132. // ===========================================================================
  133.   }
  134. }
Transparency2.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class Transparency2 : IWriter {
  18. // ===========================================================================
  19.     public void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         PdfContentByte cb = writer.DirectContent;
  28.         float gap = (document.PageSize.Width - 400) / 3;
  29.  
  30.         PictureBackdrop(gap, 500, cb, writer);
  31.         PictureBackdrop(200 + 2 * gap, 500, cb, writer);
  32.         PictureBackdrop(gap, 500 - 200 - gap, cb, writer);
  33.         PictureBackdrop(200 + 2 * gap, 500 - 200 - gap, cb, writer);
  34.         PdfTemplate tp;
  35.         PdfTransparencyGroup group;
  36.  
  37.         tp = cb.CreateTemplate(200, 200);
  38.         PictureCircles(0, 0, tp);
  39.         group = new PdfTransparencyGroup();
  40.         group.Isolated = true;
  41.         group.Knockout = true;
  42.         tp.Group = group;
  43.         cb.AddTemplate(tp, gap, 500);
  44.  
  45.         tp = cb.CreateTemplate(200, 200);
  46.         PictureCircles(0, 0, tp);
  47.         group = new PdfTransparencyGroup();
  48.         group.Isolated  = true;
  49.         group.Knockout = false;
  50.         tp.Group = group;
  51.         cb.AddTemplate(tp, 200 + 2 * gap, 500);
  52.  
  53.         tp = cb.CreateTemplate(200, 200);
  54.         PictureCircles(0, 0, tp);
  55.         group = new PdfTransparencyGroup();
  56.         group.Isolated = false;
  57.         group.Knockout = true;
  58.         tp.Group = group;
  59.         cb.AddTemplate(tp, gap, 500 - 200 - gap);
  60.  
  61.         tp = cb.CreateTemplate(200, 200);
  62.         PictureCircles(0, 0, tp);
  63.         group = new PdfTransparencyGroup();
  64.         group.Isolated = false;
  65.         group.Knockout = false;
  66.         tp.Group = group;
  67.         cb.AddTemplate(tp, 200 + 2 * gap, 500 - 200 - gap);
  68.       }
  69.     }
  70. // ---------------------------------------------------------------------------
  71.     /**
  72.      * Prints a square and fills half of it with a gray rectangle.
  73.      *
  74.      * @param x
  75.      * @param y
  76.      * @param cb
  77.      * @throws Exception
  78.      */
  79.     public static void PictureBackdrop(float x, float y, PdfContentByte cb,
  80.         PdfWriter writer)
  81.     {
  82.       PdfShading axial = PdfShading.SimpleAxial(writer, x, y, x + 200, y,
  83.         BaseColor.YELLOW, BaseColor.RED
  84.       );
  85.       PdfShadingPattern axialPattern = new PdfShadingPattern(axial);
  86.       cb.SetShadingFill(axialPattern);
  87.       cb.SetColorStroke(BaseColor.BLACK);
  88.       cb.SetLineWidth(2);
  89.       cb.Rectangle(x, y, 200, 200);
  90.       cb.FillStroke();
  91.     }    
  92. // ---------------------------------------------------------------------------
  93.     /**
  94.      * Prints 3 circles in different colors that intersect with eachother.
  95.      *
  96.      * @param x
  97.      * @param y
  98.      * @param cb
  99.      * @throws Exception
  100.      */
  101.     public static void PictureCircles(float x, float y, PdfContentByte cb) {
  102.       PdfGState gs = new PdfGState();
  103.       gs.BlendMode = PdfGState.BM_MULTIPLY;
  104.       gs.FillOpacity = 1f;
  105.       cb.SetGState(gs);
  106.       cb.SetColorFill(new CMYKColor(0f, 0f, 0f, 0.15f));
  107.       cb.Circle(x + 75, y + 75, 70);
  108.       cb.Fill();
  109.       cb.Circle(x + 75, y + 125, 70);
  110.       cb.Fill();
  111.       cb.Circle(x + 125, y + 75, 70);
  112.       cb.Fill();
  113.       cb.Circle(x + 125, y + 125, 70);
  114.       cb.Fill();
  115.     }
  116. // ===========================================================================
  117.   }
  118. }
ImageTypes.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class ImageTypes : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "image_types.pdf";
  21.     /** Paths to images. */
  22.     public string[] RESOURCES = {
  23.       "bruno_ingeborg.jpg",
  24.       "map.jp2",
  25.       "info.png",
  26.       "close.bmp",
  27.       "movie.gif",
  28.       "butterfly.wmf",
  29.       "animated_fox_dog.gif",
  30.       "marbles.tif",
  31.       "amb.jb2"
  32.     };
  33.     /** Path to an image. */
  34.     public const string RESOURCE = "hitchcock.png";
  35. // ---------------------------------------------------------------------------
  36.     public void Write(Stream stream) {
  37.       using (ZipFile zip = new ZipFile()) {
  38.         ImageTypes it = new ImageTypes();
  39.         zip.AddEntry(RESULT, it.CreatePdf());
  40. /*
  41.  * uncomment if you want to see the image files in the zip archive;
  42.  * some of them are semi-large files
  43.  *
  44.         for (int i = 0; i < RESOURCES.Length; i++) {
  45.           zip.AddFile(Path.Combine(Utility.ResourceImage, RESOURCES[i]), "");
  46.         }
  47.         zip.AddFile(Path.Combine(Utility.ResourceImage, RESOURCE), "");
  48.  */
  49.         zip.Save(stream);            
  50.       }
  51.     }
  52. // ---------------------------------------------------------------------------
  53.     /**
  54.      * Creates a PDF document.
  55.      */
  56.     public byte[] CreatePdf() {
  57.       using (MemoryStream ms = new MemoryStream()) {
  58.         using (Document document = new Document() ) {
  59.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  60.           document.Open();
  61.           Image img = null;
  62.           for (int i = 0; i < RESOURCES.Length; i++) {
  63.             img = Image.GetInstance(Path.Combine(Utility.ResourceImage, RESOURCES[i]));
  64.             if (img.ScaledWidth > 300 || img.ScaledHeight > 300) {
  65.               img.ScaleToFit(300, 300);
  66.             }
  67.             document.Add(new Paragraph(String.Format(
  68.               "{0} is an image of type {1}", RESOURCES[i], img.GetType())
  69.             ));
  70.             document.Add(img);
  71.           }
  72. /*
  73. * you DO NOT want to use classes within the System.Drawing namespace to
  74. * manipulate image files in ASP.NET applications, see the warning here:
  75. * http://msdn.microsoft.com/en-us/library/system.drawing.aspx
  76. */
  77.           using (System.Drawing.Image dotnet_img =
  78.             System.Drawing.Image.FromFile(
  79.               Path.Combine(Utility.ResourceImage, RESOURCE)
  80.             ))
  81.           {
  82.             img = Image.GetInstance(
  83.               dotnet_img, System.Drawing.Imaging.ImageFormat.Png
  84.             );
  85.             document.Add(new Paragraph(String.Format(
  86.               "{0} is an image of type {1}", "System.Drawing.Image", img.GetType())
  87.             ));
  88.             document.Add(img);
  89.           }
  90.  
  91.           BarcodeEAN codeEAN = new BarcodeEAN();
  92.           codeEAN.CodeType = Barcode.EAN13;
  93.           codeEAN.Code = "9781935182610";
  94.           img = codeEAN.CreateImageWithBarcode(writer.DirectContent, null, null);
  95.           document.Add(new Paragraph(String.Format(
  96.             "{0} is an image of type {1}", "barcode", img.GetType())
  97.           ));
  98.           document.Add(img);
  99.  
  100.           BarcodePDF417 pdf417 = new BarcodePDF417();
  101.           string text = "iText in Action, a book by Bruno Lowagie.";
  102.           pdf417.SetText(text);
  103.           img = pdf417.GetImage();
  104.           document.Add(new Paragraph(String.Format(
  105.             "{0} is an image of type {1}", "barcode", img.GetType())
  106.           ));
  107.           document.Add(img);
  108.         }
  109.         return ms.ToArray();
  110.       }
  111.     }
  112. // ===========================================================================
  113.   }
  114. }
RawImage.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class RawImage : IWriter {
  18. // ===========================================================================
  19.     public void Write(Stream stream) {
  20.       // step 1
  21.       using (Document document = new Document()) {
  22.         // step 2
  23.         PdfWriter.GetInstance(document, stream);
  24.         // step 3
  25.         document.Open();
  26.         // step 4
  27.         // Image in colorspace DeviceGray
  28.         byte[] gradient = new byte[256];
  29.         for (int i = 0; i < 256; i++) {
  30.           gradient[i] = (byte) i;
  31.         }
  32.         Image img1 = Image.GetInstance(256, 1, 1, 8, gradient);
  33.         img1.ScaleAbsolute(256, 50);
  34.         document.Add(img1);
  35.         // Image in colorspace RGB
  36.         byte[] cgradient = new byte[256 * 3];
  37.         for (int i = 0; i < 256; i++) {
  38.           cgradient[i * 3] = (byte) (255 - i);
  39.           cgradient[i * 3 + 1] = (byte) (255 - i);
  40.           cgradient[i * 3 + 2] = (byte) i;
  41.         }
  42.         Image img2 = Image.GetInstance(256, 1, 3, 8, cgradient);
  43.         img2.ScaleAbsolute(256, 50);
  44.         document.Add(img2);
  45.         Image img3 = Image.GetInstance(16, 16, 3, 8, cgradient);
  46.         img3.ScaleAbsolute(64, 64);
  47.         document.Add(img3);
  48.       }
  49.     }
  50. // ===========================================================================
  51.   }
  52. }
Barcodes.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Web;
  14. using iTextSharp.text;
  15. using iTextSharp.text.pdf;
  16.  
  17. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  18.   public class Barcodes : IWriter {
  19. // ===========================================================================
  20.     public void Write(Stream stream) {
  21.       // step 1
  22.       using (Document document = new Document(new Rectangle(340, 842))) {
  23.         // step 2
  24.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  25.         // step 3
  26.         document.Open();
  27.         // step 4
  28.         PdfContentByte cb = writer.DirectContent;
  29.  
  30.         // EAN 13
  31.         document.Add(new Paragraph("Barcode EAN.UCC-13"));
  32.         BarcodeEAN codeEAN = new BarcodeEAN();
  33.         codeEAN.Code = "4512345678906";
  34.         document.Add(new Paragraph("default:"));
  35.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  36.         codeEAN.GuardBars = false;
  37.         document.Add(new Paragraph("without guard bars:"));
  38.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  39.         codeEAN.Baseline = -1f;
  40.         codeEAN.GuardBars = true;
  41.         document.Add(new Paragraph("text above:"));
  42.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  43.         codeEAN.Baseline = codeEAN.Size;
  44.  
  45.         // UPC A
  46.         document.Add(new Paragraph("Barcode UCC-12 (UPC-A)"));
  47.         codeEAN.CodeType = Barcode.UPCA;
  48.         codeEAN.Code = "785342304749";
  49.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  50.  
  51.         // EAN 8
  52.         document.Add(new Paragraph("Barcode EAN.UCC-8"));
  53.         codeEAN.CodeType = Barcode.EAN8;
  54.         codeEAN.BarHeight = codeEAN.Size * 1.5f;
  55.         codeEAN.Code = "34569870";
  56.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  57.  
  58.         // UPC E
  59.         document.Add(new Paragraph("Barcode UPC-E"));
  60.         codeEAN.CodeType = Barcode.UPCE;
  61.         codeEAN.Code = "03456781";
  62.         document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
  63.         codeEAN.BarHeight = codeEAN.Size * 3f;
  64.  
  65.         // EANSUPP
  66.         document.Add(new Paragraph("Bookland"));
  67.         document.Add(new Paragraph("ISBN 0-321-30474-8"));
  68.         codeEAN.CodeType = Barcode.EAN13;
  69.         codeEAN.Code = "9781935182610";
  70.         BarcodeEAN codeSUPP = new BarcodeEAN();
  71.         codeSUPP.CodeType = Barcode.SUPP5;
  72.         codeSUPP.Code = "55999";
  73.         codeSUPP.Baseline = -2;
  74.         BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
  75.         document.Add(eanSupp.CreateImageWithBarcode(cb, null, BaseColor.BLUE));
  76.  
  77.         // CODE 128
  78.         document.Add(new Paragraph("Barcode 128"));
  79.         Barcode128 code128 = new Barcode128();
  80.         code128.Code = "0123456789 hello";
  81.         document.Add(code128.CreateImageWithBarcode(cb, null, null));
  82.         code128.Code = "0123456789\uffffMy Raw Barcode (0 - 9)";
  83.         code128.CodeType = Barcode.CODE128_RAW;
  84.         document.Add(code128.CreateImageWithBarcode(cb, null, null));
  85.  
  86.         // Data for the barcode :
  87.         String code402 = "24132399420058289";
  88.         String code90 = "3700000050";
  89.         String code421 = "422356";
  90.         StringBuilder data = new StringBuilder(code402);
  91.         data.Append(Barcode128.FNC1);
  92.         data.Append(code90);
  93.         data.Append(Barcode128.FNC1);
  94.         data.Append(code421);
  95.         Barcode128 shipBarCode = new Barcode128();
  96.         shipBarCode.X = 0.75f;
  97.         shipBarCode.N = 1.5f;
  98.         shipBarCode.Size = 10f;
  99.         shipBarCode.TextAlignment = Element.ALIGN_CENTER;
  100.         shipBarCode.Baseline = 10f;
  101.         shipBarCode.BarHeight = 50f;
  102.         shipBarCode.Code = data.ToString();
  103.         document.Add(shipBarCode.CreateImageWithBarcode(
  104.           cb, BaseColor.BLACK, BaseColor.BLUE
  105.         ));
  106.  
  107.         // it is composed of 3 blocks whith AI 01, 3101 and 10
  108.         Barcode128 uccEan128 = new Barcode128();
  109.         uccEan128.CodeType = Barcode.CODE128_UCC;
  110.         uccEan128.Code = "(01)00000090311314(10)ABC123(15)060916";
  111.         document.Add(uccEan128.CreateImageWithBarcode(
  112.           cb, BaseColor.BLUE, BaseColor.BLACK
  113.         ));
  114.         uccEan128.Code = "0191234567890121310100035510ABC123";
  115.         document.Add(uccEan128.CreateImageWithBarcode(
  116.           cb, BaseColor.BLUE, BaseColor.RED
  117.         ));
  118.         uccEan128.Code = "(01)28880123456788";
  119.         document.Add(uccEan128.CreateImageWithBarcode(
  120.           cb, BaseColor.BLUE, BaseColor.BLACK
  121.         ));
  122.  
  123.         // INTER25
  124.         document.Add(new Paragraph("Barcode Interleaved 2 of 5"));
  125.         BarcodeInter25 code25 = new BarcodeInter25();
  126.         code25.GenerateChecksum = true;
  127.         code25.Code = "41-1200076041-001";
  128.         document.Add(code25.CreateImageWithBarcode(cb, null, null));
  129.         code25.Code = "411200076041001";
  130.         document.Add(code25.CreateImageWithBarcode(cb, null, null));
  131.         code25.Code = "0611012345678";
  132.         code25.ChecksumText = true;
  133.         document.Add(code25.CreateImageWithBarcode(cb, null, null));
  134.  
  135.         // POSTNET
  136.         document.Add(new Paragraph("Barcode Postnet"));
  137.         BarcodePostnet codePost = new BarcodePostnet();
  138.         document.Add(new Paragraph("ZIP"));
  139.         codePost.Code = "01234";
  140.         document.Add(codePost.CreateImageWithBarcode(cb, null, null));
  141.         document.Add(new Paragraph("ZIP+4"));
  142.         codePost.Code = "012345678";
  143.         document.Add(codePost.CreateImageWithBarcode(cb, null, null));
  144.         document.Add(new Paragraph("ZIP+4 and dp"));
  145.         codePost.Code = "01234567890";
  146.         document.Add(codePost.CreateImageWithBarcode(cb, null, null));
  147.  
  148.         document.Add(new Paragraph("Barcode Planet"));
  149.         BarcodePostnet codePlanet = new BarcodePostnet();
  150.         codePlanet.Code = "01234567890";
  151.         codePlanet.CodeType = Barcode.PLANET;
  152.         document.Add(codePlanet.CreateImageWithBarcode(cb, null, null));
  153.  
  154.         // CODE 39
  155.         document.Add(new Paragraph("Barcode 3 of 9"));
  156.         Barcode39 code39 = new Barcode39();
  157.         code39.Code = "ITEXT IN ACTION";
  158.         document.Add(code39.CreateImageWithBarcode(cb, null, null));
  159.  
  160.         document.Add(new Paragraph("Barcode 3 of 9 extended"));
  161.         Barcode39 code39ext = new Barcode39();
  162.         code39ext.Code = "iText in Action";
  163.         code39ext.StartStopText = false;
  164.         code39ext.Extended = true;
  165.         document.Add(code39ext.CreateImageWithBarcode(cb, null, null));
  166.  
  167.         // CODABAR
  168.         document.Add(new Paragraph("Codabar"));
  169.         BarcodeCodabar codabar = new BarcodeCodabar();
  170.         codabar.Code = "A123A";
  171.         codabar.StartStopText = true;
  172.         document.Add(codabar.CreateImageWithBarcode(cb, null, null));
  173.  
  174.         // PDF417
  175.         document.Add(new Paragraph("Barcode PDF417"));
  176.         BarcodePDF417 pdf417 = new BarcodePDF417();
  177.         String text = "Call me Ishmael. Some years ago--never mind how long "
  178.         + "precisely --having little or no money in my purse, and nothing "
  179.               + "particular to interest me on shore, I thought I would sail about "
  180.               + "a little and see the watery part of the world."
  181.             ;
  182.         pdf417.SetText(text);
  183.         Image img = pdf417.GetImage();
  184.         img.ScalePercent(50, 50 * pdf417.YHeight);
  185.         document.Add(img);
  186.  
  187.         document.Add(new Paragraph("Barcode Datamatrix"));
  188.         BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
  189.         datamatrix.Generate(text);
  190.         img = datamatrix.CreateImage();
  191.         document.Add(img);
  192.  
  193.         document.Add(new Paragraph("Barcode QRCode"));
  194.         BarcodeQRCode qrcode = new BarcodeQRCode(
  195.           "Moby Dick by Herman Melville", 1, 1, null
  196.         );
  197.         img = qrcode.GetImage();
  198.         document.Add(img);        
  199.       }
  200.     }
  201. // ===========================================================================
  202.   }
  203. }
CompressImage.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class CompressImage : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const string RESULT1 = "uncompressed.pdf";
  21.     /** The resulting PDF file. */
  22.     public const string RESULT2 = "compressed.pdf";
  23.     /** One of the resources. */
  24.     public const string RESOURCE = "butterfly.bmp";
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       using (ZipFile zip = new ZipFile()) {
  28.         CompressImage c = new CompressImage();
  29.         zip.AddEntry(RESULT1, c.CreatePdf(false));
  30.         zip.AddEntry(RESULT2, c.CreatePdf(true));
  31.         zip.AddFile(Path.Combine(Utility.ResourceImage, RESOURCE), "");
  32.         zip.Save(stream);            
  33.       }
  34.     }
  35. // ---------------------------------------------------------------------------
  36.     /**
  37.      * Creates a PDF document.
  38.      * @param filename the path to the new PDF document
  39.      */    
  40.     public byte[] CreatePdf(bool compress)  {
  41.       using (MemoryStream ms = new MemoryStream()) {
  42.       // step 1
  43.         using (Document document = new Document()) {
  44.           // step 2
  45.           PdfWriter.GetInstance(document, ms);
  46.           // step 3
  47.           document.Open();
  48.           // step 4
  49.           Image img = Image.GetInstance(
  50.             Path.Combine(Utility.ResourceImage, RESOURCE)
  51.           );
  52.           if (compress) img.CompressionLevel = 9;
  53.           document.Add(img);
  54.         }
  55.         return ms.ToArray();
  56.       }
  57.     }    
  58. // ===========================================================================
  59.   }
  60. }
CompressAwt.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 System.Collections.Generic;
  11.  
  12. using System.Linq;
  13. using System.Web;
  14. using iTextSharp.text;
  15. using iTextSharp.text.pdf;
  16.  
  17. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  18.   public class CompressAwt : IWriter {
  19. // ===========================================================================
  20.     /** The resulting PDF file. */
  21.     public const string RESULT1 = "hitchcock100.pdf";
  22.     /** The resulting PDF file. */
  23.     public const string RESULT2 = "hitchcock20.pdf";
  24.     /** The resulting PDF file. */
  25.     public const string RESULT3 = "hitchcock10.pdf";
  26.     /** One of the resources. */
  27.     public static string RESOURCE = Path.Combine(
  28.       Utility.ResourceImage, "hitchcock.png"
  29.     );
  30. // ---------------------------------------------------------------------------
  31.     public void Write(Stream stream) {
  32.       using (ZipFile zip = new ZipFile()) {
  33.         CompressAwt c = new CompressAwt();
  34.         zip.AddEntry(RESULT1, c.CreatePdf(100L));
  35.         zip.AddEntry(RESULT2, c.CreatePdf(20L));
  36.         zip.AddEntry(RESULT3, c.CreatePdf(10L));
  37.         zip.AddFile(RESOURCE, "");
  38.         zip.Save(stream);            
  39.       }
  40.     }
  41. // ---------------------------------------------------------------------------
  42. /*
  43.  * you DO NOT want to use classes within the System.Drawing namespace to
  44.  * manipulate image files in ASP.NET applications, see the warning here:
  45.  * http://msdn.microsoft.com/en-us/library/system.drawing.aspx
  46. */
  47.     public byte[] CreatePdf(long quality) {
  48.       using (MemoryStream msDoc = new MemoryStream()) {
  49.         Image img = null;
  50.         using (System.Drawing.Bitmap dotnetImg =
  51.             new System.Drawing.Bitmap(RESOURCE))
  52.         {
  53.           // set codec to jpeg type => jpeg index codec is "1"
  54.           System.Drawing.Imaging.ImageCodecInfo codec =
  55.           System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
  56.           // set parameters for image quality
  57.           System.Drawing.Imaging.EncoderParameters eParams =
  58.             new System.Drawing.Imaging.EncoderParameters(1);
  59.           eParams.Param[0] =
  60.             new System.Drawing.Imaging.EncoderParameter(
  61.               System.Drawing.Imaging.Encoder.Quality, quality
  62.           );
  63.           using (MemoryStream msImg = new MemoryStream()) {
  64.             dotnetImg.Save(msImg, codec, eParams);
  65.             msImg.Position = 0;
  66.             img = Image.GetInstance(msImg);
  67.             img.SetAbsolutePosition(15, 15);
  68.             // step 1
  69.             using (Document document = new Document()) {
  70.               // step 2
  71.               PdfWriter.GetInstance(document, msDoc);
  72.               // step 3
  73.               document.Open();
  74.               // step 4
  75.               document.Add(img);
  76.             }
  77.           }
  78.         }
  79.         return msDoc.ToArray();
  80.       }
  81.     }
  82. // ===========================================================================
  83.   }
  84. }
PagedImages.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15. using iTextSharp.text.pdf.codec;
  16.  
  17. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  18.   public class PagedImages : IWriter {
  19. // ===========================================================================
  20.     /** The resulting PDF file. */
  21.     public const String RESULT = "tiff_jbig2_gif.pdf";
  22.     /** One of the resources. */
  23.     public static string RESOURCE1 = Path.Combine(Utility.ResourceImage, "marbles.tif");
  24.     /** One of the resources. */
  25.     public static string RESOURCE2 = Path.Combine(Utility.ResourceImage, "amb.jb2");
  26.     /** One of the resources. */
  27.     public static string RESOURCE3 = Path.Combine(Utility.ResourceImage, "animated_fox_dog.gif");
  28. // ---------------------------------------------------------------------------
  29.     public void Write(Stream stream) {
  30.       using (ZipFile zip = new ZipFile()) {
  31.         PagedImages p = new PagedImages();
  32.         zip.AddEntry(RESULT, p.CreatePdf());
  33.         zip.AddFile(RESOURCE1, "");
  34.         zip.AddFile(RESOURCE2, "");
  35.         zip.AddFile(RESOURCE3, "");
  36.         zip.Save(stream);
  37.       }
  38.     }    
  39. // ---------------------------------------------------------------------------
  40.     /**
  41.      * Creates a PDF document.
  42.      */
  43.     public byte[] CreatePdf() {
  44.       using (MemoryStream ms = new MemoryStream()) {
  45.         // step 1
  46.         using (Document document = new Document()) {
  47.           // step 2
  48.           PdfWriter.GetInstance(document, ms);
  49.           // step 3
  50.           document.Open();
  51.           // step 4
  52.           AddTif(document, RESOURCE1);
  53.           document.NewPage();
  54.           AddJBIG2(document, RESOURCE2);
  55.           document.NewPage();
  56.           AddGif(document, RESOURCE3);
  57.         }
  58.         return ms.ToArray();
  59.       }
  60.     }
  61. // ---------------------------------------------------------------------------    
  62.     public void AddTif(Document document, String path) {
  63.       RandomAccessFileOrArray ra = new RandomAccessFileOrArray(path);
  64.       int n = TiffImage.GetNumberOfPages(ra);
  65.       Image img;
  66.       for (int i = 1; i
TransparentImage.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class TransparentImage : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "transparant_image.pdf";
  21.     /** One of the resources. */
  22.     public static string RESOURCE1 = Path.Combine(Utility.ResourceImage, "bruno.jpg");
  23.     /** One of the resources. */
  24.     public static string RESOURCE2 = Path.Combine(Utility.ResourceImage, "info.png");
  25.     /** One of the resources. */
  26.     public static string RESOURCE3 = Path.Combine(Utility.ResourceImage, "1t3xt.gif");
  27.     /** One of the resources. */
  28.     public static string RESOURCE4 = Path.Combine(Utility.ResourceImage, "logo.gif");
  29. // ---------------------------------------------------------------------------
  30.     public void Write(Stream stream) {
  31.       using (ZipFile zip = new ZipFile()) {
  32.         TransparentImage t = new TransparentImage();
  33.         zip.AddEntry(RESULT, t.CreatePdf());
  34.         zip.AddFile(RESOURCE1, "");
  35.         zip.AddFile(RESOURCE2, "");
  36.         zip.AddFile(RESOURCE3, "");
  37.         zip.AddFile(RESOURCE4, "");
  38.         zip.Save(stream);            
  39.       }
  40.     }
  41. // ---------------------------------------------------------------------------
  42.     /**
  43.      * Creates a PDF document.
  44.      */
  45.     public byte[] CreatePdf() {
  46.       using (MemoryStream ms = new MemoryStream()) {
  47.         Image img1 = Image.GetInstance(
  48.           Path.Combine(Utility.ResourceImage, RESOURCE1)
  49.         );
  50.         // step 1
  51.         using (Document document = new Document()) {
  52.           // step 2
  53.           PdfWriter.GetInstance(document, ms);
  54.           // step 3
  55.           document.Open();
  56.           // step 4
  57.           img1.SetAbsolutePosition(0, 0);
  58.           document.Add(img1);
  59.           Image img2 = Image.GetInstance(
  60.             Path.Combine(Utility.ResourceImage, RESOURCE2)
  61.           );
  62.           img2.SetAbsolutePosition(0, 260);
  63.           document.Add(img2);
  64.           Image img3 = Image.GetInstance(
  65.             Path.Combine(Utility.ResourceImage,RESOURCE3)
  66.           );
  67.           img3.Transparency = new int[]{ 0x00, 0x10 };
  68.           img3.SetAbsolutePosition(0, 0);
  69.           document.Add(img3);
  70.           Image img4 = Image.GetInstance(
  71.             Path.Combine(Utility.ResourceImage,RESOURCE4)
  72.           );
  73.           img4.Transparency = new int[]{ 0xF0, 0xFF };
  74.           img4.SetAbsolutePosition(50, 50);
  75.           document.Add(img4);
  76.         }
  77.         return ms.ToArray();
  78.       }
  79.     }
  80. // ===========================================================================
  81.   }
  82. }
TransparentAwt.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class TransparentAwt : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "transparant_hitchcock.pdf";
  21.     /** One of the resources. */
  22.     public static string RESOURCE = Path.Combine(
  23.       Utility.ResourceImage, "hitchcock.gif"
  24.     );
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       using (ZipFile zip = new ZipFile()) {
  28.         TransparentAwt t = new TransparentAwt();
  29.         zip.AddEntry(RESULT, t.CreatePdf());
  30.         zip.AddFile(RESOURCE, "");
  31.         zip.Save(stream);            
  32.       }
  33.     }
  34. // ---------------------------------------------------------------------------
  35.     /**
  36.      * Creates a PDF document.
  37.      */
  38.     public byte[] CreatePdf() {
  39.       using (MemoryStream ms = new MemoryStream()) {
  40.         // step 1
  41.         Rectangle r = new Rectangle(PageSize.A4);
  42.         r.BackgroundColor = new GrayColor(0.8f);
  43.         using (Document document = new Document(r)) {
  44.           // step 2
  45.           PdfWriter.GetInstance(document, ms);
  46.           // step 3
  47.           document.Open();
  48.           // step 4
  49. /*
  50. * you DO NOT want to use classes within the System.Drawing namespace to
  51. * manipulate image files in ASP.NET applications, see the warning here:
  52. * http://msdn.microsoft.com/en-us/library/system.drawing.aspx
  53. */
  54.           using (System.Drawing.Image dotnet_img =
  55.             System.Drawing.Image.FromFile(
  56.               Path.Combine(Utility.ResourceImage, RESOURCE)
  57.             ))
  58.           {
  59.             document.Add(new Paragraph("Hitchcock in Red."));
  60.             Image img1 = Image.GetInstance(
  61.               dotnet_img, System.Drawing.Imaging.ImageFormat.Gif
  62.             );
  63.             document.Add(img1);
  64.             document.Add(new Paragraph("Hitchcock in Black and White."));
  65.             Image img2 = Image.GetInstance(dotnet_img, null, true);
  66.             document.Add(img2);
  67.             document.NewPage();
  68.             document.Add(new Paragraph("Hitchcock in Red and Yellow."));
  69.             Image img3 = Image.GetInstance(
  70.               dotnet_img, new BaseColor(0xFF, 0xFF, 0x00)
  71.             );
  72.             document.Add(img3);
  73.             document.Add(new Paragraph("Hitchcock in Black and White."));
  74.             Image img4 = Image.GetInstance(
  75.               dotnet_img, new BaseColor(0xFF, 0xFF, 0x00), true
  76.             );
  77.             document.Add(img4);
  78.           }
  79.         }
  80.         return ms.ToArray();
  81.       }
  82.     }
  83. // ===========================================================================
  84.   }
  85. }
ImageMask.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class ImageMask : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT1 = "hardmask.pdf";
  21.     /** The resulting PDF file. */
  22.     public const String RESULT2 = "softmask.pdf";
  23.     /** One of the resources. */
  24.     public static string RESOURCE = Path.Combine(Utility.ResourceImage, "bruno.jpg");
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {    
  27.       using (ZipFile zip = new ZipFile()) {
  28.         byte[] circledata = {
  29.           (byte) 0x3c, (byte) 0x7e, (byte) 0xff, (byte) 0xff,
  30.           (byte) 0xff, (byte) 0xff, (byte) 0x7e, (byte) 0x3c
  31.         };
  32.         Image mask = Image.GetInstance(8, 8, 1, 1, circledata);
  33.         mask.MakeMask();
  34.         mask.Inverted = true;
  35.        
  36.         ImageMask im = new ImageMask();
  37.         zip.AddEntry(RESULT1, im.CreatePdf(mask));
  38.        
  39.         byte[] gradient = new byte[256];
  40.         for (int i = 0; i < 256; i++) {
  41.           gradient[i] = (byte) i;
  42.         }
  43.         mask = Image.GetInstance(256, 1, 1, 8, gradient);
  44.         mask.MakeMask();
  45.         im = new ImageMask();
  46.         zip.AddEntry(RESULT2, im.CreatePdf(mask));
  47.  
  48.         zip.AddFile(RESOURCE, "");
  49.         zip.Save(stream);            
  50.       }
  51.     }
  52. // ---------------------------------------------------------------------------
  53.     /**
  54.      * Creates a PDF document.
  55.      */
  56.     public byte[] CreatePdf(Image mask) {
  57.       using (MemoryStream ms = new MemoryStream()) {
  58.         // step 1
  59.         using (Document document = new Document()) {
  60.           // step 2
  61.           PdfWriter.GetInstance(document, ms);
  62.           // step 3
  63.           document.Open();
  64.           // step 4
  65.           Image img = Image.GetInstance(RESOURCE);
  66.           img.ImageMask = mask;
  67.           document.Add(img);
  68.         }
  69.         return ms.ToArray();
  70.       }
  71.     }
  72. // ===========================================================================
  73.   }
  74. }
TemplateClip.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class TemplateClip : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "template_clip.pdf";
  21.     /** One of the resources. */
  22.     public static string RESOURCE = Path.Combine(
  23.       Utility.ResourceImage, "bruno_ingeborg.jpg"
  24.     );
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       using (ZipFile zip = new ZipFile()) {
  28.         TemplateClip t = new TemplateClip();
  29.         zip.AddEntry(RESULT, t.CreatePdf());
  30.         zip.AddFile(RESOURCE, "");
  31.         zip.Save(stream);            
  32.       }
  33.     }
  34. // ---------------------------------------------------------------------------
  35.     public byte[] CreatePdf() {
  36.       using (MemoryStream ms = new MemoryStream()) {    
  37.         // step 1
  38.         using (Document document = new Document()) {
  39.           // step 2
  40.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  41.           // step 3
  42.           document.Open();
  43.           // step 4
  44.           Image img = Image.GetInstance(
  45.             Path.Combine(Utility.ResourceImage, RESOURCE)
  46.           );
  47.           float w = img.ScaledWidth;
  48.           float h = img.ScaledHeight;
  49.           PdfTemplate t = writer.DirectContent.CreateTemplate(850, 600);
  50.           t.AddImage(img, w, 0, 0, h, 0, -600);
  51.           Image clipped = Image.GetInstance(t);
  52.           clipped.ScalePercent(50);
  53.           document.Add(clipped);
  54.         }
  55.         return ms.ToArray();
  56.       }
  57.     }
  58. // ===========================================================================
  59.   }
  60. }
ClippingPath.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class ClippingPath : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "clipping_path.pdf";
  21.     /** One of the resources. */
  22.     public static string RESOURCE = Path.Combine(
  23.       Utility.ResourceImage, "bruno_ingeborg.jpg"
  24.     );
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       using (ZipFile zip = new ZipFile()) {
  28.         ClippingPath c = new ClippingPath();
  29.         zip.AddEntry(RESULT, c.CreatePdf());
  30.         zip.AddFile(RESOURCE, "");
  31.         zip.Save(stream);            
  32.       }
  33.     }
  34. // ---------------------------------------------------------------------------
  35.     public byte[] CreatePdf() {
  36.       using (MemoryStream ms = new MemoryStream()) {    
  37.         // step 1
  38.         using (Document document = new Document()) {
  39.           // step 2
  40.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  41.           // step 3
  42.           document.Open();
  43.           // step 4
  44.           Image img = Image.GetInstance(
  45.             Path.Combine(Utility.ResourceImage, RESOURCE)
  46.           );
  47.           float w = img.ScaledWidth;
  48.           float h = img.ScaledHeight;
  49.           PdfTemplate t = writer.DirectContent.CreateTemplate(850, 600);
  50.           t.Ellipse(0, 0, 850, 600);
  51.           t.Clip();
  52.           t.NewPath();
  53.           t.AddImage(img, w, 0, 0, h, 0, -600);
  54.           Image clipped = Image.GetInstance(t);
  55.           clipped.ScalePercent(50);
  56.           document.Add(clipped);        
  57.         }
  58.         return ms.ToArray();
  59.       }
  60.     }
  61. // ===========================================================================
  62.   }
  63. }
TransparentOverlay.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 System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter10 {
  17.   public class TransparentOverlay : IWriter {
  18. // ===========================================================================
  19.     /** The resulting PDF file. */
  20.     public const String RESULT = "transparent_overlay.pdf";
  21.     /** One of the resources. */
  22.     public static string RESOURCE = Path.Combine(
  23.       Utility.ResourceImage, "bruno_ingeborg.jpg"
  24.     );
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       using (ZipFile zip = new ZipFile()) {
  28.         TransparentOverlay t = new TransparentOverlay();
  29.         zip.AddEntry(RESULT, t.CreatePdf());
  30.         zip.AddFile(RESOURCE, "");
  31.         zip.Save(stream);            
  32.       }
  33.     }
  34. // ---------------------------------------------------------------------------
  35.     public byte[] CreatePdf() {
  36.       using (MemoryStream ms = new MemoryStream()) {    
  37.         // step 1
  38.         using (Document document = new Document(new Rectangle(850, 600))) {
  39.           // step 2
  40.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  41.           // step 3
  42.           document.Open();
  43.           // step 4
  44.           PdfContentByte canvas = writer.DirectContent;
  45.           // add the clipped image
  46.           Image img = Image.GetInstance(
  47.             Path.Combine(Utility.ResourceImage, RESOURCE)
  48.           );
  49.           float w = img.ScaledWidth;
  50.           float h = img.ScaledHeight;
  51.           canvas.Ellipse(1, 1, 848, 598);
  52.           canvas.Clip();
  53.           canvas.NewPath();
  54.           canvas.AddImage(img, w, 0, 0, h, 0, -600);
  55.  
  56.           // Create a transparent PdfTemplate
  57.           PdfTemplate t2 = writer.DirectContent.CreateTemplate(850, 600);
  58.           PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
  59.           transGroup.Put( PdfName.CS, PdfName.DEVICEGRAY);
  60.           transGroup.Isolated = true;
  61.           transGroup.Knockout = false;
  62.           t2.Group = transGroup;
  63.  
  64.           // Add transparent ellipses to the template
  65.           int gradationStep = 30;
  66.           float[] gradationRatioList = new float[gradationStep];
  67.           for(int i = 0; i < gradationStep; i++) {
  68. /*
  69. * gotta love .NET, guess they forgot to copy java.lang.Math.toRadians
  70. */
  71.             double radians = (Math.PI / 180) * 90.0f / gradationStep * (i + 1);
  72.             gradationRatioList[i] = 1 - (float) Math.Sin(radians);
  73.           }
  74.           for(int i = 1; i < gradationStep + 1; i++) {
  75.               t2.SetLineWidth(5 * (gradationStep + 1 - i));
  76.               t2.SetGrayStroke(gradationRatioList[gradationStep - i]);
  77.               t2.Ellipse(0, 0, 850, 600);
  78.               t2.Stroke();
  79.           }
  80.          
  81.           // Create an image mask for the direct content
  82.           PdfDictionary maskDict = new PdfDictionary();
  83.           maskDict.Put(PdfName.TYPE, PdfName.MASK);
  84.           maskDict.Put(PdfName.S, new PdfName("Luminosity"));
  85.           maskDict.Put(new PdfName("G"), t2.IndirectReference);
  86.           PdfGState gState = new PdfGState();
  87.           gState.Put(PdfName.SMASK, maskDict );
  88.           canvas.SetGState(gState);
  89.          
  90.           canvas.AddTemplate(t2, 0, 0);        
  91.         }
  92.         return ms.ToArray();
  93.       }
  94.     }
  95. // ===========================================================================
  96.   }
  97. }
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