Lately I got pretty confused about all these getXXX() and getfromXXX()
functions. So I started to hand craft a PDF (adding entries for all
kinds of PDF types), and then read back these values with luatex.
The result is a table of these getter functions and their return values.

I just thought I'd share this information with you. Maybe it's useful
for some of you.

The PDF ('test.pdf' see attachment) is quite simple to read (formatted with
mutools), and you can easily compare the particular PDF objects with
the output of the getter functions of luatex. (I added all theses
/testXXX entries
in object number 2 of the PDF.)

Run

   texlua info.lua

to see the output in the terminal. Sorry, there's no PDF output.

Of course all this information is available in the documentation of
luatex as well, but I think it's nice to have some test files to see
what's going on.


Best regards
Andreas

Attachment: test.pdf
Description: Adobe PDF document

print()

local width = 78

local function title(header1, header2)
   print(string.rep('=', width))
   print()
   print(header1)
   print()
   print(string.rep('=', width))
   header2()
   print(string.rep('-', width))
end

local pp1 = function(func, val, t)
   print(string.format(' %-15s %-35s %-30s', func, val, t))
end

local pp2 = function(pdf_type, t, v, d, lua_type)
   print(string.format(' %-15s %-8s %-32s %-10s %s', pdf_type, t, v, d, lua_type))
end


-------------------------------------------------------------------------------
--
-- pdfe.getXXX(<dictionary>, <key>)
--
--------------------------------------------------------------------------------
title(
   ' pdfe.getXXX(<dictionary>, <key>)',
   function()
      pp1('function', 'returned', 'Lua type')
      pp1('', 'value', 'of value')
end)

local doc = pdfe.open('test.pdf')
local dict = doc.Pages[1]

local val  = pdfe.getboolean(dict, 'testBoolean')
pp1('getboolean()', val, type(val))

local val  = pdfe.getinteger(dict, 'testInteger')
pp1('getinteger()', val, type(val))

local val  = pdfe.getnumber(dict, 'testNumber')
pp1('getnumber()', val, type(val))

local val  = pdfe.getstring(dict, 'testString')
pp1('getstring()', val, type(val))

local val  = pdfe.getstring(dict, 'testStringHex')
pp1('getstring()', val, type(val))

local val  = pdfe.getarray(dict, 'testArray')
pp1('getarray()', val, type(val))

local val  = pdfe.getdictionary(dict, 'testDictionary')
pp1('getdictionary()', val, type(val))

-- reference
pp1('getreference()', 'not implemented', '')

-- stream
local stream, dict = pdfe.getstream(dict, 'Contents')
print()
pp1('getstream()', stream, type(stream))
pp1('', dict, type(dict))


--------------------------------------------------------------------------------
--
-- pdfe.getfromdictionary(<dict>, <key>)
--
--------------------------------------------------------------------------------
title(
   ' pdfe.getfromdictionary(<dictionary>, <key>)',
   function()
      pp2('PDF-type', 'returned', 'returned', 'returned', 'Lua type')
      pp2('', 'type', 'value', 'detail', 'of value')
end)

local doc = pdfe.open('test.pdf')
local dict = doc.Pages[1]

local t, v, d = pdfe.getfromdictionary(dict, 'testBoolean')
pp2('boolean', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testInteger')
pp2('integer', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testNumber')
pp2('number', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testName')
pp2('name', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testString')
pp2('string (plain)', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testStringHex')
pp2('string (hex)', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testArray')
pp2('array', t, v, d, type(v))

local t, v, d = pdfe.getfromdictionary(dict, 'testDictionary')
pp2('dictionary', t, v, d, type(v))

-- stream
pp2('stream', '', 'streams not allowed', '')

local t, v, d = pdfe.getfromdictionary(dict, 'testReference')
pp2('reference', t, v, d, type(v))


-------------------------------------------------------------------------------
--
-- pdfe.getXXX(<array>, <key>)
--
--    Note: Zero-based array indexing!
--
--------------------------------------------------------------------------------
title(
   ' pdfe.getXXX(<array>, <key>)    [Note: Zero-based array indexing!]',
   function()
      pp1('function', 'returned', 'Lua type')
      pp1('', 'value', 'of value')
end)

local doc = pdfe.open('test.pdf')
local array = doc.Pages[1].testArray2

local val  = pdfe.getboolean(array, 0)
pp1('getboolean()', val, type(val))

local val  = pdfe.getinteger(array, 1)
pp1('getinteger()', val, type(val))

local val  = pdfe.getnumber(array, 2)
pp1('getnumber()', val, type(val))

local val = pdfe.getname(array, 3)
pp1('getname()', val, type(val))

local val  = pdfe.getstring(array, 4)
pp1('getstring()', val, type(val))

local val  = pdfe.getstring(array, 5)
pp1('getstring()', val, type(val))

local val  = pdfe.getarray(array, 6)
pp1('getarray()', val, type(val))

local val  = pdfe.getdictionary(array, 7)
pp1('getdictionary()', val, type(val))

pp1('', 'streams not allowed')
pp1('', 'references not allowed')


--------------------------------------------------------------------------------
--
-- pdfe.getfromarray(<array>, <index>)
--
--    Note: One-based array indexing!
--
--------------------------------------------------------------------------------
title(
   ' pdfe.getfromarray(<array>, <index>)    [Note: One-based array indexing!]',
   function()
      pp2('PDF-type', 'returned', 'returned', 'returned', 'Lua type')
      pp2('', 'type', 'value', 'detail', 'of value')
end)

local doc = pdfe.open('test.pdf')
local array = doc.Pages[1].testArray2

local t, v, d = pdfe.getfromarray(array, 1)
pp2('boolean', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 2)
pp2('integer', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 3)
pp2('number', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 4)
pp2('name', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 5)
pp2('string (plain)', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 6)
pp2('string (hex)', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 7)
pp2('array', t, v, d, type(v))

local t, v, d = pdfe.getfromarray(array, 8)
pp2('dictionary', t, v, d, type(v))

pp2('', '', 'streams not allowed', '', '')

pp2('', '', 'references not allowed', '', '')

print(string.rep('-', width))

Reply via email to