docx icon indicating copy to clipboard operation
docx copied to clipboard

font color?

Open jnylund opened this issue 11 years ago • 3 comments

Hi, is there anyway to set the color of the text that is inserted using insert_text_after?

If not, I can try to add it, but can someone give me a hint of where to look or generally what to do? (I don't know anything about docx format).

thanks Joel

jnylund avatar May 30 '14 01:05 jnylund

Here's a way to do it using Nokogiri (which is what docx uses under the hood)

require 'docx'
require 'nokogiri'

def insert_colored_text_after(bookmark, text, hex_color)
    color = Nokogiri::XML::Node.new("w:color", bookmark.node)
    color['w:val'] = hex_color

    text_run = bookmark.get_run_after
    text_run.text = "#{text}#{text_run.text}"

    text_run.node.children.each do |child|
        #Find the node for formatting
        if child.name == "rPr"
            child<<color
        end
    end
end

Steimel avatar Jun 04 '14 14:06 Steimel

@Steimel Thank you for the solution, really appreciate it!

matthewgani avatar Mar 06 '24 07:03 matthewgani