PDFDoc.h 12.4 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>
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>
25
// Copyright (C) 2011, 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
26
// Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
27
// Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
28
// Copyright (C) 2013 Adam Reichold <adamreichold@myopera.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
29
// Copyright (C) 2013 Adrian Perez de Castro <aperez@igalia.com>
30 31 32 33 34 35
//
// 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
36 37 38 39 40 41 42
#ifndef PDFDOC_H
#define PDFDOC_H

#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif

43
#include "poppler-config.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
44
#include <stdio.h>
45
#include "goo/GooMutex.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
46 47 48 49
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
#include "Annot.h"
50
#include "OptionalContent.h"
51
#include "Stream.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
52 53

class GooString;
54
class GooFile;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
55 56 57 58 59 60
class BaseStream;
class OutputDev;
class Links;
class LinkAction;
class LinkDest;
class Outline;
Hib Eris's avatar
Hib Eris committed
61 62 63
class Linearization;
class SecurityHandler;
class Hints;
64
class StructTreeRoot;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
65

66 67 68 69 70 71
enum PDFWriteMode {
  writeStandard,
  writeForceRewrite,
  writeForceIncremental
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
72 73 74 75 76 77 78 79
//------------------------------------------------------------------------
// PDFDoc
//------------------------------------------------------------------------

class PDFDoc {
public:

  PDFDoc(GooString *fileNameA, GooString *ownerPassword = NULL,
80
	 GooString *userPassword = NULL, void *guiDataA = NULL);
81

82
#ifdef _WIN32
83 84 85 86
  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
87
  PDFDoc(BaseStream *strA, GooString *ownerPassword = NULL,
88
	 GooString *userPassword = NULL, void *guiDataA = NULL);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
89 90
  ~PDFDoc();

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

Kristian Høgsberg's avatar
Kristian Høgsberg committed
93 94 95 96 97 98
  // Was PDF document successfully opened?
  GBool isOk() { return ok; }

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

99 100 101 102
  // Get the error code returned by fopen() (if getErrorCode() == 
  // errOpenFile).
  int getFopenErrno() { return fopenErrno; }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
103 104
  // Get file name.
  GooString *getFileName() { return fileName; }
105 106 107
#ifdef _WIN32
  wchar_t *getFileNameU() { return fileNameU; }
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
108

Hib Eris's avatar
Hib Eris committed
109 110 111
  // Get the linearization table.
  Linearization *getLinearization();

Kristian Høgsberg's avatar
Kristian Høgsberg committed
112 113 114 115 116 117
  // Get the xref table.
  XRef *getXRef() { return xref; }

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

118 119 120
  // Get optional content configuration
  OCGs *getOptContentConfig() { return catalog->getOptContentConfig(); }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
121 122 123 124
  // Get base stream.
  BaseStream *getBaseStream() { return str; }

  // Get page parameters.
125
  double getPageMediaWidth(int page)
Hib Eris's avatar
Hib Eris committed
126
    { return getPage(page) ? getPage(page)->getMediaWidth() : 0.0 ; }
127
  double getPageMediaHeight(int page)
Hib Eris's avatar
Hib Eris committed
128
    { return getPage(page) ? getPage(page)->getMediaHeight() : 0.0 ; }
129
  double getPageCropWidth(int page)
Hib Eris's avatar
Hib Eris committed
130
    { return getPage(page) ? getPage(page)->getCropWidth() : 0.0 ; }
131
  double getPageCropHeight(int page)
Hib Eris's avatar
Hib Eris committed
132
    { return getPage(page) ? getPage(page)->getCropHeight() : 0.0 ; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
133
  int getPageRotate(int page)
Hib Eris's avatar
Hib Eris committed
134
    { return getPage(page) ? getPage(page)->getRotate() : 0 ; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
135 136

  // Get number of pages.
Hib Eris's avatar
Hib Eris committed
137
  int getNumPages();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
138 139 140 141 142 143

  // 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.
144
  StructTreeRoot *getStructTreeRoot() { return catalog->getStructTreeRoot(); }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
145

Hib Eris's avatar
Hib Eris committed
146 147 148
  // Get page.
  Page *getPage(int page);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
149
  // Display a page.
150 151 152
  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
153 154 155
		   GBool (*abortCheckCbk)(void *data) = NULL,
		   void *abortCheckCbkData = NULL,
                   GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
156
                   void *annotDisplayDecideCbkData = NULL, GBool copyXRef = gFalse);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
157 158 159 160

  // Display a range of pages.
  void displayPages(OutputDev *out, int firstPage, int lastPage,
		    double hDPI, double vDPI, int rotate,
161
		    GBool useMediaBox, GBool crop, GBool printing,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
162 163 164 165 166 167 168
		    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,
169 170
			double hDPI, double vDPI, int rotate, 
			GBool useMediaBox, GBool crop, GBool printing,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
171 172 173 174
			int sliceX, int sliceY, int sliceW, int sliceH,
			GBool (*abortCheckCbk)(void *data) = NULL,
			void *abortCheckCbkData = NULL,
                        GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
175
                        void *annotDisplayDecideCbkData = NULL, GBool copyXRef = gFalse);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
176 177 178 179 180

  // 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); }

181 182
  // Returns the links for the current page, transferring ownership to
  // the caller.
183
  Links *getLinks(int page);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
184 185 186

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

190 191 192 193
  // Process the links for a page.
  void processLinks(OutputDev *out, int page);


Kristian Høgsberg's avatar
Kristian Høgsberg committed
194 195
#ifndef DISABLE_OUTLINE
  // Return the outline object.
196
  Outline *getOutline();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
197 198 199 200 201 202 203 204
#endif

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

  // Check various permissions.
  GBool okToPrint(GBool ignoreOwnerPW = gFalse)
    { return xref->okToPrint(ignoreOwnerPW); }
205 206
  GBool okToPrintHighRes(GBool ignoreOwnerPW = gFalse)
    { return xref->okToPrintHighRes(ignoreOwnerPW); }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
207 208 209 210 211 212
  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); }
213 214 215 216 217 218 219
  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
220 221 222 223 224 225 226 227 228

  // 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.
229 230
  int getPDFMajorVersion() { return pdfMajorVersion; }
  int getPDFMinorVersion() { return pdfMinorVersion; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
231

232 233 234
  //Return the PDF ID in the trailer dictionary (if any).
  GBool getID(GooString *permanent_id, GooString *update_id);

235 236
  // Save one page with another name.
  int savePageAs(GooString *name, int pageNo);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
237
  // Save this file with another name.
238
  int saveAs(GooString *name, PDFWriteMode mode=writeStandard);
239
  // Save this file in the given output stream.
240
  int saveAs(OutStream *outStr, PDFWriteMode mode=writeStandard);
241
  // Save this file with another name without saving changes
242
  int saveWithoutChangesAs(GooString *name);
243
  // Save this file in the given output stream without saving changes
244
  int saveWithoutChangesAs(OutStream *outStr);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
245

246 247 248
  // Return a pointer to the GUI (XPDFCore or WinPDFCore object).
  void *getGUIData() { return guiData; }

249 250 251 252
  // 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
Thomas Freitag's avatar
Thomas Freitag committed
253
  Guint writePageObjects(OutStream *outStr, XRef *xRef, Guint numOffset, GBool combine = gFalse);
254 255
  static void writeObject (Object *obj, OutStream* outStr, XRef *xref, Guint numOffset, Guchar *fileKey,
                           CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
256
  static void writeHeader(OutStream *outStr, int major, int minor);
257 258

  // Ownership goes to the caller
259
  static Dict *createTrailerDict (int uxrefSize, GBool incrUpdate, Goffset startxRef,
Adrian Johnson's avatar
Adrian Johnson committed
260
                                  Ref *root, XRef *xRef, const char *fileName, Goffset fileSize);
261
  static void writeXRefTableTrailer (Dict *trailerDict, XRef *uxref, GBool writeAllEntries,
Adrian Johnson's avatar
Adrian Johnson committed
262
                                     Goffset uxrefOffset, OutStream* outStr, XRef *xRef);
263
  static void writeXRefStreamTrailer (Dict *trailerDict, XRef *uxref, Ref *uxrefStreamRef,
Adrian Johnson's avatar
Adrian Johnson committed
264
                                      Goffset uxrefOffset, OutStream* outStr, XRef *xRef);
265

Kristian Høgsberg's avatar
Kristian Høgsberg committed
266
private:
267 268 269
  // 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);
270 271
  static void writeDictionnary (Dict* dict, OutStream* outStr, XRef *xRef, Guint numOffset, Guchar *fileKey,
                                CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
272

273
  // Write object header to current file stream and return its offset
274
  static Goffset writeObjectHeader (Ref *ref, OutStream* outStr);
275 276
  static void writeObjectFooter (OutStream* outStr);

277 278 279 280 281 282
  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); }
283 284
  static void writeStream (Stream* str, OutStream* outStr);
  static void writeRawStream (Stream* str, OutStream* outStr);
285
  void writeXRefTableTrailer (Goffset uxrefOffset, XRef *uxref, GBool writeAllEntries,
286
                              int uxrefSize, OutStream* outStr, GBool incrUpdate);
287 288
  static void writeString (GooString* s, OutStream* outStr, Guchar *fileKey,
                           CryptAlgorithm encAlgorithm, int keyLength, int objNum, int objGen);
289 290 291
  void saveIncrementalUpdate (OutStream* outStr);
  void saveCompleteRewrite (OutStream* outStr);

Hib Eris's avatar
Hib Eris committed
292 293 294 295 296
  Page *parsePage(int page);

  // Get hints.
  Hints *getHints();

Hib Eris's avatar
Hib Eris committed
297
  PDFDoc();
Hib Eris's avatar
Hib Eris committed
298
  void init();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
299
  GBool setup(GooString *ownerPassword, GooString *userPassword);
300
  GBool checkFooter();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
301
  void checkHeader();
302
  GBool checkEncryption(GooString *ownerPassword, GooString *userPassword);
Hib Eris's avatar
Hib Eris committed
303
  // Get the offset of the start xref table.
Adrian Johnson's avatar
Adrian Johnson committed
304
  Goffset getStartXRef();
Hib Eris's avatar
Hib Eris committed
305 306
  // Get the offset of the entries in the main XRef table of a
  // linearized document (0 for non linearized documents).
Adrian Johnson's avatar
Adrian Johnson committed
307 308
  Goffset getMainXRefEntriesOffset();
  long long strToLongLong(char *s);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
309 310

  GooString *fileName;
311 312 313
#ifdef _WIN32
  wchar_t *fileNameU;
#endif
314
  GooFile *file;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
315
  BaseStream *str;
316
  void *guiData;
317 318
  int pdfMajorVersion;
  int pdfMinorVersion;
Hib Eris's avatar
Hib Eris committed
319
  Linearization *linearization;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
320
  XRef *xref;
Hib Eris's avatar
Hib Eris committed
321
  SecurityHandler *secHdlr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
322
  Catalog *catalog;
Hib Eris's avatar
Hib Eris committed
323
  Hints *hints;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
324 325 326
#ifndef DISABLE_OUTLINE
  Outline *outline;
#endif
Hib Eris's avatar
Hib Eris committed
327
  Page **pageCache;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
328 329 330

  GBool ok;
  int errCode;
331 332 333
  //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
334

Adrian Johnson's avatar
Adrian Johnson committed
335
  Goffset startXRefPos;		// offset of last xref table
336 337 338
#if MULTITHREADED
  GooMutex mutex;
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
339 340 341
};

#endif