1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominik Seichter                                *
3  *   domseichter@web.de                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  *                                                                         *
20  *   In addition, as a special exception, the copyright holders give       *
21  *   permission to link the code of portions of this program with the      *
22  *   OpenSSL library under certain conditions as described in each         *
23  *   individual source file, and distribute linked combinations            *
24  *   including the two.                                                    *
25  *   You must obey the GNU General Public License in all respects          *
26  *   for all of the code used other than OpenSSL.  If you modify           *
27  *   file(s) with this exception, you may extend this exception to your    *
28  *   version of the file(s), but you are not obligated to do so.  If you   *
29  *   do not wish to do so, delete this exception statement from your       *
30  *   version.  If you delete this exception statement from all source      *
31  *   files in the program, then also delete it here.                       *
32  ***************************************************************************/
33 
34 #ifndef _PDF_IMMEDIATE_WRITER_H_
35 #define _PDF_IMMEDIATE_WRITER_H_
36 
37 #include "PdfDefines.h"
38 #include "PdfVecObjects.h"
39 #include "PdfWriter.h"
40 
41 namespace PoDoFo {
42 
43 class PdfEncrypt;
44 class PdfOutputDevice;
45 class PdfXRef;
46 
47 /** A kind of PdfWriter that writes objects with streams immediately to
48  *  a PdfOutputDevice
49  */
50 class PODOFO_API PdfImmediateWriter : private PdfWriter,
51     private PdfVecObjects::Observer,
52     private PdfVecObjects::StreamFactory {
53 
54  public:
55     /** Create a new PdfWriter that writes objects with streams immediately to a PdfOutputDevice
56      *
57      *  This has the advantage that large documents can be created without
58      *  having to keep the whole document in memory.
59      *
60      *  @param pDevice all stream streams are immediately written to this output device
61      *                 while the document is created.
62      *  @param pVecObjects a vector of objects containing the objects which are written to disk
63      *  @param pTrailer the trailer object
64      *  @param eVersion the PDF version of the document to write.
65      *                      The PDF version can only be set in the constructor
66      *                      as it is the first item written to the document on disk.
67      *  @param pEncrypt pointer to an encryption object or NULL. If not NULL
68      *                  the PdfEncrypt object will be copied and used to encrypt the
69      *                  created document.
70      *  @param eWriteMode additional options for writing the pdf
71      */
72     PdfImmediateWriter( PdfOutputDevice* pDevice, PdfVecObjects* pVecObjects, const PdfObject* pTrailer,
73                         EPdfVersion eVersion = ePdfVersion_1_5, PdfEncrypt* pEncrypt = NULL,
74                         EPdfWriteMode eWriteMode = ePdfWriteMode_Default );
75 
76     ~PdfImmediateWriter();
77 
78     /** Get the write mode used for wirting the PDF
79      *  \returns the write mode
80      */
81     inline EPdfWriteMode GetWriteMode() const;
82 
83     /** Get the PDF version of the document
84      *  The PDF version can only be set in the constructor
85      *  as it is the first item written to the document on disk
86      *
87      *  \returns EPdfVersion version of the pdf document
88      */
89     inline EPdfVersion GetPdfVersion() const;
90 
91  private:
92     void WriteObject( const PdfObject* pObject );
93 
94     /** Called when the PdfVecObjects we observer is deleted.
95      */
96     void ParentDestructed();
97 
98     /** Finish the PDF file.
99      *  I.e. write the XRef and close the output device.
100      */
101     void Finish();
102 
103     /** Called whenever appending to a stream is started.
104      *  \param pStream the stream object the user currently writes to.
105      */
106     void BeginAppendStream( const PdfStream* pStream );
107 
108     /** Called whenever appending to a stream has ended.
109      *  \param pStream the stream object the user currently writes to.
110      */
111     void EndAppendStream( const PdfStream* pStream );
112 
113     /** Creates a stream object
114      *
115      *  \param pParent parent object
116      *
117      *  \returns a new stream object
118      */
119     PdfStream* CreateStream( PdfObject* pParent );
120 
121     /** Assume the stream for the last object has
122      *  been written complete.
123      *  Therefore close the stream of the object
124      *  now so that the next object can be written
125      *  to disk
126      */
127     void FinishLastObject();
128 
129  private:
130     PdfVecObjects*   m_pParent;
131     PdfOutputDevice* m_pDevice;
132 
133     PdfXRef*         m_pXRef;
134     PdfObject*       m_pLast;
135 
136     bool             m_bOpenStream;
137 };
138 
139 // -----------------------------------------------------
140 //
141 // -----------------------------------------------------
GetWriteMode()142 inline EPdfWriteMode PdfImmediateWriter::GetWriteMode() const
143 {
144     return PdfWriter::GetWriteMode();
145 }
146 
147 // -----------------------------------------------------
148 //
149 // -----------------------------------------------------
GetPdfVersion()150 inline EPdfVersion PdfImmediateWriter::GetPdfVersion() const
151 {
152     return PdfWriter::GetPdfVersion();
153 }
154 
155 };
156 
157 #endif /* _PDF_IMMEDIATE_WRITER_H_ */
158 
159