首页 > 解决方案 > Hexapdf:试图绘制白框,但它没有出现

问题描述

当我运行我的代码时,我的白框没有出现。我需要一个白框来覆盖现有的图像、文本,这样我就可以添加新的文本。如果我将颜色更改为 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")

标签: rubypdfrubygemsprawn

解决方案


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


推荐阅读