1 /***************************************************************************
2  *   Copyright (C) 2007 by Pierre Marchand   *
3  *   pierre@moulindetouvois.com   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (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 General Public License     *
16  *   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 #ifndef PDFTRANSLATOR_H
21 #define PDFTRANSLATOR_H
22 
23 #include "podofo.h"
24 #include "impositionplan.h"
25 
26 #include <string>
27 #include <map>
28 #include <set>
29 #include <vector>
30 #include <sstream>
31 #include <istream>
32 #include <string>
33 
34 
35 
36 // using namespace PoDoFo;
37 
38 namespace PoDoFo
39 {
40 	namespace Impose
41 	{
42 
43 /**
44 PdfTranslator create a new PDF file which is the imposed version, following the imposition
45 plan provided by the user, of the source PDF file.
46 Pdftranslate does not really create a new PDF doc, it rather works on source doc, getting all page contents
47 as XObjects and put these XObjects on new pages. At the end, it removes original pages from the doc, but since
48 PoDoFo keeps them --- just removing from the pages tree ---, if it happens that you have a lot of content
49 in content stream rather than in resources, you'll get a huge file.
50 Usage is something like :
51 p = new PdfTranslator;
52 p->setSource("mydoc.pdf");
53 p->setTarget("myimposeddoc.pdf");
54 p->loadPlan("in4-32p.plan");
55 p->impose();
56 p->mailItToMyPrinterShop("job@proprint.com");//Would be great, doesn't it ?
57 */
58 class PdfTranslator
59 {
60 	public:
61 		PdfTranslator();
62 
~PdfTranslator()63 		~PdfTranslator() { }
64 
65 		PdfMemDocument *sourceDoc;
66 		PdfMemDocument *targetDoc;
67 
68 		/**
69 		Set the source document(s) to be imposed.
70 		Argument source is the path of the PDF file, or the path of a file containing a list of paths of PDF files...
71 		*/
72 		void setSource ( const std::string & source );
73 
74 		/**
75 		Another way to set many files as source document.
76 		Note that a source must be set before you call addToSource().
77 		*/
78 		void addToSource ( const std::string & source );
79 
80 		/**
81 		Set the path of the file where the imposed PDF doc will be save.
82 		*/
83 		void setTarget ( const std::string & target );
84 
85 		/**
86 		Load an imposition plan file of form:
87 		widthOfSheet heightOfSheet
88 		sourcePage destPage rotation translationX translationY
89 		...        ...      ...      ...          ...
90 		*/
91 		void loadPlan ( const std::string & planFile , PoDoFo::Impose::PlanReader loader );
92 
93 		/**
94 		When all is prepared, call it to do the job.
95 		*/
96 		void impose();
97 
98 	private:
99 		std::string inFilePath;
100 		std::string outFilePath;
101 
102 		PdfReference globalResRef;
103 
104 		ImpositionPlan *planImposition;
105 
106 		std::map<int, PdfXObject*> xobjects;
107 		std::map<int,PdfObject*> resources;
108 		std::map<int, PdfRect> cropRect;
109 		std::map<int,PdfRect> bleedRect;
110 		std::map<int, PdfRect> trimRect;
111 		std::map<int,PdfRect> artRect;
112 		std::map<int, PdfDictionary*> pDict;
113 		std::map<int, int> virtualMap;
114 // 		int maxPageDest;
115 		int duplicate;
116 
117 		bool checkIsPDF ( std::string path );
118 		PdfObject* getInheritedResources ( PdfPage* page );
119 		void mergeResKey ( PdfObject *base, PdfName key,  PdfObject *tomerge );
120 		PdfObject* migrateResource(const PdfObject * obj);
121 		void drawLine ( double x, double y, double xx, double yy, std::ostringstream & a );
122 		void signature ( double x , double y, int sheet, const std::vector<int> & pages, std::ostringstream & a );
123 
124 		// An attempt to allow nested loops
125 		// returns new position in records list.
126 		int sortLoop(std::vector<std::string>& memfile, int numline);
127 
128 		std::string useFont;
129 		PdfReference useFontRef;
130 		double extraSpace;
131 
132 		std::vector<std::string> multiSource;
133 
134 		std::map<std::string, PdfObject*> migrateMap;
135 		std::set<PdfObject*> setMigrationPending;
136 	public:
137 		int pcount;
138 		double sourceWidth;
139 		double sourceHeight;
140 		double destWidth;
141 		double destHeight;
142 		double scaleFactor;
143 		std::string boundingBox;
144 
145 
146 };
147 
148 	};}; // end of namespace
149 #endif
150