itext7学习笔记——第2章实践&example

x33g5p2x  于2021-12-28 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(544)

本章的例子,请参考我翻译的博文:itext7学习笔记——第2章,里面有详细的解释,有什么不懂得也可以评论或者私信我!

例子1:简单的坐标系

简单地画一个x和y轴坐标系,代码如下:

  1. import com.itextpdf.kernel.geom.PageSize;
  2. import com.itextpdf.kernel.pdf.PdfDocument;
  3. import com.itextpdf.kernel.pdf.PdfPage;
  4. import com.itextpdf.kernel.pdf.PdfWriter;
  5. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  6. import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
  7. import java.io.File;
  8. import java.io.IOException;
  9. public class C02E01_Axes {
  10. public static final String DEST = "results/chapter02/axes.pdf";
  11. public static void main(String args[]) throws IOException {
  12. File file = new File(DEST);
  13. file.getParentFile().mkdirs();
  14. new C02E01_Axes().createPdf(DEST);
  15. }
  16. public void createPdf(String dest) throws IOException {
  17. //Initialize PDF document
  18. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  19. PageSize ps = PageSize.A4.rotate();
  20. PdfPage page = pdf.addNewPage(ps);
  21. PdfCanvas canvas = new PdfCanvas(page);
  22. //Replace the origin of the coordinate system to the center of the page
  23. canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2);
  24. C02E01_Axes.drawAxes(canvas, ps);
  25. //Close document
  26. pdf.close();
  27. }
  28. public static void drawAxes(PdfCanvas canvas, PageSize ps) {
  29. //Draw X axis
  30. canvas.moveTo(-(ps.getWidth() / 2 - 15), 0)
  31. .lineTo(ps.getWidth() / 2 - 15, 0)
  32. .stroke();
  33. //Draw X axis arrow
  34. canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
  35. .moveTo(ps.getWidth() / 2 - 25, -10)
  36. .lineTo(ps.getWidth() / 2 - 15, 0)
  37. .lineTo(ps.getWidth() / 2 - 25, 10).stroke()
  38. .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER);
  39. //Draw Y axis
  40. canvas.moveTo(0, -(ps.getHeight() / 2 - 15))
  41. .lineTo(0, ps.getHeight() / 2 - 15)
  42. .stroke();
  43. //Draw Y axis arrow
  44. canvas.saveState()
  45. .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
  46. .moveTo(-10, ps.getHeight() / 2 - 25)
  47. .lineTo(0, ps.getHeight() / 2 - 15)
  48. .lineTo(10, ps.getHeight() / 2 - 25).stroke()
  49. .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. }

例子2:坐标系和网格线

在坐标系的基础上,分割坐标系,画出网格线,代码如下:

  1. import com.itextpdf.kernel.color.Color;
  2. import com.itextpdf.kernel.color.DeviceCmyk;
  3. import com.itextpdf.kernel.geom.PageSize;
  4. import com.itextpdf.kernel.pdf.PdfDocument;
  5. import com.itextpdf.kernel.pdf.PdfPage;
  6. import com.itextpdf.kernel.pdf.PdfWriter;
  7. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  8. import java.io.File;
  9. import java.io.IOException;
  10. public class C02E02_GridLines {
  11. public static final String DEST = "results/chapter02/grid_lines.pdf";
  12. public static void main(String args[]) throws IOException {
  13. File file = new File(DEST);
  14. file.getParentFile().mkdirs();
  15. new C02E02_GridLines().createPdf(DEST);
  16. }
  17. public void createPdf(String dest) throws IOException {
  18. //Initialize PDF document
  19. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  20. PageSize ps = PageSize.A4.rotate();
  21. PdfPage page = pdf.addNewPage(ps);
  22. PdfCanvas canvas = new PdfCanvas(page);
  23. //Replace the origin of the coordinate system to the center of the page
  24. canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2);
  25. Color grayColor = new DeviceCmyk(0.f, 0.f, 0.f, 0.875f);
  26. Color greenColor = new DeviceCmyk(1.f, 0.f, 1.f, 0.176f);
  27. Color blueColor = new DeviceCmyk(1.f, 0.156f, 0.f, 0.118f);
  28. canvas.setLineWidth(0.5f).setStrokeColor(blueColor);
  29. //Draw horizontal grid lines
  30. for (int i = -((int) ps.getHeight() / 2 - 57); i < ((int) ps.getHeight() / 2 - 56); i += 40) {
  31. canvas.moveTo(-(ps.getWidth() / 2 - 15), i)
  32. .lineTo(ps.getWidth() / 2 - 15, i);
  33. }
  34. //Draw vertical grid lines
  35. for (int j = -((int) ps.getWidth() / 2 - 61); j < ((int) ps.getWidth() / 2 - 60); j += 40) {
  36. canvas.moveTo(j, -(ps.getHeight() / 2 - 15))
  37. .lineTo(j, ps.getHeight() / 2 - 15);
  38. }
  39. canvas.stroke();
  40. //Draw axes
  41. canvas.setLineWidth(3).setStrokeColor(grayColor);
  42. C02E01_Axes.drawAxes(canvas, ps);
  43. //Draw plot
  44. canvas.setLineWidth(2).setStrokeColor(greenColor)
  45. .setLineDash(10, 10, 8)
  46. .moveTo(-(ps.getWidth() / 2 - 15), -(ps.getHeight() / 2 - 15))
  47. .lineTo(ps.getWidth() / 2 - 15, ps.getHeight() / 2 - 15).stroke();
  48. //Close document
  49. pdf.close();
  50. }
  51. }

例子3:成段的文字和字体

这里我们简单显示星球大战5帝国反击战的开头的文字,代码如下:

  1. import com.itextpdf.io.font.FontConstants;
  2. import com.itextpdf.kernel.font.PdfFontFactory;
  3. import com.itextpdf.kernel.geom.PageSize;
  4. import com.itextpdf.kernel.pdf.PdfDocument;
  5. import com.itextpdf.kernel.pdf.PdfPage;
  6. import com.itextpdf.kernel.pdf.PdfWriter;
  7. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class C02E03_StarWars {
  13. public static final String DEST = "results/chapter02/star_wars.pdf";
  14. public static void main(String args[]) throws IOException {
  15. File file = new File(DEST);
  16. file.getParentFile().mkdirs();
  17. new C02E03_StarWars().createPdf(DEST);
  18. }
  19. public void createPdf(String dest) throws IOException {
  20. //Initialize PDF document
  21. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  22. //Add new page
  23. PageSize ps = PageSize.A4;
  24. PdfPage page = pdf.addNewPage(ps);
  25. PdfCanvas canvas = new PdfCanvas(page);
  26. List<String> text = new ArrayList();
  27. text.add(" Episode V ");
  28. text.add(" THE EMPIRE STRIKES BACK ");
  29. text.add("It is a dark time for the");
  30. text.add("Rebellion. Although the Death");
  31. text.add("Star has been destroyed,");
  32. text.add("Imperial troops have driven the");
  33. text.add("Rebel forces from their hidden");
  34. text.add("base and pursued them across");
  35. text.add("the galaxy.");
  36. text.add("Evading the dreaded Imperial");
  37. text.add("Starfleet, a group of freedom");
  38. text.add("fighters led by Luke Skywalker");
  39. text.add("has established a new secret");
  40. text.add("base on the remote ice world");
  41. text.add("of Hoth...");
  42. //Replace the origin of the coordinate system to the top left corner
  43. canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight());
  44. canvas.beginText()
  45. .setFontAndSize(PdfFontFactory.createFont(FontConstants.COURIER_BOLD), 14)
  46. .setLeading(14 * 1.2f)
  47. .moveText(70, -40);
  48. for (String s : text) {
  49. //Add text and move to the next line
  50. canvas.newlineShowText(s);
  51. }
  52. canvas.endText();
  53. //Close document
  54. pdf.close();
  55. }
  56. }

例子4:炫酷的文字效果

我们在第三个例子的基础上,添加炫酷的效果,代码如下:

  1. import com.itextpdf.io.font.FontConstants;
  2. import com.itextpdf.kernel.color.Color;
  3. import com.itextpdf.kernel.color.DeviceCmyk;
  4. import com.itextpdf.kernel.font.PdfFontFactory;
  5. import com.itextpdf.kernel.geom.PageSize;
  6. import com.itextpdf.kernel.pdf.PdfDocument;
  7. import com.itextpdf.kernel.pdf.PdfPage;
  8. import com.itextpdf.kernel.pdf.PdfWriter;
  9. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class C02E04_StarWarsCrawl {
  15. public static final String DEST = "results/chapter02/star_wars_crawl.pdf";
  16. public static void main(String args[]) throws IOException {
  17. File file = new File(DEST);
  18. file.getParentFile().mkdirs();
  19. new C02E04_StarWarsCrawl().createPdf(DEST);
  20. }
  21. public void createPdf(String dest) throws IOException {
  22. List<String> text = new ArrayList();
  23. text.add(" Episode V ");
  24. text.add(" THE EMPIRE STRIKES BACK ");
  25. text.add("It is a dark time for the");
  26. text.add("Rebellion. Although the Death");
  27. text.add("Star has been destroyed,");
  28. text.add("Imperial troops have driven the");
  29. text.add("Rebel forces from their hidden");
  30. text.add("base and pursued them across");
  31. text.add("the galaxy.");
  32. text.add("Evading the dreaded Imperial");
  33. text.add("Starfleet, a group of freedom");
  34. text.add("fighters led by Luke Skywalker");
  35. text.add("has established a new secret");
  36. text.add("base on the remote ice world");
  37. text.add("of Hoth...");
  38. int maxStringWidth = 0;
  39. for (String fragment : text) {
  40. if (fragment.length() > maxStringWidth) maxStringWidth = fragment.length();
  41. }
  42. //Initialize PDF document
  43. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44. //Add new page
  45. PageSize ps = PageSize.A4;
  46. PdfPage page = pdf.addNewPage(ps);
  47. PdfCanvas canvas = new PdfCanvas(page);
  48. //Set black background
  49. canvas.rectangle(0, 0, ps.getWidth(), ps.getHeight())
  50. .setColor(Color.BLACK, true)
  51. .fill();
  52. //Replace the origin of the coordinate system to the top left corner
  53. canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight());
  54. Color yellowColor = new DeviceCmyk(0.f, 0.0537f, 0.769f, 0.051f);
  55. float lineHeight = 5;
  56. float yOffset = -40;
  57. canvas.beginText()
  58. .setFontAndSize(PdfFontFactory.createFont(FontConstants.COURIER_BOLD), 1)
  59. .setColor(yellowColor, true);
  60. for (int j = 0; j < text.size(); j++) {
  61. String line = text.get(j);
  62. float xOffset = ps.getWidth() / 2 - 45 - 8 * j;
  63. float fontSizeCoeff = 6 + j;
  64. float lineSpacing = (lineHeight + j) * j / 1.5f;
  65. int stringWidth = line.length();
  66. for (int i = 0; i < stringWidth; i++) {
  67. float angle = (maxStringWidth / 2 - i) / 2f;
  68. float charXOffset = (4 + (float) j / 2) * i;
  69. canvas.setTextMatrix(fontSizeCoeff, 0,
  70. angle, fontSizeCoeff / 1.5f,
  71. xOffset + charXOffset, yOffset - lineSpacing)
  72. .showText(String.valueOf(line.charAt(i)));
  73. }
  74. }
  75. canvas.endText();
  76. //Close document
  77. pdf.close();
  78. }
  79. }

Example代码下载

本章代码可在如下地址下载(IDEA工程):iText7——第二章源代码工程

相关文章