lib/hexapdf/cli/modify.rb in hexapdf-0.22.0 vs lib/hexapdf/cli/modify.rb in hexapdf-0.23.0

- old
+ new

@@ -51,18 +51,19 @@ def initialize #:nodoc: super('modify', takes_commands: false) short_desc("Modify a PDF file") long_desc(<<~EOF) - This command modifies a PDF file. It can be used to select pages that should appear in - the output file and/or rotate them. The output file can also be encrypted/decrypted and - optimized in various ways. + This command modifies a PDF file. It can be used, for example, to select pages that should + appear in the output file and/or rotate them. The output file can also be + encrypted/decrypted and optimized in various ways. EOF @password = nil @pages = '1-e' @embed_files = [] + @annotation_mode = nil options.on("--password PASSWORD", "-p", String, "The password for decryption. Use - for reading from standard input.") do |pwd| @password = (pwd == '-' ? read_password : pwd) end @@ -72,18 +73,23 @@ end options.on("-e", "--embed FILE", String, "Embed the file into the output file (can be " \ "used multiple times)") do |file| @embed_files << file end + options.on("--annotations MODE", [:remove, :flatten], "Handling of annotations (either " \ + "remove or flatten)") do |mode| + @annotation_mode = mode + end define_optimization_options define_encryption_options end def execute(in_file, out_file) #:nodoc: maybe_raise_on_existing_file(out_file) with_document(in_file, password: @password, out_file: out_file) do |doc| arrange_pages(doc) unless @pages == '1-e' + handle_annotations(doc) @embed_files.each {|file| doc.files.add(file, embed: true) } apply_encryption_options(doc) apply_optimization_options(doc) end end @@ -105,9 +111,23 @@ new_page_tree.add_page(page) end doc.catalog[:Pages] = new_page_tree remove_unused_pages(doc) doc.pages.add unless doc.pages.count > 0 + end + + # Handles the annotations of all selected pages by doing nothing, removing them or flattening + # them. + def handle_annotations(doc) + return unless @annotation_mode + + doc.pages.each do |page| + if @annotation_mode == :remove + page.delete(:Annots) + else + page.flatten_annotations + end + end end end end