Coverage Report

Created: 2026-11-12 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/include/qpdf/QPDFPageLabelDocumentHelper.hh
Line
Count
Source
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2026 Jay Berkenbilt and Manfred Holger
3
//
4
// This file is part of qpdf.
5
//
6
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7
// in compliance with the License. You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software distributed under the License
12
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
// or implied. See the License for the specific language governing permissions and limitations under
14
// the License.
15
//
16
// Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic
17
// License. At your option, you may continue to consider qpdf to be licensed under those terms.
18
// Please see the manual for additional information.
19
20
#ifndef QPDFPAGELABELDOCUMENTHELPER_HH
21
#define QPDFPAGELABELDOCUMENTHELPER_HH
22
23
#include <qpdf/QPDFDocumentHelper.hh>
24
25
#include <qpdf/DLL.h>
26
#include <qpdf/QPDF.hh>
27
28
#include <vector>
29
30
// Page labels are discussed in the PDF spec (ISO-32000) in section 12.4.2.
31
//
32
// Page labels are implemented as a number tree. Each key is a page index, numbered from 0. The
33
// values are dictionaries with the following keys, all optional:
34
//
35
// * /Type: if present, must be /PageLabel
36
// * /S: one of /D, /R, /r, /A, or /a for decimal, upper-case and lower-case Roman numeral, or
37
//   upper-case and lower-case alphabetic
38
// * /P: if present, a fixed prefix string that is prepended to each page number
39
// * /St: the starting number, or 1 if not specified
40
41
class QPDFPageLabelDocumentHelper: public QPDFDocumentHelper
42
{
43
  public:
44
    // Get a shared document helper for a given QPDF object.
45
    //
46
    // Retrieving a document helper for a QPDF object rather than creating a new one avoids repeated
47
    // validation of the PageLabels structure, which can be expensive.
48
    QPDF_DLL
49
    static QPDFPageLabelDocumentHelper& get(QPDF& qpdf);
50
51
    // Re-validate the PageLabels structure. This is useful if you have modified the structure of
52
    // the PageLabels dictionary in a way that could have invalidated the structure.
53
    //
54
    // If repair is true, the document will be repaired if possible if the validation encounters
55
    // errors.
56
    QPDF_DLL
57
    void validate(bool repair = true);
58
59
    QPDF_DLL
60
    QPDFPageLabelDocumentHelper(QPDF&);
61
62
10.3k
    ~QPDFPageLabelDocumentHelper() override = default;
63
64
    QPDF_DLL
65
    bool hasPageLabels();
66
67
    // Helper function to create a dictionary suitable for adding to the /PageLabels numbers tree.
68
    QPDF_DLL
69
    static QPDFObjectHandle
70
    pageLabelDict(qpdf_page_label_e label_type, int start_num, std::string_view prefix);
71
72
    // Return a page label dictionary representing the page label for the given page. The page does
73
    // not need to appear explicitly in the page label dictionary. This method will adjust /St as
74
    // needed to produce a label that is suitable for the page.
75
    QPDF_DLL
76
    QPDFObjectHandle getLabelForPage(long long page_idx);
77
78
    // Append to the incoming vector a list of objects suitable for inclusion in a /PageLabels
79
    // dictionary's /Nums field. start_idx and end_idx are the indexes to the starting and ending
80
    // pages (inclusive) in the original file, and new_start_idx is the index to the first page in
81
    // the new file. For example, if pages 10 through 12 of one file are being copied to a new file
82
    // as pages 6 through 8, you would call getLabelsForPageRange(10, 12, 6), which would return as
83
    // many entries as are required to add to the new file's PageLabels. This method fabricates a
84
    // suitable entry even if the original document has no page labels. This behavior facilitates
85
    // using this function to incrementally build up a page labels tree when merging files.
86
    QPDF_DLL
87
    void getLabelsForPageRange(
88
        long long start_idx,
89
        long long end_idx,
90
        long long new_start_idx,
91
        std::vector<QPDFObjectHandle>& new_labels);
92
93
  private:
94
    class Members;
95
96
    std::shared_ptr<Members> m;
97
};
98
99
#endif // QPDFPAGELABELDOCUMENTHELPER_HH