Method: PDFium::Document#metadata

Defined in:
ext/pdfium_ext/document.cc

#metadataHash

Retrieves and optionally sets the metadata on a document. Returns a hash with the following keys:

:title, :author :subject, :keywords, :creator, :producer, :creation_date, :mod_date

An empty Hash will be returned if the metadata cannot be read

All values in the hash are encoded as UTF-16LE strings.

If caled with a block, the values will be passed to it and updates written back to the Document

Example

pdf = PDFium::Document.new( "test.pdf" )
pdf. do | md |
    md[:title]  = "My Awesome PDF"
    md[:author] = "Nathan Stitt"
end
pdf.[:author] # => "Nathan Stitt"

Returns:

  • (Hash)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/pdfium_ext/document.cc', line 205

VALUE
(int argc, VALUE *argv, VALUE self)
{
    auto doc = RB2DOC(self);
    VALUE  = rb_hash_new();
    CPDF_Dictionary* info = doc->GetInfo();
    if (!info)
        return ;

    VALUE block;
    rb_scan_args(argc, argv, "0&", &block);

    std::map<std::string, std::string> keys = {
        { "Title",        "title"  },
        { "Author",       "author" },
        { "Subject",      "subject" },
        { "Keywords",     "keywords"},
        { "Creator",      "creator" },
        { "Producer",     "producer"},
        { "CreationDate", "creation_date" },
        { "ModDate",      "mod_date" }
    };

    for (auto& kv : keys) {
        rb_hash_aset(,
                     ID2SYM( rb_intern( kv.second.c_str() ) ),
                     RB::to_string( info->GetUnicodeText( kv.first.c_str() ) )
                     );
    }

    if (RTEST(block)){
        rb_yield(  );
        for (auto& kv : keys) {
            VALUE value = RB::get_option(, kv.second);
            auto bs = CFX_ByteString( RSTRING_PTR(value), RSTRING_LEN(value) );
            info->SetAtString(kv.first.c_str(), bs);
        }
    }

    return ;
}