iText 7 jump-start tutorial : Chapter 2

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

6th April 2016
admin-marketing

Switch code for this example

C02E01_Axes.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter02;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfPage;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
  20. import com.itextpdf.test.annotations.WrapToTest;
  21.  
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * Simple drawing lines example.
  27.  */
  28. @WrapToTest
  29. public class C02E01_Axes {
  30.  
  31.     public static final String DEST = "results/chapter02/axes.pdf";
  32.  
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C02E01_Axes().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException {
  40.  
  41.         //Initialize PDF document
  42.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  43.  
  44.         PageSize ps = PageSize.A4.rotate();
  45.         PdfPage page = pdf.addNewPage(ps);
  46.  
  47.         PdfCanvas canvas = new PdfCanvas(page);
  48.         //Replace the origin of the coordinate system to the center of the page
  49.         canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2);
  50.  
  51.         C02E01_Axes.drawAxes(canvas, ps);
  52.  
  53.         //Close document
  54.         pdf.close();
  55.  
  56.     }
  57.  
  58.     public static void drawAxes(PdfCanvas canvas, PageSize ps) {
  59.         //Draw X axis
  60.         canvas.moveTo(-(ps.getWidth() / 2 - 15), 0)
  61.                 .lineTo(ps.getWidth() / 2 - 15, 0)
  62.                 .stroke();
  63.  
  64.         //Draw X axis arrow
  65.         canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
  66.                 .moveTo(ps.getWidth() / 2 - 25, -10)
  67.                 .lineTo(ps.getWidth() / 2 - 15, 0)
  68.                 .lineTo(ps.getWidth() / 2 - 25, 10).stroke()
  69.                 .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER);
  70.  
  71.         //Draw Y axis
  72.         canvas.moveTo(0, -(ps.getHeight() / 2 - 15))
  73.                 .lineTo(0, ps.getHeight() / 2 - 15)
  74.                 .stroke();
  75.        
  76.         //Draw Y axis arrow
  77.         canvas.saveState()
  78.                 .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
  79.                 .moveTo(-10, ps.getHeight() / 2 - 25)
  80.                 .lineTo(0, ps.getHeight() / 2 - 15)
  81.                 .lineTo(10, ps.getHeight() / 2 - 25).stroke()
  82.                 .restoreState();
  83.  
  84.         //Draw X serif
  85.         for (int i = -((int) ps.getWidth() / 2 - 61); i < ((int) ps.getWidth() / 2 - 60); i += 40) {
  86.             canvas.moveTo(i, 5).lineTo(i, -5);
  87.         }
  88.         //Draw Y serif
  89.         for (int j = -((int) ps.getHeight() / 2 - 57); j < ((int) ps.getHeight() / 2 - 56); j += 40) {
  90.             canvas.moveTo(5, j).lineTo(-5, j);
  91.         }
  92.         canvas.stroke();
  93.     }
  94. }
C02E02_GridLines.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter02;
  13.  
  14. import com.itextpdf.kernel.colors.Color;
  15. import com.itextpdf.kernel.colors.DeviceCmyk;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfPage;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.test.annotations.WrapToTest;
  22.  
  23. import java.io.File;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * Simple changing graphics state example.
  28.  */
  29. @WrapToTest
  30. public class C02E02_GridLines {
  31.  
  32.     public static final String DEST = "results/chapter02/grid_lines.pdf";
  33.  
  34.     public static void main(String args[]) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new C02E02_GridLines().createPdf(DEST);
  38.     }
  39.  
  40.     public void createPdf(String dest) throws IOException {
  41.  
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.  
  45.         PageSize ps = PageSize.A4.rotate();
  46.         PdfPage page = pdf.addNewPage(ps);
  47.  
  48.         PdfCanvas canvas = new PdfCanvas(page);
  49.         //Replace the origin of the coordinate system to the center of the page
  50.         canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2);
  51.  
  52.         Color grayColor = new DeviceCmyk(0.f, 0.f, 0.f, 0.875f);
  53.         Color greenColor = new DeviceCmyk(1.f, 0.f, 1.f, 0.176f);
  54.         Color blueColor = new DeviceCmyk(1.f, 0.156f, 0.f, 0.118f);
  55.        
  56.         canvas.setLineWidth(0.5f).setStrokeColor(blueColor);
  57.  
  58.         //Draw horizontal grid lines
  59.         for (int i = -((int) ps.getHeight() / 2 - 57); i < ((int) ps.getHeight() / 2 - 56); i += 40) {
  60.             canvas.moveTo(-(ps.getWidth() / 2 - 15), i)
  61.                     .lineTo(ps.getWidth() / 2 - 15, i);
  62.         }
  63.         //Draw vertical grid lines
  64.         for (int j = -((int) ps.getWidth() / 2 - 61); j < ((int) ps.getWidth() / 2 - 60); j += 40) {
  65.             canvas.moveTo(j, -(ps.getHeight() / 2 - 15))
  66.                     .lineTo(j, ps.getHeight() / 2 - 15);
  67.         }
  68.         canvas.stroke();
  69.  
  70.         //Draw axes
  71.         canvas.setLineWidth(3).setStrokeColor(grayColor);
  72.         C02E01_Axes.drawAxes(canvas, ps);
  73.  
  74.         //Draw plot
  75.         canvas.setLineWidth(2).setStrokeColor(greenColor)
  76.                 .setLineDash(10, 10, 8)
  77.                 .moveTo(-(ps.getWidth() / 2 - 15), -(ps.getHeight() / 2 - 15))
  78.                 .lineTo(ps.getWidth() / 2 - 15, ps.getHeight() / 2 - 15).stroke();
  79.  
  80.         //Close document
  81.         pdf.close();
  82.  
  83.     }
  84. }
C02E03_StarWars.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter02;
  13.  
  14. import com.itextpdf.io.font.constants.StandardFonts;
  15. import com.itextpdf.kernel.font.PdfFontFactory;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfPage;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.test.annotations.WrapToTest;
  22.  
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28. /**
  29.  * Simple drawing text example.
  30.  */
  31. @WrapToTest
  32. public class C02E03_StarWars {
  33.  
  34.     public static final String DEST = "results/chapter02/star_wars.pdf";
  35.  
  36.     public static void main(String args[]) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new C02E03_StarWars().createPdf(DEST);
  40.     }
  41.  
  42.     public void createPdf(String dest) throws IOException {
  43.  
  44.         //Initialize PDF document
  45.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  46.  
  47.         //Add new page
  48.         PageSize ps = PageSize.A4;
  49.         PdfPage page = pdf.addNewPage(ps);
  50.  
  51.         PdfCanvas canvas = new PdfCanvas(page);
  52.  
  53.         List text = new ArrayList();
  54.         text.add("         Episode V         ");
  55.         text.add("  THE EMPIRE STRIKES BACK  ");
  56.         text.add("It is a dark time for the");
  57.         text.add("Rebellion. Although the Death");
  58.         text.add("Star has been destroyed,");
  59.         text.add("Imperial troops have driven the");
  60.         text.add("Rebel forces from their hidden");
  61.         text.add("base and pursued them across");
  62.         text.add("the galaxy.");
  63.         text.add("Evading the dreaded Imperial");
  64.         text.add("Starfleet, a group of freedom");
  65.         text.add("fighters led by Luke Skywalker");
  66.         text.add("has established a new secret");
  67.         text.add("base on the remote ice world");
  68.         text.add("of Hoth...");
  69.  
  70.         //Replace the origin of the coordinate system to the top left corner
  71.         canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight());
  72.         canvas.beginText()
  73.                 .setFontAndSize(PdfFontFactory.createFont(StandardFonts.COURIER_BOLD), 14)
  74.                 .setLeading(14 * 1.2f)
  75.                 .moveText(70, -40);
  76.         for (String s : text) {
  77.             //Add text and move to the next line
  78.             canvas.newlineShowText(s);
  79.         }
  80.         canvas.endText();
  81.  
  82.         //Close document
  83.         pdf.close();
  84.  
  85.     }
  86. }
C02E04_StarWarsCrawl.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example is part of the iText 7 tutorial.
  11.  */
  12. package tutorial.chapter02;
  13.  
  14. import com.itextpdf.io.font.constants.StandardFonts;
  15. import com.itextpdf.kernel.colors.Color;
  16. import com.itextpdf.kernel.colors.ColorConstants;
  17. import com.itextpdf.kernel.colors.DeviceCmyk;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.geom.PageSize;
  20. import com.itextpdf.kernel.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfPage;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30.  
  31. /**
  32.  * Simple changing text state example.
  33.  */
  34.  
  35. @WrapToTest
  36. public class C02E04_StarWarsCrawl {
  37.  
  38.     public static final String DEST = "results/chapter02/star_wars_crawl.pdf";
  39.  
  40.     public static void main(String args[]) throws IOException {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new C02E04_StarWarsCrawl().createPdf(DEST);
  44.     }
  45.  
  46.     public void createPdf(String dest) throws IOException {
  47.  
  48.         List text = new ArrayList();
  49.         text.add("            Episode V      ");
  50.         text.add("    THE EMPIRE STRIKES BACK  ");
  51.         text.add("It is a dark time for the");
  52.         text.add("Rebellion. Although the Death");
  53.         text.add("Star has been destroyed,");
  54.         text.add("Imperial troops have driven the");
  55.         text.add("Rebel forces from their hidden");
  56.         text.add("base and pursued them across");
  57.         text.add("the galaxy.");
  58.         text.add("Evading the dreaded Imperial");
  59.         text.add("Starfleet, a group of freedom");
  60.         text.add("fighters led by Luke Skywalker");
  61.         text.add("has established a new secret");
  62.         text.add("base on the remote ice world");
  63.         text.add("of Hoth...");
  64.  
  65.         int maxStringWidth = 0;
  66.         for (String fragment : text) {
  67.             if (fragment.length() > maxStringWidth) maxStringWidth = fragment.length();
  68.         }
  69.  
  70.         //Initialize PDF document
  71.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  72.  
  73.         //Add new page
  74.         PageSize ps = PageSize.A4;
  75.         PdfPage page = pdf.addNewPage(ps);
  76.  
  77.         PdfCanvas canvas = new PdfCanvas(page);
  78.  
  79.         //Set black background
  80.         canvas.rectangle(0, 0, ps.getWidth(), ps.getHeight())
  81.                 .setColor(ColorConstants.BLACK, true)
  82.                 .fill();
  83.  
  84.         //Replace the origin of the coordinate system to the top left corner
  85.         canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight());
  86.         Color yellowColor = new DeviceCmyk(0.f, 0.0537f, 0.769f, 0.051f);
  87.         float lineHeight = 5;
  88.         float yOffset = -40;
  89.         canvas.beginText()
  90.             .setFontAndSize(PdfFontFactory.createFont(StandardFonts.COURIER_BOLD), 1)
  91.             .setColor(yellowColor, true);
  92.         for (int j = 0; j < text.size(); j++) {
  93.             String line = text.get(j);
  94.             float xOffset = ps.getWidth() / 2 - 45 - 8 * j;
  95.             float fontSizeCoeff = 6 + j;
  96.             float lineSpacing = (lineHeight + j) * j / 1.5f;
  97.             int stringWidth = line.length();
  98.             for (int i = 0; i < stringWidth; i++) {
  99.                 float angle = (maxStringWidth / 2 - i) / 2f;
  100.                 float charXOffset = (4 + (float) j / 2) * i;
  101.                 canvas.setTextMatrix(fontSizeCoeff, 0,
  102.                         angle, fontSizeCoeff / 1.5f,
  103.                         xOffset + charXOffset, yOffset - lineSpacing)
  104.                     .showText(String.valueOf(line.charAt(i)));
  105.             }
  106.         }
  107.         canvas.endText();
  108.  
  109.         //Close document
  110.         pdf.close();
  111.  
  112.     }
  113. }
C02E01_Axes.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.Kernel.Geom;
  7. using iText.Kernel.Pdf;
  8. using iText.Kernel.Pdf.Canvas;
  9. using iText.Test.Attributes;
  10.  
  11. namespace Tutorial.Chapter02 {
  12.     /// Simple drawing lines example.
  13.     [WrapToTest]
  14.     public class C02E01_Axes {
  15.         public const String DEST = "../../results/chapter02/axes.pdf";
  16.  
  17.         ///
  18.         public static void Main(String[] args) {
  19.             FileInfo file = new FileInfo(DEST);
  20.             file.Directory.Create();
  21.             new C02E01_Axes().CreatePdf(DEST);
  22.         }
  23.  
  24.         ///
  25.         public virtual void CreatePdf(String dest) {
  26.             //Initialize PDF document
  27.             PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  28.             PageSize ps = PageSize.A4.Rotate();
  29.             PdfPage page = pdf.AddNewPage(ps);
  30.             PdfCanvas canvas = new PdfCanvas(page);
  31.             //Replace the origin of the coordinate system to the center of the page
  32.             canvas.ConcatMatrix(1, 0, 0, 1, ps.GetWidth() / 2, ps.GetHeight() / 2);
  33.             C02E01_Axes.DrawAxes(canvas, ps);
  34.             //Close document
  35.             pdf.Close();
  36.         }
  37.  
  38.         public static void DrawAxes(PdfCanvas canvas, PageSize ps) {
  39.             //Draw X axis
  40.             canvas.MoveTo(-(ps.GetWidth() / 2 - 15), 0).LineTo(ps.GetWidth() / 2 - 15, 0).Stroke();
  41.             //Draw X axis arrow
  42.             canvas.SetLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND).MoveTo(ps.GetWidth() / 2 - 25, -10).LineTo
  43.                 (ps.GetWidth() / 2 - 15, 0).LineTo(ps.GetWidth() / 2 - 25, 10).Stroke().SetLineJoinStyle(PdfCanvasConstants.LineJoinStyle
  44.                 .MITER);
  45.             //Draw Y axis
  46.             canvas.MoveTo(0, -(ps.GetHeight() / 2 - 15)).LineTo(0, ps.GetHeight() / 2 - 15).Stroke();
  47.             //Draw Y axis arrow
  48.             canvas.SaveState().SetLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND).MoveTo(-10, ps.GetHeight() / 2
  49.                  - 25).LineTo(0, ps.GetHeight() / 2 - 15).LineTo(10, ps.GetHeight() / 2 - 25).Stroke().RestoreState();
  50.             //Draw X serif
  51.             for (int i = -((int)ps.GetWidth() / 2 - 61); i < ((int)ps.GetWidth() / 2 - 60); i += 40) {
  52.                 canvas.MoveTo(i, 5).LineTo(i, -5);
  53.             }
  54.             //Draw Y serif
  55.             for (int j = -((int)ps.GetHeight() / 2 - 57); j < ((int)ps.GetHeight() / 2 - 56); j += 40) {
  56.                 canvas.MoveTo(5, j).LineTo(-5, j);
  57.             }
  58.             canvas.Stroke();
  59.         }
  60.     }
  61. }
C02E02_GridLines.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.IO;
  6. using iText.Kernel.Colors;
  7. using iText.Kernel.Geom;
  8. using iText.Kernel.Pdf;
  9. using iText.Kernel.Pdf.Canvas;
  10. using iText.Test.Attributes;
  11.  
  12. namespace Tutorial.Chapter02 {
  13.     /// Simple changing graphics state example.
  14.     [WrapToTest]
  15.     public class C02E02_GridLines {
  16.         public const String DEST = "../../results/chapter02/grid_lines.pdf";
  17.  
  18.         ///
  19.         public static void Main(String[] args) {
  20.             FileInfo file = new FileInfo(DEST);
  21.             file.Directory.Create();
  22.             new C02E02_GridLines().CreatePdf(DEST);
  23.         }
  24.  
  25.         ///
  26.         public virtual void CreatePdf(String dest) {
  27.             //Initialize PDF document
  28.             PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  29.             PageSize ps = PageSize.A4.Rotate();
  30.             PdfPage page = pdf.AddNewPage(ps);
  31.             PdfCanvas canvas = new PdfCanvas(page);
  32.             //Replace the origin of the coordinate system to the center of the page
  33.             canvas.ConcatMatrix(1, 0, 0, 1, ps.GetWidth() / 2, ps.GetHeight() / 2);
  34.             Color grayColor = new DeviceCmyk(0f, 0f, 0f, 0.875f);
  35.             Color greenColor = new DeviceCmyk(1f, 0f, 1f, 0.176f);
  36.             Color blueColor = new DeviceCmyk(1f, 0.156f, 0f, 0.118f);
  37.             canvas.SetLineWidth(0.5f).SetStrokeColor(blueColor);
  38.             //Draw horizontal grid lines
  39.             for (int i = -((int)ps.GetHeight() / 2 - 57); i < ((int)ps.GetHeight() / 2 - 56); i += 40) {
  40.                 canvas.MoveTo(-(ps.GetWidth() / 2 - 15), i).LineTo(ps.GetWidth() / 2 - 15, i);
  41.             }
  42.             //Draw vertical grid lines
  43.             for (int j = -((int)ps.GetWidth() / 2 - 61); j < ((int)ps.GetWidth() / 2 - 60); j += 40) {
  44.                 canvas.MoveTo(j, -(ps.GetHeight() / 2 - 15)).LineTo(j, ps.GetHeight() / 2 - 15);
  45.             }
  46.             canvas.Stroke();
  47.             //Draw axes
  48.             canvas.SetLineWidth(3).SetStrokeColor(grayColor);
  49.             C02E01_Axes.DrawAxes(canvas, ps);
  50.             //Draw plot
  51.             canvas.SetLineWidth(2).SetStrokeColor(greenColor).SetLineDash(10, 10, 8).MoveTo(-(ps.GetWidth() / 2 - 15),
  52.                 -(ps.GetHeight() / 2 - 15)).LineTo(ps.GetWidth() / 2 - 15, ps.GetHeight() / 2 - 15).Stroke();
  53.             //Close document
  54.             pdf.Close();
  55.         }
  56.     }
  57. }
C02E03_StarWars.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using iText.IO.Font.Constants;
  9. using iText.Kernel.Font;
  10. using iText.Kernel.Geom;
  11. using iText.Kernel.Pdf;
  12. using iText.Kernel.Pdf.Canvas;
  13. using iText.Test.Attributes;
  14.  
  15. namespace Tutorial.Chapter02 {
  16.     /// Simple drawing text example.
  17.     [WrapToTest]
  18.     public class C02E03_StarWars {
  19.         public const String DEST = "../../results/chapter02/star_wars.pdf";
  20.  
  21.         ///
  22.         public static void Main(String[] args) {
  23.             FileInfo file = new FileInfo(DEST);
  24.             file.Directory.Create();
  25.             new C02E03_StarWars().CreatePdf(DEST);
  26.         }
  27.  
  28.         ///
  29.         public virtual void CreatePdf(String dest) {
  30.             //Initialize PDF document
  31.             PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  32.             //Add new page
  33.             PageSize ps = PageSize.A4;
  34.             PdfPage page = pdf.AddNewPage(ps);
  35.             PdfCanvas canvas = new PdfCanvas(page);
  36.             IList text = new List();
  37.             text.Add("         Episode V         ");
  38.             text.Add("  THE EMPIRE STRIKES BACK  ");
  39.             text.Add("It is a dark time for the");
  40.             text.Add("Rebellion. Although the Death");
  41.             text.Add("Star has been destroyed,");
  42.             text.Add("Imperial troops have driven the");
  43.             text.Add("Rebel forces from their hidden");
  44.             text.Add("base and pursued them across");
  45.             text.Add("the galaxy.");
  46.             text.Add("Evading the dreaded Imperial");
  47.             text.Add("Starfleet, a group of freedom");
  48.             text.Add("fighters led by Luke Skywalker");
  49.             text.Add("has established a new secret");
  50.             text.Add("base on the remote ice world");
  51.             text.Add("of Hoth...");
  52.             //Replace the origin of the coordinate system to the top left corner
  53.             canvas.ConcatMatrix(1, 0, 0, 1, 0, ps.GetHeight());
  54.             canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD), 14).SetLeading(14
  55.                  * 1.2f).MoveText(70, -40);
  56.             foreach (String s in text) {
  57.                 //Add text and move to the next line
  58.                 canvas.NewlineShowText(s);
  59.             }
  60.             canvas.EndText();
  61.             //Close document
  62.             pdf.Close();
  63.         }
  64.     }
  65. }
C02E04_StarWarsCrawl.cs
  1. /*
  2. * This example is part of the iText 7 tutorial.
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using iText.IO.Font.Constants;
  9. using iText.Kernel.Colors;
  10. using iText.Kernel.Font;
  11. using iText.Kernel.Geom;
  12. using iText.Kernel.Pdf;
  13. using iText.Kernel.Pdf.Canvas;
  14. using iText.Test.Attributes;
  15.  
  16. namespace Tutorial.Chapter02 {
  17.     /// Simple changing text state example.
  18.     [WrapToTest]
  19.     public class C02E04_StarWarsCrawl {
  20.         public const String DEST = "../../results/chapter02/star_wars_crawl.pdf";
  21.  
  22.         ///
  23.         public static void Main(String[] args) {
  24.             FileInfo file = new FileInfo(DEST);
  25.             file.Directory.Create();
  26.             new C02E04_StarWarsCrawl().CreatePdf(DEST);
  27.         }
  28.  
  29.         ///
  30.         public virtual void CreatePdf(String dest) {
  31.             IList text = new List();
  32.             text.Add("            Episode V      ");
  33.             text.Add("    THE EMPIRE STRIKES BACK  ");
  34.             text.Add("It is a dark time for the");
  35.             text.Add("Rebellion. Although the Death");
  36.             text.Add("Star has been destroyed,");
  37.             text.Add("Imperial troops have driven the");
  38.             text.Add("Rebel forces from their hidden");
  39.             text.Add("base and pursued them across");
  40.             text.Add("the galaxy.");
  41.             text.Add("Evading the dreaded Imperial");
  42.             text.Add("Starfleet, a group of freedom");
  43.             text.Add("fighters led by Luke Skywalker");
  44.             text.Add("has established a new secret");
  45.             text.Add("base on the remote ice world");
  46.             text.Add("of Hoth...");
  47.             int maxStringWidth = 0;
  48.             foreach (String fragment in text) {
  49.                 if (fragment.Length > maxStringWidth) {
  50.                     maxStringWidth = fragment.Length;
  51.                 }
  52.             }
  53.             //Initialize PDF document
  54.             PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  55.             //Add new page
  56.             PageSize ps = PageSize.A4;
  57.             PdfPage page = pdf.AddNewPage(ps);
  58.             PdfCanvas canvas = new PdfCanvas(page);
  59.             //Set black background
  60.             canvas.Rectangle(0, 0, ps.GetWidth(), ps.GetHeight()).SetColor(ColorConstants.BLACK, true).Fill();
  61.             //Replace the origin of the coordinate system to the top left corner
  62.             canvas.ConcatMatrix(1, 0, 0, 1, 0, ps.GetHeight());
  63.             Color yellowColor = new DeviceCmyk(0f, 0.0537f, 0.769f, 0.051f);
  64.             float lineHeight = 5;
  65.             float yOffset = -40;
  66.             canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD), 1).SetColor(yellowColor
  67.                 , true);
  68.             for (int j = 0; j < text.Count; j++) {
  69.                 String line = text[j];
  70.                 float xOffset = ps.GetWidth() / 2 - 45 - 8 * j;
  71.                 float fontSizeCoeff = 6 + j;
  72.                 float lineSpacing = (lineHeight + j) * j / 1.5f;
  73.                 int stringWidth = line.Length;
  74.                 for (int i = 0; i < stringWidth; i++) {
  75.                     float angle = (maxStringWidth / 2 - i) / 2f;
  76.                     float charXOffset = (4 + (float)j / 2) * i;
  77.                     canvas.SetTextMatrix(fontSizeCoeff, 0, angle, fontSizeCoeff / 1.5f, xOffset + charXOffset, yOffset - lineSpacing
  78.                         ).ShowText(line[i].ToString());
  79.                 }
  80.             }
  81.             canvas.EndText();
  82.             //Close document
  83.             pdf.Close();
  84.         }
  85.     }
  86. }
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