Coverage Report

Created: 2025-08-29 06:53

/src/qpdf/include/qpdf/QPDFPageDocumentHelper.hh
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2025 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 QPDFPAGEDOCUMENTHELPER_HH
21
#define QPDFPAGEDOCUMENTHELPER_HH
22
23
#include <qpdf/Constants.h>
24
#include <qpdf/QPDFDocumentHelper.hh>
25
#include <qpdf/QPDFPageObjectHelper.hh>
26
27
#include <qpdf/DLL.h>
28
29
#include <vector>
30
31
#include <qpdf/QPDF.hh>
32
33
class QPDFAcroFormDocumentHelper;
34
35
class QPDFPageDocumentHelper: public QPDFDocumentHelper
36
{
37
  public:
38
    QPDF_DLL
39
    QPDFPageDocumentHelper(QPDF&);
40
41
0
    ~QPDFPageDocumentHelper() override = default;
42
43
    // Traverse page tree, and return all /Page objects wrapped in QPDFPageObjectHelper objects.
44
    // Unlike with QPDF::getAllPages, the vector of pages returned by this call is not affected by
45
    // additions or removals of pages. If you manipulate pages, you will have to call this again to
46
    // get a new copy. Please see comments in QPDF.hh for getAllPages() for additional details.
47
    QPDF_DLL
48
    std::vector<QPDFPageObjectHelper> getAllPages();
49
50
    // The PDF /Pages tree allows inherited values. Working with the pages of a pdf is much easier
51
    // when the inheritance is resolved by explicitly setting the values in each /Page.
52
    QPDF_DLL
53
    void pushInheritedAttributesToPage();
54
55
    // This calls QPDFPageObjectHelper::removeUnreferencedResources for every page in the document.
56
    // See comments in QPDFPageObjectHelper.hh for details.
57
    QPDF_DLL
58
    void removeUnreferencedResources();
59
60
    // Add a new page at the beginning or the end of the current pdf. The newpage parameter may be
61
    // either a direct object, an indirect object from this QPDF, or an indirect object from another
62
    // QPDF. If it is a direct object, it will be made indirect. If it is an indirect object from
63
    // another QPDF, this method will call pushInheritedAttributesToPage on the other file and then
64
    // copy the page to this QPDF using the same underlying code as copyForeignObject. At this
65
    // stage, if the indirect object is already in the pages tree, a shallow copy is made to avoid
66
    // adding the same page more than once. In version 10.3.1 and earlier, adding a page that
67
    // already existed would throw an exception and could cause qpdf to crash on subsequent page
68
    // insertions in some cases. Note that this means that, in some cases, the page actually added
69
    // won't be exactly the same object as the one passed in. If you want to do subsequent
70
    // modification on the page, you should retrieve it again.
71
    //
72
    // Note that you can call copyForeignObject directly to copy a page from a different file, but
73
    // the resulting object will not be a page in the new file. You could do this, for example, to
74
    // convert a page into a form XObject, though for that, you're better off using
75
    // QPDFPageObjectHelper::getFormXObjectForPage.
76
    //
77
    // This method does not have any specific awareness of annotations or form fields, so if you
78
    // just add a page without thinking about it, you might end up with two pages that share form
79
    // fields or annotations. While the page may look fine, it will probably not function properly
80
    // with regard to interactive features. To work around this, you should call
81
    // QPDFAcroFormDocumentHelper::fixCopiedAnnotations. A future version of qpdf will likely
82
    // provide a higher-level interface for copying pages around that will handle document-level
83
    // constructs in a less error-prone fashion.
84
85
    QPDF_DLL
86
    void addPage(QPDFPageObjectHelper newpage, bool first);
87
88
    // Add new page before or after refpage. See comments for addPage for details about what newpage
89
    // should be.
90
    QPDF_DLL
91
    void addPageAt(QPDFPageObjectHelper newpage, bool before, QPDFPageObjectHelper refpage);
92
93
    // Remove page from the pdf.
94
    QPDF_DLL
95
    void removePage(QPDFPageObjectHelper page);
96
97
    // For every annotation, integrate the annotation's appearance stream into the containing page's
98
    // content streams, merge the annotation's resources with the page's resources, and remove the
99
    // annotation from the page. Handles widget annotations associated with interactive form fields
100
    // as a special case, including removing the /AcroForm key from the document catalog. The values
101
    // passed to required_flags and forbidden_flags are passed along to
102
    // QPDFAnnotationObjectHelper::getPageContentForAppearance. See comments there in
103
    // QPDFAnnotationObjectHelper.hh for meanings of those flags.
104
    QPDF_DLL
105
    void flattenAnnotations(int required_flags = 0, int forbidden_flags = an_invisible | an_hidden);
106
107
  private:
108
    void flattenAnnotationsForPage(
109
        QPDFPageObjectHelper& page,
110
        QPDFObjectHandle& resources,
111
        QPDFAcroFormDocumentHelper& afdh,
112
        int required_flags,
113
        int forbidden_flags);
114
115
    class Members
116
    {
117
        friend class QPDFPageDocumentHelper;
118
119
      public:
120
        ~Members() = default;
121
122
      private:
123
        Members() = default;
124
        Members(Members const&) = delete;
125
    };
126
127
    std::shared_ptr<Members> m;
128
};
129
130
#endif // QPDFPAGEDOCUMENTHELPER_HH