# Renumbering PDF files - can this be implemented with PDFIO.jl instead?

**URL:** <https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144>\
**Category:** New to Julia\
**Tags:** pdf-format\
**Created:** [January 11, 2021, 7:14am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144 "2021-01-11T07:14:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 11, 2021, 7:14am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/1 "2021-01-11T07:14:49Z")

</div>

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](https://superuser.com/a/430628)

I’ve written this function:

```julia
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
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](https://github.com/sambitdash/PDFIO.jl) package instead?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 11, 2021, 8:13am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/2 "2021-01-11T08:13:17Z")

</div>

I just want to say that I like the `Pair` based syntax. LOL.

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 11, 2021, 8:22am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/3 "2021-01-11T08:22:13Z")

</div>

cc: @sambitdash

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 11, 2021, 8:22am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/4 "2021-01-11T08:22:29Z")

</div>

> [@mkitti](#):
>
> I just want to say that I like the `Pair` based syntax. LOL.

Inspired by your recent range PR!

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [January 11, 2021, 8:39am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/5 "2021-01-11T08:39:50Z")

</div>

The PDF editing is not currently a functionality in PDFIO. While I am open to any PRs that will enhance the scope PDFIO, PDFIO will always be compliant to the PDF Specification. The code you have suggested definitely does some quickfixes for PDF files but lacks in terms of ensuring consistent and specification compliant PDF files. If you can suggest the same and work towards ensuring PDF generated will be PDF specification compliant, feel free to submit a PR for review. I would believe it needs significant design changes to the existing PDFIO core to achieve the same.

Thanking you.

regards,

Sambit

Note: Some readers or even Adobe Reader opening a file does not necessarily make a file specification compliant. Readers show leniency as many incompatible PDF files are generated, that readers try to work gracefully.

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 11, 2021, 9:12am UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/6 "2021-01-11T09:12:45Z")

</div>

Thanks Sambit! I definitely agree that we must comply with the PDF spec.

I don’t know very much about the internals of PDF files. Do you know of a way we can accomplish the desired task (adding `PageLabels` to the catalog dictionary, as described in table 28 on page 73 [here](https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf)) while staying in compliance with the PDF spec?

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 11, 2021, 12:00pm UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/7 "2021-01-11T12:00:04Z")

</div>

I think the main point here is: With your string replacement you change the length of an (PDF) object and that should be input to recreate the correct xref table at the end of the .pdf (or the correct place). PDF supports also incremental updates, so you could put your object at the end of the previous file and write a new end-of-file structure. Both things are easy if the underlying library deals with independent objects.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 11, 2021, 2:00pm UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/8 "2021-01-11T14:00:38Z")

</div>

Having a clear unambiguous argument order that will not get confused with similarly named Python API has its benefits.

To your original question, have you considered Taro.jl by @avik ? Use it Java 8. It provides a Julia interface to to Apache Tika and FOP.

> **[Taro.jl](http://aviks.github.io/Taro.jl/)**
>
> Work with document files in Julia

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [January 11, 2021, 6:11pm UTC](https://discourse.julialang.org/t/renumbering-pdf-files-can-this-be-implemented-with-pdfio-jl-instead/53144/9 "2021-01-11T18:11:29Z")

</div>

PDFIO does not support PDF editing functionality. So if you are expecting to recreate a new PDF or modify an existing PDF file, the PDFIO library today cannot do so.

regards,

Sambit
