Class PageBuilder

java.lang.Object
cloud.opencode.base.pdf.document.PageBuilder

public final class PageBuilder extends Object
PDF Page Builder PDF 页面构建器

Fluent API for adding content to a page.

用于向页面添加内容的流畅 API。

Features | 主要功能:

  • Add text and paragraphs - 添加文本和段落
  • Add images - 添加图像
  • Add tables - 添加表格
  • Draw graphics - 绘制图形

Usage Examples | 使用示例:

documentBuilder.addPage()
    .text("Title", 100, 750)
    .text("Content", 100, 700, PdfFont.helvetica(), 12)
    .line(50, 720, 550, 720)
    .image(Path.of("logo.png"), 50, 50, 100, 50)
    .endPage();

Security | 安全性:

  • Thread-safe: No — not designed for concurrent use - 线程安全: 否 — 非并发设计
  • Null-safe: Yes — parameters are validated - 空值安全: 是 — 参数已验证

Performance | 性能特性:

  • Time complexity: O(1) per element addition; O(e) overall where e is the number of elements added - 时间复杂度: 每次元素添加 O(1);整体为 O(e),e 为添加的元素数
  • Space complexity: O(e) - element list grows proportionally to the number of content items added - 空间复杂度: O(e) - 元素列表与添加的内容项数成正比增长
Since:
JDK 25, opencode-base-pdf V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • text

      public PageBuilder text(String text, float x, float y)
      Adds text at position. 在指定位置添加文本。
      Parameters:
      text - text content | 文本内容
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      Returns:
      this builder | 当前构建器
    • text

      public PageBuilder text(String text, float x, float y, PdfFont font, float fontSize)
      Adds styled text. 添加带样式的文本。
      Parameters:
      text - text content | 文本内容
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      font - font to use | 使用的字体
      fontSize - font size | 字体大小
      Returns:
      this builder | 当前构建器
    • text

      public PageBuilder text(PdfText pdfText)
      Adds a PdfText element. 添加 PdfText 元素。
      Parameters:
      pdfText - text element | 文本元素
      Returns:
      this builder | 当前构建器
    • paragraph

      public PageBuilder paragraph(PdfParagraph paragraph)
      Adds a paragraph. 添加段落。
      Parameters:
      paragraph - paragraph element | 段落元素
      Returns:
      this builder | 当前构建器
    • image

      public PageBuilder image(Path imagePath, float x, float y)
      Adds an image from file. 从文件添加图像。
      Parameters:
      imagePath - image file path | 图像文件路径
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      Returns:
      this builder | 当前构建器
    • image

      public PageBuilder image(Path imagePath, float x, float y, float width, float height)
      Adds an image with size. 添加指定大小的图像。
      Parameters:
      imagePath - image file path | 图像文件路径
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      width - image width | 图像宽度
      height - image height | 图像高度
      Returns:
      this builder | 当前构建器
    • image

      public PageBuilder image(PdfImage pdfImage)
      Adds a PdfImage element. 添加 PdfImage 元素。
      Parameters:
      pdfImage - image element | 图像元素
      Returns:
      this builder | 当前构建器
    • table

      public PageBuilder table(PdfTable table)
      Adds a table. 添加表格。
      Parameters:
      table - table element | 表格元素
      Returns:
      this builder | 当前构建器
    • tableBuilder

      public PdfTable.Builder tableBuilder(int columns)
      Creates table builder. 创建表格构建器。
      Parameters:
      columns - number of columns | 列数
      Returns:
      table builder | 表格构建器
    • line

      public PageBuilder line(float x1, float y1, float x2, float y2)
      Draws a line. 绘制线条。
      Parameters:
      x1 - start x | 起点 x
      y1 - start y | 起点 y
      x2 - end x | 终点 x
      y2 - end y | 终点 y
      Returns:
      this builder | 当前构建器
    • line

      public PageBuilder line(PdfLine line)
      Draws a line with style. 绘制带样式的线条。
      Parameters:
      line - line element | 线条元素
      Returns:
      this builder | 当前构建器
    • rectangle

      public PageBuilder rectangle(float x, float y, float width, float height)
      Draws a rectangle. 绘制矩形。
      Parameters:
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      width - rectangle width | 矩形宽度
      height - rectangle height | 矩形高度
      Returns:
      this builder | 当前构建器
    • filledRectangle

      public PageBuilder filledRectangle(float x, float y, float width, float height, PdfColor color)
      Draws a filled rectangle. 绘制填充矩形。
      Parameters:
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      width - rectangle width | 矩形宽度
      height - rectangle height | 矩形高度
      color - fill color | 填充颜色
      Returns:
      this builder | 当前构建器
    • rectangle

      public PageBuilder rectangle(PdfRectangle rectangle)
      Draws a rectangle element. 绘制矩形元素。
      Parameters:
      rectangle - rectangle element | 矩形元素
      Returns:
      this builder | 当前构建器
    • ellipse

      public PageBuilder ellipse(PdfEllipse ellipse)
      Draws an ellipse. 绘制椭圆。
      Parameters:
      ellipse - ellipse element | 椭圆元素
      Returns:
      this builder | 当前构建器
    • endPage

      public DocumentBuilder endPage()
      Finishes current page and returns to document builder. 完成当前页面并返回文档构建器。
      Returns:
      document builder | 文档构建器
    • nextPage

      public PageBuilder nextPage()
      Adds another page. 添加另一个页面。
      Returns:
      new page builder | 新页面构建器
    • nextPage

      public PageBuilder nextPage(PageSize pageSize)
      Adds another page with specific size. 添加指定大小的另一个页面。
      Parameters:
      pageSize - page size | 页面大小
      Returns:
      new page builder | 新页面构建器
    • getPageSize

      public PageSize getPageSize()
    • getOrientation

      public Orientation getOrientation()
    • getElements

      public List<PdfElement> getElements()
    • getWidth

      public float getWidth()
      Gets page width in points. 获取页面宽度(点)。
      Returns:
      page width | 页面宽度
    • getHeight

      public float getHeight()
      Gets page height in points. 获取页面高度(点)。
      Returns:
      page height | 页面高度