【问题标题】:Hexapdf: trying to draw white box, but its not appearingHexapdf:试图绘制白框,但它没有出现
【发布时间】:2026-01-11 12:50:02
【问题描述】:

当我运行我的代码时,我的白框没有出现。我需要一个白框来覆盖现有的图像、文本,这样我就可以添加新的文本。如果我将颜色更改为 background_color: [255,255,180],则该框是透明的黄色。但是,我需要一个不透明的白色。

require 'hexapdf'
require 'pry'

doc = HexaPDF::Document.open('template.pdf')
pages = doc.pages

box = HexaPDF::Layout::Box.create(
  width: 500, height: 500, content_box: true,
  background_color: [255,255,255]
  )

pages.each do |p|
  canvas = p.canvas(type: :underlay)
  box.draw(canvas, 20, 100)
end

doc.write("template_with_white_box.pdf")

【问题讨论】:

    标签: ruby pdf rubygems prawn


    【解决方案1】:

    您需要使用canvas = p.canvas(type: :overlay) 才能工作,因为底层画布绘制在现有页面下方,而覆盖画布绘制在现有页面上方。

    【讨论】: