• iText实战--根据绝对位置添加内容


    3.1 direct content 概念简介

    pdf内容的4个层级

    层级1:在text和graphics底下,PdfWriter.getDirectContentUnder()

    层级2:graphics层,Chunk, Images背景,PdfPCell的边界等

    层级3:text层,Chunks, Phrases, Paragraphs 内容等

    层级4:在text和graphics顶上,PdfWriter.getDirectContent()

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Element;
    6. import com.itextpdf.text.Font;
    7. import com.itextpdf.text.Image;
    8. import com.itextpdf.text.PageSize;
    9. import com.itextpdf.text.Paragraph;
    10. import com.itextpdf.text.Font.FontFamily;
    11. import com.itextpdf.text.pdf.BaseFont;
    12. import com.itextpdf.text.pdf.PdfContentByte;
    13. import com.itextpdf.text.pdf.PdfWriter;
    14. public class FestivalOpening {
    15. /** The resulting PDF. */
    16. public static final String RESULT
    17. = "D:/data/iText/inAction/chapter03/festival_opening.pdf";
    18. /** The movie poster. */
    19. public static final String RESOURCE = "E:/study/PDF/SourceCodeiText/itext-book/book/resources/img/loa.jpg";
    20. /**
    21. * Main method.
    22. * @param args no arguments needed
    23. * @throws DocumentException
    24. * @throws IOException
    25. */
    26. public static void main(String[] args)
    27. throws IOException, DocumentException {
    28. // step 1
    29. Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
    30. // step 2
    31. PdfWriter writer
    32. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    33. // step 3
    34. document.open();
    35. // step 4
    36. // Create and add a Paragraph
    37. Paragraph p
    38. = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
    39. p.setAlignment(Element.ALIGN_CENTER);
    40. document.add(p);
    41. // Create and add an Image
    42. Image img = Image.getInstance(RESOURCE);
    43. img.setAbsolutePosition(
    44. (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
    45. (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    46. document.add(img);
    47. // Now we go to the next page
    48. document.newPage();
    49. document.add(p);
    50. document.add(img);
    51. // Add text on top of the image
    52. PdfContentByte over = writer.getDirectContent();
    53. over.saveState();
    54. float sinus = (float)Math.sin(Math.PI / 60);
    55. float cosinus = (float)Math.cos(Math.PI / 60);
    56. BaseFont bf = BaseFont.createFont();
    57. over.beginText();
    58. over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
    59. over.setLineWidth(1.5f);
    60. over.setRGBColorStroke(0xFF, 0x00, 0x00);
    61. over.setRGBColorFill(0xFF, 0xFF, 0xFF);
    62. over.setFontAndSize(bf, 36);
    63. over.setTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324);
    64. over.showText("SOLD OUT");
    65. over.endText();
    66. over.restoreState();
    67. // Add a rectangle under the image
    68. PdfContentByte under = writer.getDirectContentUnder();
    69. under.saveState();
    70. under.setRGBColorFill(0xFF, 0xD7, 0x00);
    71. under.rectangle(5, 5,
    72. PageSize.POSTCARD.getWidth() - 10, PageSize.POSTCARD.getHeight() - 10);
    73. under.fill();
    74. under.restoreState();
    75. // step 5
    76. document.close();
    77. }
    78. }

    Graphics 状态

    1、fill()填充四方形,并根据setRGBColorFill()填充,默认无边框

    2、fillStroke()填充四方形,并根据setLineWidth()和默认black画边框

    3、setRGBColorStroke() 设置边框颜色

    4、current transformation matrix(CTM)当前转换矩阵

    5、stroke() 仅画边框

    6、saveState/rrestoreState 对应入栈、出栈

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Rectangle;
    6. import com.itextpdf.text.pdf.PdfContentByte;
    7. import com.itextpdf.text.pdf.PdfWriter;
    8. public class GraphicsStateStack {
    9. /** The resulting PDF. */
    10. public static final String RESULT
    11. = "D:/data/iText/inAction/chapter03/graphics_state.pdf";
    12. /**
    13. * Main method.
    14. *
    15. * @param args no arguments needed
    16. * @throws DocumentException
    17. * @throws IOException
    18. */
    19. public static void main(String[] args)
    20. throws IOException, DocumentException {
    21. // step 1
    22. Document document = new Document(new Rectangle(200, 120));
    23. // step 2
    24. PdfWriter writer
    25. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    26. // step 3
    27. document.open();
    28. // step 4
    29. PdfContentByte canvas = writer.getDirectContent();
    30. // state 1:
    31. canvas.setRGBColorFill(0xFF, 0x45, 0x00);
    32. // fill a rectangle in state 1
    33. canvas.rectangle(10, 10, 60, 60);
    34. canvas.fill();
    35. canvas.saveState();
    36. // state 2;
    37. canvas.setLineWidth(3);
    38. canvas.setRGBColorFill(0x8B, 0x00, 0x00);
    39. // fill and stroke a rectangle in state 2
    40. canvas.rectangle(40, 20, 60, 60);
    41. canvas.fillStroke();
    42. canvas.saveState();
    43. // state 3:
    44. canvas.concatCTM(1, 0, 0.1f, 1, 0, 0);
    45. canvas.setRGBColorStroke(0xFF, 0x45, 0x00);
    46. canvas.setRGBColorFill(0xFF, 0xD7, 0x00);
    47. // fill and stroke a rectangle in state 3
    48. canvas.rectangle(70, 30, 60, 60);
    49. canvas.fillStroke();
    50. canvas.restoreState();
    51. // stroke a rectangle in state 2
    52. canvas.rectangle(100, 40, 60, 60);
    53. canvas.stroke();
    54. canvas.restoreState();
    55. // fill and stroke a rectangle in state 1
    56. canvas.rectangle(130, 50, 60, 60);
    57. canvas.fillStroke();
    58. // step 5
    59. document.close();
    60. }
    61. }

    Text 状态

    1、showText(),设置显示的文本

    2、setTextRenderingMode()设置边框模式,setLineWidth()设置边框宽度,默认无边框

    3、setFontAndSize() 设置字体和大小

    4、setTextMatrix() 设置字体矩阵

    5、setRGBColorStoke() 设置边框颜色

    6、setRGBColorFill() 设置填充颜色

    3.2 根据绝对定位添加Text

    PdfContentByte.showTextAligned()

    测量字符串

    1. Chunk c;
    2. String foobar = "Foobar Film Festival";
    3. // Measuring a String in Helvetica
    4. Font helvetica = new Font(FontFamily.HELVETICA, 12);
    5. BaseFont bf_helv = helvetica.getCalculatedBaseFont(false);
    6. float width_helv = bf_helv.getWidthPoint(foobar, 12);
    7. c = new Chunk(foobar + ": " + width_helv, helvetica);
    8. document.add(new Paragraph(c));
    9. document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
    10. // Measuring a String in Times
    11. BaseFont bf_times = BaseFont.createFont(
    12. "c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
    13. Font times = new Font(bf_times, 12);
    14. float width_times = bf_times.getWidthPoint(foobar, 12);
    15. c = new Chunk(foobar + ": " + width_times, times);
    16. document.add(new Paragraph(c));
    17. document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
    18. document.add(Chunk.NEWLINE);

    字符串上行空间和下行空间

    注意:字体大小不是某个字符的高度,而是一行的垂直空间

    1. // Ascent and descent of the String
    2. document.add(new Paragraph("Ascent Helvetica: "
    3. + bf_helv.getAscentPoint(foobar, 12)));
    4. document.add(new Paragraph("Ascent Times: "
    5. + bf_times.getAscentPoint(foobar, 12)));
    6. document.add(new Paragraph("Descent Helvetica: "
    7. + bf_helv.getDescentPoint(foobar, 12)));
    8. document.add(new Paragraph("Descent Times: "
    9. + bf_times.getDescentPoint(foobar, 12)));

    字符串定位

    1. PdfContentByte canvas = writer.getDirectContent();
    2. // Adding text with PdfContentByte.showTextAligned()
    3. canvas.beginText();
    4. canvas.setFontAndSize(bf_helv, 12);
    5. canvas.showTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
    6. canvas.showTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
    7. canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
    8. canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
    9. canvas.showTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
    10. canvas.endText();

    字距调整(KERNING

    1. // Kerned text
    2. width_helv = bf_helv.getWidthPointKerned(foobar, 12);
    3. c = new Chunk(foobar + ": " + width_helv, helvetica);
    4. document.add(new Paragraph(c));

    ColumnText.showTextAligned()

    1. // Adding text with ColumnText.showTextAligned()
    2. Phrase phrase = new Phrase(foobar, times);
    3. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
    4. ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
    5. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
    6. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
    7. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);

    短句(Phrase)定位

    一个短句(Phrase)能够包含一系列的块(Chunk)。添加短句(Phrase)到绝对定位,可以方便地选择字体、字号和颜色,iText会自动计算短句(Phrase)内每个块(Chunk)的间距。

    块:缩放、倾斜、渲染模式(Chunks: Scaling、Skewing、Rendering Mode)

    1、setScaling(float scale) 设置缩放比例

    2、setSkew(float arg1, float arg2) 设置倾斜,arg1:基线的角度,arg2:字符对基线的角度

    3、setTextRenderMode() 设置文本渲染模式

    PdfContentByte.TEXT_RENDER_MODE_FILL,默认填充字符形状,无边框
    PdfContentByte.TEXT_RENDER_MODE_STROKE,不填充字符,仅边框
    PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE,填充字符,带边框
    PdfContentByte.TEXT_RENDER_MODE_INVISIBLE,文本不可见
    1. // Chunk attributes
    2. c = new Chunk(foobar, times);
    3. c.setHorizontalScaling(0.5f);
    4. phrase = new Phrase(c);
    5. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
    6. c = new Chunk(foobar, times);
    7. c.setSkew(15, 15);
    8. phrase = new Phrase(c);
    9. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
    10. c = new Chunk(foobar, times);
    11. c.setSkew(0, 25);
    12. phrase = new Phrase(c);
    13. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
    14. c = new Chunk(foobar, times);
    15. c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
    16. phrase = new Phrase(c);
    17. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
    18. c = new Chunk(foobar, times);
    19. c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
    20. phrase = new Phrase(c);
    21. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);

    完整示例

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Chunk;
    4. import com.itextpdf.text.Document;
    5. import com.itextpdf.text.DocumentException;
    6. import com.itextpdf.text.Element;
    7. import com.itextpdf.text.Font;
    8. import com.itextpdf.text.Paragraph;
    9. import com.itextpdf.text.Phrase;
    10. import com.itextpdf.text.Font.FontFamily;
    11. import com.itextpdf.text.pdf.BaseFont;
    12. import com.itextpdf.text.pdf.ColumnText;
    13. import com.itextpdf.text.pdf.PdfContentByte;
    14. import com.itextpdf.text.pdf.PdfWriter;
    15. import com.itextpdf.text.BaseColor;
    16. public class FoobarFilmFestival {
    17. public static final String RESULT
    18. = "D:/data/iText/inAction/chapter03/foobar_film_festival.pdf";
    19. /**
    20. * Main method.
    21. *
    22. * @param args no arguments needed
    23. * @throws DocumentException
    24. * @throws IOException
    25. */
    26. public static void main(String[] args)
    27. throws IOException, DocumentException {
    28. // step 1
    29. Document document = new Document();
    30. // step 2
    31. PdfWriter writer
    32. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    33. // step 3
    34. document.open();
    35. // step 4
    36. Chunk c;
    37. String foobar = "Foobar Film Festival";
    38. // Measuring a String in Helvetica
    39. Font helvetica = new Font(FontFamily.HELVETICA, 12);
    40. BaseFont bf_helv = helvetica.getCalculatedBaseFont(false);
    41. float width_helv = bf_helv.getWidthPoint(foobar, 12);
    42. c = new Chunk(foobar + ": " + width_helv, helvetica);
    43. document.add(new Paragraph(c));
    44. document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
    45. // Measuring a String in Times
    46. BaseFont bf_times = BaseFont.createFont(
    47. "c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
    48. Font times = new Font(bf_times, 12);
    49. float width_times = bf_times.getWidthPoint(foobar, 12);
    50. c = new Chunk(foobar + ": " + width_times, times);
    51. document.add(new Paragraph(c));
    52. document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
    53. document.add(Chunk.NEWLINE);
    54. // Ascent and descent of the String
    55. document.add(new Paragraph("Ascent Helvetica: "
    56. + bf_helv.getAscentPoint(foobar, 12)));
    57. document.add(new Paragraph("Ascent Times: "
    58. + bf_times.getAscentPoint(foobar, 12)));
    59. document.add(new Paragraph("Descent Helvetica: "
    60. + bf_helv.getDescentPoint(foobar, 12)));
    61. document.add(new Paragraph("Descent Times: "
    62. + bf_times.getDescentPoint(foobar, 12)));
    63. document.add(Chunk.NEWLINE);
    64. // Kerned text
    65. width_helv = bf_helv.getWidthPointKerned(foobar, 12);
    66. c = new Chunk(foobar + ": " + width_helv, helvetica);
    67. document.add(new Paragraph(c));
    68. // Drawing lines to see where the text is added
    69. PdfContentByte canvas = writer.getDirectContent();
    70. canvas.saveState();
    71. canvas.setLineWidth(0.05f);
    72. canvas.moveTo(400, 806);
    73. canvas.lineTo(400, 626);
    74. canvas.moveTo(508.7f, 806);
    75. canvas.lineTo(508.7f, 626);
    76. canvas.moveTo(280, 788);
    77. canvas.lineTo(520, 788);
    78. canvas.moveTo(280, 752);
    79. canvas.lineTo(520, 752);
    80. canvas.moveTo(280, 716);
    81. canvas.lineTo(520, 716);
    82. canvas.moveTo(280, 680);
    83. canvas.lineTo(520, 680);
    84. canvas.moveTo(280, 644);
    85. canvas.lineTo(520, 644);
    86. canvas.stroke();
    87. canvas.restoreState();
    88. // Adding text with PdfContentByte.showTextAligned()
    89. canvas.beginText();
    90. canvas.setFontAndSize(bf_helv, 12);
    91. canvas.showTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
    92. canvas.showTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
    93. canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
    94. canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
    95. canvas.showTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
    96. canvas.endText();
    97. // More lines to see where the text is added
    98. canvas.saveState();
    99. canvas.setLineWidth(0.05f);
    100. canvas.moveTo(200, 590);
    101. canvas.lineTo(200, 410);
    102. canvas.moveTo(400, 590);
    103. canvas.lineTo(400, 410);
    104. canvas.moveTo(80, 572);
    105. canvas.lineTo(520, 572);
    106. canvas.moveTo(80, 536);
    107. canvas.lineTo(520, 536);
    108. canvas.moveTo(80, 500);
    109. canvas.lineTo(520, 500);
    110. canvas.moveTo(80, 464);
    111. canvas.lineTo(520, 464);
    112. canvas.moveTo(80, 428);
    113. canvas.lineTo(520, 428);
    114. canvas.stroke();
    115. canvas.restoreState();
    116. // Adding text with ColumnText.showTextAligned()
    117. Phrase phrase = new Phrase(foobar, times);
    118. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
    119. ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
    120. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
    121. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
    122. ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
    123. // Chunk attributes
    124. c = new Chunk(foobar, times);
    125. c.setHorizontalScaling(0.5f);
    126. phrase = new Phrase(c);
    127. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
    128. c = new Chunk(foobar, times);
    129. c.setSkew(15, 15);
    130. phrase = new Phrase(c);
    131. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
    132. c = new Chunk(foobar, times);
    133. c.setSkew(0, 25);
    134. phrase = new Phrase(c);
    135. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
    136. c = new Chunk(foobar, times);
    137. c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
    138. phrase = new Phrase(c);
    139. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
    140. c = new Chunk(foobar, times);
    141. c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
    142. phrase = new Phrase(c);
    143. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);
    144. // step 5
    145. document.close();
    146. }
    147. }

    3.3 使用ColumnText对象

    在文本模式下使用ColumnText

    在复合模式中使用ColumnText

    3.4 创建可重复使用的内容

    如何在文档重复添加image?PDF图片的字节存储在单独区域,页面包含一个图片的引用,指向external object(XObject)

    Image XObjects

    添加图片到顶层

    顶层的图片遮盖住文本:Foobar Film Festival

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Element;
    6. import com.itextpdf.text.Font;
    7. import com.itextpdf.text.Image;
    8. import com.itextpdf.text.PageSize;
    9. import com.itextpdf.text.Paragraph;
    10. import com.itextpdf.text.Font.FontFamily;
    11. import com.itextpdf.text.pdf.PdfWriter;
    12. public class ImageDirect {
    13. /** The resulting PDF. */
    14. public static final String RESULT
    15. = "D:/data/iText/inAction/chapter03/image_direct.pdf";
    16. /** The movie poster. */
    17. public static final String RESOURCE
    18. = "E:/study/PDF/SourceCodeiText/itext-book/book/resources/img/loa.jpg";
    19. public static void main(String[] args)
    20. throws IOException, DocumentException {
    21. // step 1
    22. Document document
    23. = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
    24. // step 2
    25. PdfWriter writer
    26. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    27. writer.setCompressionLevel(0);
    28. // step 3
    29. document.open();
    30. // step 4
    31. Image img = Image.getInstance(RESOURCE);
    32. img.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
    33. (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    34. writer.getDirectContent().addImage(img);
    35. Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
    36. p.setAlignment(Element.ALIGN_CENTER);
    37. document.add(p);
    38. // step 5
    39. document.close();
    40. }
    41. }

    倾斜图片

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Image;
    6. import com.itextpdf.text.PageSize;
    7. import com.itextpdf.text.pdf.PdfWriter;
    8. public class ImageSkew {
    9. /** The resulting PDF. */
    10. public static final String RESULT = "D:/data/iText/inAction/chapter03/image_skew.pdf";
    11. /** The movie poster. */
    12. public static final String RESOURCE = "E:/study/PDF/SourceCodeiText/itext-book/book/resources/img/loa.jpg";
    13. /**
    14. * Main method.
    15. *
    16. * @param args no arguments needed
    17. * @throws DocumentException
    18. * @throws IOException
    19. */
    20. public static void main(String[] args)
    21. throws IOException, DocumentException {
    22. // step 1
    23. Document document = new Document(PageSize.POSTCARD.rotate());
    24. // step 2
    25. PdfWriter writer = PdfWriter.getInstance(document,
    26. new FileOutputStream(RESULT));
    27. writer.setCompressionLevel(0);
    28. // step 3
    29. document.open();
    30. // step 4
    31. Image img = Image.getInstance(RESOURCE);
    32. // Add the image to the upper layer
    33. writer.getDirectContent().addImage(img,
    34. img.getWidth(), 0, 0.35f * img.getHeight(),
    35. 0.65f * img.getHeight(), 30, 30);
    36. // step 5
    37. document.close();
    38. }
    39. }

    内嵌图片

    PdfWriter.getDirectContent().addImage(img, true)

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Image;
    6. import com.itextpdf.text.PageSize;
    7. import com.itextpdf.text.pdf.PdfWriter;
    8. public class ImageInline {
    9. /** The resulting PDF. */
    10. public static final String RESULT
    11. = "D:/data/iText/inAction/chapter03/image_inline.pdf";
    12. /** The movie poster. */
    13. public static final String RESOURCE
    14. = "E:/study/PDF/SourceCodeiText/itext-book/book/resources/img/loa.jpg";
    15. /**
    16. * Main method.
    17. *
    18. * @param args no arguments needed
    19. * @throws DocumentException
    20. * @throws IOException
    21. */
    22. public static void main(String[] args)
    23. throws IOException, DocumentException {
    24. // step 1
    25. Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
    26. // step 2
    27. PdfWriter writer = PdfWriter.getInstance(document,
    28. new FileOutputStream(RESULT));
    29. writer.setCompressionLevel(0);
    30. // step 3
    31. document.open();
    32. // step 4
    33. Image img = Image.getInstance(RESOURCE);
    34. img.setAbsolutePosition(
    35. (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
    36. (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    37. writer.getDirectContent().addImage(img, true);
    38. // step 5
    39. document.close();
    40. }
    41. }

    PdfTemplate 对象

    PdfTemplate:XObject的别称

    PdfTemplate是一个PDF内容流,它是任何图形对象的序列。PdfTemplate扩展PdfContentByte并继承所有其方法。

    1. PdfContentByte canvas = writer.getDirectContent();
    2. // Create the XObject
    3. PdfTemplate celluloid = canvas.createTemplate(595, 84.2f);
    4. celluloid.rectangle(8, 8, 579, 68);
    5. for (float f = 8.25f; f < 581; f+= 6.5f) {
    6. celluloid.roundRectangle(f, 8.5f, 6, 3, 1.5f);
    7. celluloid.roundRectangle(f, 72.5f, 6, 3, 1.5f);
    8. }
    9. celluloid.setGrayFill(0.1f);
    10. celluloid.eoFill();
    11. // Write the XObject to the OutputStream
    12. writer.releaseTemplate(celluloid);
    13. // Add the XObject 10 times
    14. for (int i = 0; i < 10; i++) {
    15. canvas.addTemplate(celluloid, 0, i * 84.2f);
    16. }
    17. // Go to the next page
    18. document.newPage();
    19. // Add the XObject 10 times
    20. for (int i = 0; i < 10; i++) {
    21. canvas.addTemplate(celluloid, 0, i * 84.2f);
    22. }

  • 相关阅读:
    Java中的Object类
    指派问题——匈牙利法
    openvino系列教程之人脸检测 mobilenetv2
    算法笔记:哈夫曼树、哈夫曼编码
    语言的未来:深度学习在自然语言处理中的革命
    基于mbedtls的AES加密(C/C++)
    petite-vue源码剖析-沙箱模型
    Python统计pdf中英文单词的个数
    Controller层代码组织方式
    java计算机毕业设计家政服务公司管理信息源码+系统+数据库+lw文档
  • 原文地址:https://blog.csdn.net/gqltt/article/details/132915083