PDFDoc.h 12 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1
2
3
4
5
6
7
8
//========================================================================
//
// PDFDoc.h
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

9
10
11
12
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
13
14
15
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
16
// Copyright (C) 2005, 2006, 2008 Brad Hards <bradh@frogmouth.net>
17
// Copyright (C) 2005, 2009 Albert Astals Cid <aacid@kde.org>
18
19
20
// Copyright (C) 2008 Julien Rebetez <julienr@svn.gnome.org>
// Copyright (C) 2008 Pino Toscano <pino@kde.org>
// Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
21
// Copyright (C) 2009 Eric Toombs <ewtoombs@uwaterloo.ca>
Kovid Goyal's avatar
Kovid Goyal committed
22
// Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
Hib Eris's avatar
Hib Eris committed
23
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
Albert Astals Cid's avatar
Albert Astals Cid committed
24
// Copyright (C) 2010 Srinivas Adicherla <srinivas.adicherla@geodesic.com>
Thomas Freitag's avatar
Thomas Freitag committed
25
// Copyright (C) 2011 Thomas Freitag <Thomas.Freitag@alfa.de>
26
// Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
27
28
29
30
31
32
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef PDFDOC_H
#define PDFDOC_H

#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif

#include <stdio.h>
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
#include "Annot.h"
45
#include "OptionalContent.h"
46
#include "Stream.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
47
48
49
50
51
52
53
54

class GooString;
class BaseStream;
class OutputDev;
class Links;
class LinkAction;
class LinkDest;
class Outline;
Hib Eris's avatar
Hib Eris committed
55
56
57
class Linearization;
class SecurityHandler;
class Hints;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
58

59
60
61
62
63
64
enum PDFWriteMode {
  writeStandard,
  writeForceRewrite,
  writeForceIncremental
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
65
66
67
68
69
70
71
72
//------------------------------------------------------------------------
// PDFDoc
//------------------------------------------------------------------------

class PDFDoc {
public:

  PDFDoc(GooString *fileNameA, GooString *ownerPassword = NULL,
73
	 GooString *userPassword = NULL, void *guiDataA = NULL);
74

Kovid Goyal's avatar
Kovid Goyal committed
75
#ifdef _WIN32
76
77
78
79
  PDFDoc(wchar_t *fileNameA, int fileNameLen, GooString *ownerPassword = NULL,
	 GooString *userPassword = NULL, void *guiDataA = NULL);
#endif

Kristian Høgsberg's avatar
Kristian Høgsberg committed
80
  PDFDoc(BaseStream *strA, GooString *ownerPassword = NULL,
81
	 GooString *userPassword = NULL, void *guiDataA = NULL);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
82
83
  ~PDFDoc();

Hib Eris's avatar
Hib Eris committed
84
85
  static PDFDoc *ErrorPDFDoc(int errorCode, GooString *fileNameA = NULL);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
86
87
88
89
90
91
  // Was PDF document successfully opened?
  GBool isOk() { return ok; }

  // Get the error code (if isOk() returns false).
  int getErrorCode() { return errCode; }

92
93
94
95
  // Get the error code returned by fopen() (if getErrorCode() == 
  // errOpenFile).
  int getFopenErrno() { return fopenErrno; }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
96
97
  // Get file name.
  GooString *getFileName() { return fileName; }
98
99
100
#ifdef _WIN32
  wchar_t *getFileNameU() { return fileNameU; }
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
101

Hib Eris's avatar
Hib Eris committed
102
103
104
  // Get the linearization table.
  Linearization *getLinearization();

Kristian Høgsberg's avatar
Kristian Høgsberg committed
105
106
107
108
109
110
  // Get the xref table.
  XRef *getXRef() { return xref; }

  // Get catalog.
  Catalog *getCatalog() { return catalog; }

111
112
113
  // Get optional content configuration
  OCGs *getOptContentConfig() { return catalog->getOptContentConfig(); }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
114
115
116
117
  // Get base stream.
  BaseStream *getBaseStream() { return str; }

  // Get page parameters.
118
  double getPageMediaWidth(int page)
Hib Eris's avatar
Hib Eris committed
119
    { return getPage(page) ? getPage(page)->getMediaWidth() : 0.0 ; }
120
  double getPageMediaHeight(int page)
Hib Eris's avatar
Hib Eris committed
121
    { return getPage(page) ? getPage(page)->getMediaHeight() : 0.0 ; }
122
  double getPageCropWidth(int page)
Hib Eris's avatar
Hib Eris committed
123
    { return getPage(page) ? getPage(page)->getCropWidth() : 0.0 ; }
124
  double getPageCropHeight(int page)
Hib Eris's avatar
Hib Eris committed
125
    { return getPage(page) ? getPage(page)->getCropHeight() : 0.0 ; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
126
  int getPageRotate(int page)
Hib Eris's avatar
Hib Eris committed
127
    { return getPage(page) ? getPage(page)->getRotate() : 0 ; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
128
129

  // Get number of pages.
Hib Eris's avatar
Hib Eris committed
130
  int getNumPages();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
131
132
133
134
135
136
137
138

  // Return the contents of the metadata stream, or NULL if there is
  // no metadata.
  GooString *readMetadata() { return catalog->readMetadata(); }

  // Return the structure tree root object.
  Object *getStructTreeRoot() { return catalog->getStructTreeRoot(); }

Hib Eris's avatar
Hib Eris committed
139
140
141
  // Get page.
  Page *getPage(int page);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
142
  // Display a page.
143
144
145
  void displayPage(OutputDev *out, int page,
		   double hDPI, double vDPI, int rotate,
		   GBool useMediaBox, GBool crop, GBool printing,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
146
147
148
149
150
151
152
153
		   GBool (*abortCheckCbk)(void *data) = NULL,
		   void *abortCheckCbkData = NULL,
                   GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
                   void *annotDisplayDecideCbkData = NULL);

  // Display a range of pages.
  void displayPages(OutputDev *out, int firstPage, int lastPage,
		    double hDPI, double vDPI, int rotate,
154
		    GBool useMediaBox, GBool crop, GBool printing,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
155
156
157
158
159
160
161
		    GBool (*abortCheckCbk)(void *data) = NULL,
		    void *abortCheckCbkData = NULL,
                    GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
                    void *annotDisplayDecideCbkData = NULL);

  // Display part of a page.
  void displayPageSlice(OutputDev *out, int page,
162
163
			double hDPI, double vDPI, int rotate, 
			GBool useMediaBox, GBool crop, GBool printing,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
164
165
166
167
168
169
170
171
172
173
			int sliceX, int sliceY, int sliceW, int sliceH,
			GBool (*abortCheckCbk)(void *data) = NULL,
			void *abortCheckCbkData = NULL,
                        GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
                        void *annotDisplayDecideCbkData = NULL);

  // Find a page, given its object ID.  Returns page number, or 0 if
  // not found.
  int findPage(int num, int gen) { return catalog->findPage(num, gen); }

174
175
  // Returns the links for the current page, transferring ownership to
  // the caller.
176
  Links *getLinks(int page);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
177
178
179

  // Find a named destination.  Returns the link destination, or
  // NULL if <name> is not a destination.
180
  LinkDest *findDest(GooString *name)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
181
182
    { return catalog->findDest(name); }

183
184
185
186
  // Process the links for a page.
  void processLinks(OutputDev *out, int page);


Kristian Høgsberg's avatar
Kristian Høgsberg committed
187
188
#ifndef DISABLE_OUTLINE
  // Return the outline object.
Hib Eris's avatar
Hib Eris committed
189
  Outline *getOutline();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
190
191
192
193
194
195
196
197
#endif

  // Is the file encrypted?
  GBool isEncrypted() { return xref->isEncrypted(); }

  // Check various permissions.
  GBool okToPrint(GBool ignoreOwnerPW = gFalse)
    { return xref->okToPrint(ignoreOwnerPW); }
198
199
  GBool okToPrintHighRes(GBool ignoreOwnerPW = gFalse)
    { return xref->okToPrintHighRes(ignoreOwnerPW); }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
200
201
202
203
204
205
  GBool okToChange(GBool ignoreOwnerPW = gFalse)
    { return xref->okToChange(ignoreOwnerPW); }
  GBool okToCopy(GBool ignoreOwnerPW = gFalse)
    { return xref->okToCopy(ignoreOwnerPW); }
  GBool okToAddNotes(GBool ignoreOwnerPW = gFalse)
    { return xref->okToAddNotes(ignoreOwnerPW); }
206
207
208
209
210
211
212
  GBool okToFillForm(GBool ignoreOwnerPW = gFalse)
    { return xref->okToFillForm(ignoreOwnerPW); }
  GBool okToAccessibility(GBool ignoreOwnerPW = gFalse)
    { return xref->okToAccessibility(ignoreOwnerPW); }
  GBool okToAssemble(GBool ignoreOwnerPW = gFalse)
    { return xref->okToAssemble(ignoreOwnerPW); }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
213
214
215
216
217
218
219
220
221

  // Is this document linearized?
  GBool isLinearized();

  // Return the document's Info dictionary (if any).
  Object *getDocInfo(Object *obj) { return xref->getDocInfo(obj); }
  Object *getDocInfoNF(Object *obj) { return xref->getDocInfoNF(obj); }

  // Return the PDF version specified by the file.
222
223
  int getPDFMajorVersion() { return pdfMajorVersion; }
  int getPDFMinorVersion() { return pdfMinorVersion; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
224

225
226
227
  //Return the PDF ID in the trailer dictionary (if any).
  GBool getID(GooString *permanent_id, GooString *update_id);

Thomas Freitag's avatar
Thomas Freitag committed
228
229
  // Save one page with another name.
  int savePageAs(GooString *name, int pageNo);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
230
  // Save this file with another name.
231
  int saveAs(GooString *name, PDFWriteMode mode=writeStandard);
232
  // Save this file in the given output stream.
233
  int saveAs(OutStream *outStr, PDFWriteMode mode=writeStandard);
234
  // Save this file with another name without saving changes
235
  int saveWithoutChangesAs(GooString *name);
236
  // Save this file in the given output stream without saving changes
237
  int saveWithoutChangesAs(OutStream *outStr);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
238

239
240
241
  // Return a pointer to the GUI (XPDFCore or WinPDFCore object).
  void *getGUIData() { return guiData; }

Thomas Freitag's avatar
Thomas Freitag committed
242
243
244
245
246
  // rewrite pageDict with MediaBox, CropBox and new page CTM
  void replacePageDict(int pageNo, int rotate, PDFRectangle *mediaBox, PDFRectangle *cropBox, Object *pageCTM);
  void markPageObjects(Dict *pageDict, XRef *xRef, XRef *countRef, Guint numOffset);
  // write all objects used by pageDict to outStr
  Guint writePageObjects(OutStream *outStr, XRef *xRef, Guint numOffset);
247
248
  static void writeObject (Object *obj, OutStream* outStr, XRef *xref, Guint numOffset, Guchar *fileKey,
                           CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
Thomas Freitag's avatar
Thomas Freitag committed
249
  static void writeHeader(OutStream *outStr, int major, int minor);
250
251
252
253
254
255
256
257

  // Ownership goes to the caller
  static Dict *createTrailerDict (int uxrefSize, GBool incrUpdate, Guint startxRef,
                                  Ref *root, XRef *xRef, const char *fileName, Guint fileSize);
  static void writeXRefTableTrailer (Dict *trailerDict, XRef *uxref, GBool writeAllEntries,
                                     Guint uxrefOffset, OutStream* outStr, XRef *xRef);
  static void writeXRefStreamTrailer (Dict *trailerDict, XRef *uxref, Ref *uxrefStreamRef,
                                      Guint uxrefOffset, OutStream* outStr, XRef *xRef);
Thomas Freitag's avatar
Thomas Freitag committed
258

Kristian Høgsberg's avatar
Kristian Høgsberg committed
259
private:
Thomas Freitag's avatar
Thomas Freitag committed
260
261
262
  // insert referenced objects in XRef
  void markDictionnary (Dict* dict, XRef *xRef, XRef *countRef, Guint numOffset);
  void markObject (Object *obj, XRef *xRef, XRef *countRef, Guint numOffset);
263
264
  static void writeDictionnary (Dict* dict, OutStream* outStr, XRef *xRef, Guint numOffset, Guchar *fileKey,
                                CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
Thomas Freitag's avatar
Thomas Freitag committed
265

266
267
268
269
  // Write object header to current file stream and return its offset
  static Guint writeObjectHeader (Ref *ref, OutStream* outStr);
  static void writeObjectFooter (OutStream* outStr);

270
271
272
273
274
275
  void writeObject (Object *obj, OutStream* outStr, Guchar *fileKey, CryptAlgorithm encAlgorithm,
                    int keyLength, int objNum, int objGen)
  { writeObject(obj, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen); }
  void writeDictionnary (Dict* dict, OutStream* outStr, Guchar *fileKey, CryptAlgorithm encAlgorithm,
                         int keyLength, int objNum, int objGen)
  { writeDictionnary(dict, outStr, getXRef(), 0, fileKey, encAlgorithm, keyLength, objNum, objGen); }
Thomas Freitag's avatar
Thomas Freitag committed
276
277
  static void writeStream (Stream* str, OutStream* outStr);
  static void writeRawStream (Stream* str, OutStream* outStr);
278
279
  void writeXRefTableTrailer (Guint uxrefOffset, XRef *uxref, GBool writeAllEntries,
                              int uxrefSize, OutStream* outStr, GBool incrUpdate);
280
281
  static void writeString (GooString* s, OutStream* outStr, Guchar *fileKey,
                           CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
282
283
284
  void saveIncrementalUpdate (OutStream* outStr);
  void saveCompleteRewrite (OutStream* outStr);

Hib Eris's avatar
Hib Eris committed
285
286
287
288
289
  Page *parsePage(int page);

  // Get hints.
  Hints *getHints();

Hib Eris's avatar
Hib Eris committed
290
  PDFDoc();
Hib Eris's avatar
Hib Eris committed
291
  void init();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
292
  GBool setup(GooString *ownerPassword, GooString *userPassword);
293
  GBool checkFooter();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
294
  void checkHeader();
295
  GBool checkEncryption(GooString *ownerPassword, GooString *userPassword);
Hib Eris's avatar
Hib Eris committed
296
297
298
299
300
301
  // Get the offset of the start xref table.
  Guint getStartXRef();
  // Get the offset of the entries in the main XRef table of a
  // linearized document (0 for non linearized documents).
  Guint getMainXRefEntriesOffset();
  Guint strToUnsigned(char *s);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
302
303

  GooString *fileName;
304
305
306
#ifdef _WIN32
  wchar_t *fileNameU;
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
307
308
  FILE *file;
  BaseStream *str;
309
  void *guiData;
310
311
  int pdfMajorVersion;
  int pdfMinorVersion;
Hib Eris's avatar
Hib Eris committed
312
  Linearization *linearization;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
313
  XRef *xref;
Hib Eris's avatar
Hib Eris committed
314
  SecurityHandler *secHdlr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
315
  Catalog *catalog;
Hib Eris's avatar
Hib Eris committed
316
  Hints *hints;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
317
318
319
#ifndef DISABLE_OUTLINE
  Outline *outline;
#endif
Hib Eris's avatar
Hib Eris committed
320
  Page **pageCache;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
321
322
323

  GBool ok;
  int errCode;
324
325
326
  //If there is an error opening the PDF file with fopen() in the constructor, 
  //then the POSIX errno will be here.
  int fopenErrno;
Hib Eris's avatar
Hib Eris committed
327
328

  Guint startXRefPos;		// offset of last xref table
Kristian Høgsberg's avatar
Kristian Høgsberg committed
329
330
331
};

#endif