aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/pdf/PDFPage.java
blob: 49eb0c2784ffd47fba885fe64b59365d6357758b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */
 
package org.apache.fop.pdf;

/**
 * Class representing a /Page object.
 * <p>
 * There is one of these for every page in a PDF document. The object
 * specifies the dimensions of the page and references a /Resources
 * object, a contents stream and the page's parent in the page
 * hierarchy.
 */
public class PDFPage extends PDFResourceContext {

    /**
     * Holds a reference on the parent PDFPages object.
     */
    private String parentRef;

    /**
     * the contents stream
     */
    protected PDFStream contents;

    /**
     * the width of the page in points
     */
    protected int pagewidth;

    /**
     * the height of the page in points
     */
    protected int pageheight;

    /** the page index (zero-based) */
    protected int pageIndex;
    
    /**
     * Duration to display page
     */
    protected int duration = -1;

    /**
     * Transition dictionary
     */
    protected TransitionDictionary trDictionary = null;

    /**
     * Create a /Page object
     *
     * @param resources the /Resources object
     * @param contents the content stream
     * @param pageWidth the page's width in points
     * @param pageHeight the page's height in points
     * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
     */
    public PDFPage(PDFResources resources, PDFStream contents,
                   int pageWidth, int pageHeight, int pageIndex) {

        /* generic creation of object */
        super(resources);

        /* set fields using parameters */
        this.contents = contents;
        this.pagewidth = pageWidth;
        this.pageheight = pageHeight;
        this.pageIndex = pageIndex;
    }

    /**
     * Create a /Page object
     *
     * @param resources the /Resources object
     * @param pageWidth the page's width in points
     * @param pageHeight the page's height in points
     * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
     */
    public PDFPage(PDFResources resources,
                   int pageWidth, int pageHeight, int pageIndex) {
        this(resources, null, pageWidth, pageHeight, pageIndex);
    }

    /**
     * set this page contents
     *
     * @param contents the contents of the page
     */
    public void setContents(PDFStream contents) {
        this.contents = contents;
    }

    /**
     * set this page's parent
     *
     * @param parent the /Pages object that is this page's parent
     */
    public void setParent(PDFPages parent) {
        this.parentRef = parent.referencePDF();
    }

    /**
     * Set the transition dictionary and duration.
     * This sets the duration of the page and the transition
     * dictionary used when going to the next page.
     *
     * @param dur the duration in seconds
     * @param tr the transition dictionary
     */
    public void setTransition(int dur, TransitionDictionary tr) {
        duration = dur;
        trDictionary = tr;
    }

    /**
     * Returns the page width.
     * @return the page width
     */
    public int getWidth() {
        return this.pagewidth;
    }

    /**
     * Returns the page height.
     * @return the page height
     */
    public int getHeight() {
        return this.pageheight;
    }

    /**
     * @return the page Index of this page (zero-based), -1 if it the page index should
     *         automatically be determined.
     */
    public int getPageIndex() {
        return this.pageIndex;
    }
    
    /**
     * @see org.apache.fop.pdf.PDFObject#toPDFString()
     */
    public String toPDFString() {
        StringBuffer sb = new StringBuffer();

        String box = "[ 0 0 " + getWidth() + " " + getHeight() + " ]";
        sb = sb.append(getObjectID()
                       + "<< /Type /Page\n" 
                       + "/Parent " + this.parentRef + "\n"
                       + "/MediaBox " + box + "\n" 
                       + "/TrimBox " + box + "\n" //Needed for PDF/X
                       + "/BleedBox " + box + "\n" //Recommended by PDF/X
                       + "/Resources " + this.resources.referencePDF() + "\n" 
                       + "/Contents " + this.contents.referencePDF() + "\n");
        if (this.annotList != null) {
            sb = sb.append("/Annots " + this.annotList.referencePDF() + "\n");
        }
        if (this.duration != -1) {
            sb = sb.append("/Dur " + this.duration + "\n");
        }
        if (this.trDictionary != null) {
            sb = sb.append("/Trans << " + this.trDictionary.getDictionary() + " >>\n");
        }

        sb = sb.append(">>\nendobj\n");
        return sb.toString();
    }

}