lang en ja

How to write page border with Asciidoctor

Published Last Update author
2024/11/23 2024/11/26 Shumpei Tanaka

Summary

I introduce how to write page border with Asciidoctor.

There is a feature: Page Border and Surround header in Word of Microsoft’s Office.

This converter was made for substitution of their.

Demo

Result looks like below.

script results

Usage

There is usage in repository: Shumpei-Tanaka/AsciiDoc-BorderInMargin.

Script

There is latest in repository: Shumpei-Tanaka/AsciiDoc-BorderInMargin .

write_header_border is function to write border surround header. It takes values below from style setting.

  • header margin
  • herder column ratios

write_mergin_border is function to write page border in mergin. It takes margin values from style setting.

class PDFConverterCustomTitlePage < (Asciidoctor::Converter.for 'pdf')
  register_for 'pdf'
  def ink_title_page(doc)
    super
    write_mergin_border 5, 1
    write_header_border 5, 1
  end

  def ink_running_content(*)
    write_mergin_border 5, 1
    write_header_border 5, 1
    super
  end
end

def write_header_border(line_offset:, line_width:)
  _m_t, m_r, m_b, m_l = page_margin
  header_height = theme.header_height
  header_columns = theme.header_columns
  mh_t, mh_r, _mh_b, mh_l = theme.header_margin

  columns = header_columns.delete('^0-9,.').split(',').map(&)

  o_sum = columns.sum
  sum = 0
  ps = []
  columns.pop
  columns.each do |col|
    sum += col
    ps << sum
  end
  ps = ps.map { |col| col / o_sum }

  p_t = -(page_height - (mh_t - line_offset) - m_b)
  p_b = -(page_height - (mh_t + line_offset) - m_b - header_height)
  p_l = -line_offset
  p_r = page_width - (m_r - line_offset) - m_l

  t_width = page_width - mh_r - mh_l

  p_vs = ps.map do |i|
    t_width * i - m_l + mh_l
  end

  move_cursor_to 0
  stroke_horizontal_rule '000000', line_width, p_t, line_offset,
                                   -line_offset
  stroke_horizontal_rule '000000', line_width, p_b, line_offset,
                                   -line_offset
  p_vs.each do |p_v|
    stroke_vertical_line(-p_b, -p_t, p_v)
  end
  stroke_vertical_line(-p_b, -p_t, p_l)
  stroke_vertical_line(-p_b, -p_t, p_r)
end

def write_mergin_border(line_offset:, line_width:)
  m_t, m_r, m_b, m_l = page_margin

  p_t = -(page_height - (m_t - line_offset) - m_b)
  p_b = line_offset
  p_l = -line_offset
  p_r = page_width - (m_r - line_offset) - m_l

  move_cursor_to 0
  stroke_horizontal_rule '000000', line_width, p_t, line_offset,
                                   -line_offset
  stroke_horizontal_rule '000000', line_width, p_b, line_offset,
                                   -line_offset
  stroke_vertical_rule '000000', line_width, p_l, line_offset,
                                 line_offset
  stroke_vertical_rule '000000', line_width, p_r, line_offset,
                                 line_offset
end

Appendix

Way to set Page Border and Surround header in Word of Microsoft’s Office.

How to set Page Border in Word is below.

  • Open DesignPageBorders
  • Push Box
  • Push OK
setting page border in Word

Result is below.

result of page border in Word

How to set Page Border in Word is below.

  • Open DesignPageBorders
  • Open Options...
  • Set Measure from to Text
  • Check Surround header
setting page border and surround header in Word

Result is below.

result of page border and surround header in Word

Say thank you

If this post helps you, I'd be glad to get your support via the link below.
paypal.me Badge paypal.me Badge