1 //========================================================================
2 //
3 // PDFDoc.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
16 // Copyright (C) 2005, 2006, 2008 Brad Hards <bradh@frogmouth.net>
17 // Copyright (C) 2005, 2009 Albert Astals Cid <aacid@kde.org>
18 // Copyright (C) 2008 Julien Rebetez <julienr@svn.gnome.org>
19 // Copyright (C) 2008 Pino Toscano <pino@kde.org>
20 // 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>
23 // Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
24 // Copyright (C) 2010 Srinivas Adicherla <srinivas.adicherla@geodesic.com>
25 //
26 // To see a description of the changes please see the Changelog file that
27 // came with your tarball or type make ChangeLog if you are building from git
28 //
29 //========================================================================
30 
31 #ifndef PDFDOC_H
32 #define PDFDOC_H
33 
34 #ifdef USE_GCC_PRAGMAS
35 #pragma interface
36 #endif
37 
38 #include <stdio.h>
39 #include "XRef.h"
40 #include "Catalog.h"
41 #include "Page.h"
42 #include "Annot.h"
43 #include "OptionalContent.h"
44 
45 class GooString;
46 class BaseStream;
47 class OutputDev;
48 class Links;
49 class LinkAction;
50 class LinkDest;
51 class Outline;
52 class Linearization;
53 class SecurityHandler;
54 class Hints;
55 
56 enum PDFWriteMode {
57   writeStandard,
58   writeForceRewrite,
59   writeForceIncremental
60 };
61 
62 //------------------------------------------------------------------------
63 // PDFDoc
64 //------------------------------------------------------------------------
65 
66 class PDFDoc {
67 public:
68 
69   PDFDoc(GooString *fileNameA, GooString *ownerPassword = NULL,
70 	 GooString *userPassword = NULL, void *guiDataA = NULL);
71 
72 #ifdef _WIN32
73   PDFDoc(wchar_t *fileNameA, int fileNameLen, GooString *ownerPassword = NULL,
74 	 GooString *userPassword = NULL, void *guiDataA = NULL);
75 #endif
76 
77   PDFDoc(BaseStream *strA, GooString *ownerPassword = NULL,
78 	 GooString *userPassword = NULL, void *guiDataA = NULL);
79   ~PDFDoc();
80 
81   static PDFDoc *ErrorPDFDoc(int errorCode, GooString *fileNameA = NULL);
82 
83   // Was PDF document successfully opened?
isOk()84   GBool isOk() { return ok; }
85 
86   // Get the error code (if isOk() returns false).
getErrorCode()87   int getErrorCode() { return errCode; }
88 
89   // Get the error code returned by fopen() (if getErrorCode() ==
90   // errOpenFile).
getFopenErrno()91   int getFopenErrno() { return fopenErrno; }
92 
93   // Get file name.
getFileName()94   GooString *getFileName() { return fileName; }
95 
96   // Get the linearization table.
97   Linearization *getLinearization();
98 
99   // Get the xref table.
getXRef()100   XRef *getXRef() { return xref; }
101 
102   // Get catalog.
getCatalog()103   Catalog *getCatalog() { return catalog; }
104 
105   // Get optional content configuration
getOptContentConfig()106   OCGs *getOptContentConfig() { return catalog->getOptContentConfig(); }
107 
108   // Get base stream.
getBaseStream()109   BaseStream *getBaseStream() { return str; }
110 
111   // Get page parameters.
getPageMediaWidth(int page)112   double getPageMediaWidth(int page)
113     { return getPage(page) ? getPage(page)->getMediaWidth() : 0.0 ; }
getPageMediaHeight(int page)114   double getPageMediaHeight(int page)
115     { return getPage(page) ? getPage(page)->getMediaHeight() : 0.0 ; }
getPageCropWidth(int page)116   double getPageCropWidth(int page)
117     { return getPage(page) ? getPage(page)->getCropWidth() : 0.0 ; }
getPageCropHeight(int page)118   double getPageCropHeight(int page)
119     { return getPage(page) ? getPage(page)->getCropHeight() : 0.0 ; }
getPageRotate(int page)120   int getPageRotate(int page)
121     { return getPage(page) ? getPage(page)->getRotate() : 0 ; }
122 
123   // Get number of pages.
124   int getNumPages();
125 
126   // Return the contents of the metadata stream, or NULL if there is
127   // no metadata.
readMetadata()128   GooString *readMetadata() { return catalog->readMetadata(); }
129 
130   // Return the structure tree root object.
getStructTreeRoot()131   Object *getStructTreeRoot() { return catalog->getStructTreeRoot(); }
132 
133   // Get page.
134   Page *getPage(int page);
135 
136   // Display a page.
137   void displayPage(OutputDev *out, int page,
138 		   double hDPI, double vDPI, int rotate,
139 		   GBool useMediaBox, GBool crop, GBool printing,
140 		   GBool (*abortCheckCbk)(void *data) = NULL,
141 		   void *abortCheckCbkData = NULL,
142                    GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
143                    void *annotDisplayDecideCbkData = NULL);
144 
145   // Display a range of pages.
146   void displayPages(OutputDev *out, int firstPage, int lastPage,
147 		    double hDPI, double vDPI, int rotate,
148 		    GBool useMediaBox, GBool crop, GBool printing,
149 		    GBool (*abortCheckCbk)(void *data) = NULL,
150 		    void *abortCheckCbkData = NULL,
151                     GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
152                     void *annotDisplayDecideCbkData = NULL);
153 
154   // Display part of a page.
155   void displayPageSlice(OutputDev *out, int page,
156 			double hDPI, double vDPI, int rotate,
157 			GBool useMediaBox, GBool crop, GBool printing,
158 			int sliceX, int sliceY, int sliceW, int sliceH,
159 			GBool (*abortCheckCbk)(void *data) = NULL,
160 			void *abortCheckCbkData = NULL,
161                         GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL,
162                         void *annotDisplayDecideCbkData = NULL);
163 
164   // Find a page, given its object ID.  Returns page number, or 0 if
165   // not found.
findPage(int num,int gen)166   int findPage(int num, int gen) { return catalog->findPage(num, gen); }
167 
168   // Returns the links for the current page, transferring ownership to
169   // the caller.
170   Links *getLinks(int page);
171 
172   // Find a named destination.  Returns the link destination, or
173   // NULL if <name> is not a destination.
findDest(GooString * name)174   LinkDest *findDest(GooString *name)
175     { return catalog->findDest(name); }
176 
177   // Process the links for a page.
178   void processLinks(OutputDev *out, int page);
179 
180 
181 #ifndef DISABLE_OUTLINE
182   // Return the outline object.
183   Outline *getOutline();
184 #endif
185 
186   // Is the file encrypted?
isEncrypted()187   GBool isEncrypted() { return xref->isEncrypted(); }
188 
189   // Check various permissions.
190   GBool okToPrint(GBool ignoreOwnerPW = gFalse)
191     { return xref->okToPrint(ignoreOwnerPW); }
192   GBool okToPrintHighRes(GBool ignoreOwnerPW = gFalse)
193     { return xref->okToPrintHighRes(ignoreOwnerPW); }
194   GBool okToChange(GBool ignoreOwnerPW = gFalse)
195     { return xref->okToChange(ignoreOwnerPW); }
196   GBool okToCopy(GBool ignoreOwnerPW = gFalse)
197     { return xref->okToCopy(ignoreOwnerPW); }
198   GBool okToAddNotes(GBool ignoreOwnerPW = gFalse)
199     { return xref->okToAddNotes(ignoreOwnerPW); }
200   GBool okToFillForm(GBool ignoreOwnerPW = gFalse)
201     { return xref->okToFillForm(ignoreOwnerPW); }
202   GBool okToAccessibility(GBool ignoreOwnerPW = gFalse)
203     { return xref->okToAccessibility(ignoreOwnerPW); }
204   GBool okToAssemble(GBool ignoreOwnerPW = gFalse)
205     { return xref->okToAssemble(ignoreOwnerPW); }
206 
207 
208   // Is this document linearized?
209   GBool isLinearized();
210 
211   // Return the document's Info dictionary (if any).
getDocInfo(Object * obj)212   Object *getDocInfo(Object *obj) { return xref->getDocInfo(obj); }
getDocInfoNF(Object * obj)213   Object *getDocInfoNF(Object *obj) { return xref->getDocInfoNF(obj); }
214 
215   // Return the PDF version specified by the file.
getPDFMajorVersion()216   int getPDFMajorVersion() { return pdfMajorVersion; }
getPDFMinorVersion()217   int getPDFMinorVersion() { return pdfMinorVersion; }
218 
219   //Return the PDF ID in the trailer dictionary (if any).
220   GBool getID(GooString *permanent_id, GooString *update_id);
221 
222   // Save this file with another name.
223   int saveAs(GooString *name, PDFWriteMode mode=writeStandard);
224   // Save this file in the given output stream.
225   int saveAs(OutStream *outStr, PDFWriteMode mode=writeStandard);
226   // Save this file with another name without saving changes
227   int saveWithoutChangesAs(GooString *name);
228   // Save this file in the given output stream without saving changes
229   int saveWithoutChangesAs(OutStream *outStr);
230 
231   // Return a pointer to the GUI (XPDFCore or WinPDFCore object).
getGUIData()232   void *getGUIData() { return guiData; }
233 
234 private:
235   // Add object to current file stream and return the offset of the beginning of the object
236   Guint writeObject (Object *obj, Ref *ref, OutStream* outStr);
237   void writeDictionnary (Dict* dict, OutStream* outStr);
238   void writeStream (Stream* str, OutStream* outStr);
239   void writeRawStream (Stream* str, OutStream* outStr);
240   void writeTrailer (Guint uxrefOffset, int uxrefSize, OutStream* outStr, GBool incrUpdate);
241   void writeString (GooString* s, OutStream* outStr);
242   void saveIncrementalUpdate (OutStream* outStr);
243   void saveCompleteRewrite (OutStream* outStr);
244 
245   Page *parsePage(int page);
246 
247   // Get hints.
248   Hints *getHints();
249 
250   PDFDoc();
251   void init();
252   GBool setup(GooString *ownerPassword, GooString *userPassword);
253   GBool checkFooter();
254   void checkHeader();
255   GBool checkEncryption(GooString *ownerPassword, GooString *userPassword);
256   // Get the offset of the start xref table.
257   Guint getStartXRef();
258   // Get the offset of the entries in the main XRef table of a
259   // linearized document (0 for non linearized documents).
260   Guint getMainXRefEntriesOffset();
261   Guint strToUnsigned(char *s);
262 
263   GooString *fileName;
264   FILE *file;
265   BaseStream *str;
266   void *guiData;
267   int pdfMajorVersion;
268   int pdfMinorVersion;
269   Linearization *linearization;
270   XRef *xref;
271   SecurityHandler *secHdlr;
272   Catalog *catalog;
273   Hints *hints;
274 #ifndef DISABLE_OUTLINE
275   Outline *outline;
276 #endif
277   Page **pageCache;
278 
279   GBool ok;
280   int errCode;
281   //If there is an error opening the PDF file with fopen() in the constructor,
282   //then the POSIX errno will be here.
283   int fopenErrno;
284 
285   Guint startXRefPos;		// offset of last xref table
286 };
287 
288 #endif
289