1 /*
2 * $Id: PdfReaderInstance.java 3527 2008-07-06 15:34:38Z blowagie $
3 *
4 * Copyright 2001, 2002 Paulo Soares
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text.pdf;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.Iterator;
55 /**
56 * Instance of PdfReader in each output document.
57 *
58 * @author Paulo Soares (psoares@consiste.pt)
59 */
60 class PdfReaderInstance {
61 static final PdfLiteral IDENTITYMATRIX = new PdfLiteral("[1 0 0 1 0 0]");
62 static final PdfNumber ONE = new PdfNumber(1);
63 int myXref[];
64 PdfReader reader;
65 RandomAccessFileOrArray file;
66 HashMap importedPages = new HashMap();
67 PdfWriter writer;
68 HashMap visited = new HashMap();
69 ArrayList nextRound = new ArrayList();
70
71 PdfReaderInstance(PdfReader reader, PdfWriter writer) {
72 this.reader = reader;
73 this.writer = writer;
74 file = reader.getSafeFile();
75 myXref = new int[reader.getXrefSize()];
76 }
77
78 PdfReader getReader() {
79 return reader;
80 }
81
82 PdfImportedPage getImportedPage(int pageNumber) {
83 if (!reader.isOpenedWithFullPermissions())
84 throw new IllegalArgumentException("PdfReader not opened with owner password");
85 if (pageNumber < 1 || pageNumber > reader.getNumberOfPages())
86 throw new IllegalArgumentException("Invalid page number: " + pageNumber);
87 Integer i = new Integer(pageNumber);
88 PdfImportedPage pageT = (PdfImportedPage)importedPages.get(i);
89 if (pageT == null) {
90 pageT = new PdfImportedPage(this, writer, pageNumber);
91 importedPages.put(i, pageT);
92 }
93 return pageT;
94 }
95
96 int getNewObjectNumber(int number, int generation) {
97 if (myXref[number] == 0) {
98 myXref[number] = writer.getIndirectReferenceNumber();
99 nextRound.add(new Integer(number));
100 }
101 return myXref[number];
102 }
103
104 RandomAccessFileOrArray getReaderFile() {
105 return file;
106 }
107
108 PdfObject getResources(int pageNumber) {
109 PdfObject obj = PdfReader.getPdfObjectRelease(reader.getPageNRelease(pageNumber).get(PdfName.RESOURCES));
110 return obj;
111 }
112
113 /**
114 * Gets the content stream of a page as a PdfStream object.
115 * @param pageNumber the page of which you want the stream
116 * @param compressionLevel the compression level you want to apply to the stream
117 * @return a PdfStream object
118 * @since 2.1.3 (the method already existed without param compressionLevel)
119 */
120 PdfStream getFormXObject(int pageNumber, int compressionLevel) throws IOException {
121 PdfDictionary page = reader.getPageNRelease(pageNumber);
122 PdfObject contents = PdfReader.getPdfObjectRelease(page.get(PdfName.CONTENTS));
123 PdfDictionary dic = new PdfDictionary();
124 byte bout[] = null;
125 if (contents != null) {
126 if (contents.isStream())
127 dic.putAll((PRStream)contents);
128 else
129 bout = reader.getPageContent(pageNumber, file);
130 }
131 else
132 bout = new byte[0];
133 dic.put(PdfName.RESOURCES, PdfReader.getPdfObjectRelease(page.get(PdfName.RESOURCES)));
134 dic.put(PdfName.TYPE, PdfName.XOBJECT);
135 dic.put(PdfName.SUBTYPE, PdfName.FORM);
136 PdfImportedPage impPage = (PdfImportedPage)importedPages.get(new Integer(pageNumber));
137 dic.put(PdfName.BBOX, new PdfRectangle(impPage.getBoundingBox()));
138 PdfArray matrix = impPage.getMatrix();
139 if (matrix == null)
140 dic.put(PdfName.MATRIX, IDENTITYMATRIX);
141 else
142 dic.put(PdfName.MATRIX, matrix);
143 dic.put(PdfName.FORMTYPE, ONE);
144 PRStream stream;
145 if (bout == null) {
146 stream = new PRStream((PRStream)contents, dic);
147 }
148 else {
149 stream = new PRStream(reader, bout, compressionLevel);
150 stream.putAll(dic);
151 }
152 return stream;
153 }
154
155 void writeAllVisited() throws IOException {
156 while (!nextRound.isEmpty()) {
157 ArrayList vec = nextRound;
158 nextRound = new ArrayList();
159 for (int k = 0; k < vec.size(); ++k) {
160 Integer i = (Integer)vec.get(k);
161 if (!visited.containsKey(i)) {
162 visited.put(i, null);
163 int n = i.intValue();
164 writer.addToBody(reader.getPdfObjectRelease(n), myXref[n]);
165 }
166 }
167 }
168 }
169
170 void writeAllPages() throws IOException {
171 try {
172 file.reOpen();
173 for (Iterator it = importedPages.values().iterator(); it.hasNext();) {
174 PdfImportedPage ip = (PdfImportedPage)it.next();
175 writer.addToBody(ip.getFormXObject(writer.getCompressionLevel()), ip.getIndirectReference());
176 }
177 writeAllVisited();
178 }
179 finally {
180 try {
181 reader.close();
182 file.close();
183 }
184 catch (Exception e) {
185 //Empty on purpose
186 }
187 }
188 }
189 }