I often find myself with PDFs in which the physical numbering of the pages does not match the numbering of the PDF.
For example, suppose that you have a PDF in which the first twelve pages of the PDF are front matter that should be numbered with the Roman numerals i, ii, iii, ..., xi, xii. And then, starting with the thirteenth page of the PDF, the pages should be numbered with Arabic numerals 1, 2, 3, ....
Basically, it’s the task described in this answer: metadata - How to change internal page numbers in the meta data of a PDF? - Super User
I’ve written this function:
function renumber(p::Pair{<:AbstractString, <:AbstractString},
n::Integer)
input_filename = p[1]
output_filename = p[2]
a = read(input_filename, String)
r = r"(<<\/Type[\s]?\/Catalog[\s]?[\s\S]*?)(>>)"
s = SubstitutionString(
string(
"\\1",
"/PageLabels << /Nums [ 0 << /S /r >>\n",
" % labels pages 1 to $(n-1) in small Roman numerals\n",
" $(n-1) << /S /D >>\n",
" % numbers pages $(n) to the end in Arabic numerals\n",
" ]\n",
" >>\n",
"\\2\n",
)
)
b = replace(a, r => s)
rm(output_filename; force = true, recursive = true)
open(output_filename, "w") do io
println(io, b)
end
return output_filename
end
Which can be run as such:
julia> renumber("inputfile.pdf" => "outputfile.pdf", 13)
This is pretty hacky. Is there a better way? For example, could we implement this using the PDFIO.jl package instead?