4eJCjoFlAG8UP1p43DfbJw changeset

Changeset663964323165 (b)
ParentNone (a)
ab
0+import java.io.IOException;
0+import java.util.Iterator;
0+import java.util.List;
0+
0+import org.apache.commons.lang3.time.StopWatch;
0+import org.joda.time.DateTime;
0+import org.joda.time.Instant;
0+import org.joda.time.Interval;
0+
0+import de.intarsys.pdf.cds.CDSRectangle;
0+import de.intarsys.pdf.content.CSContent;
0+import de.intarsys.pdf.content.IContentStreamProvider;
0+import de.intarsys.pdf.content.TextState;
0+import de.intarsys.pdf.content.common.CSCreator;
0+import de.intarsys.pdf.cos.COSArray;
0+import de.intarsys.pdf.cos.COSBasedObject;
0+import de.intarsys.pdf.cos.COSCatalog;
0+import de.intarsys.pdf.cos.COSDictionary;
0+import de.intarsys.pdf.cos.COSObject;
0+import de.intarsys.pdf.cos.COSStream;
0+import de.intarsys.pdf.encoding.WinAnsiEncoding;
0+import de.intarsys.pdf.font.PDFont;
0+import de.intarsys.pdf.font.PDFontType1;
0+import de.intarsys.pdf.font.PDFontType3;
0+import de.intarsys.pdf.font.outlet.FontFactoryException;
0+import de.intarsys.pdf.font.outlet.FontOutlet;
0+import de.intarsys.pdf.font.outlet.FontQuery;
0+import de.intarsys.pdf.font.outlet.IFontFactory;
0+import de.intarsys.pdf.parser.COSLoadException;
0+import de.intarsys.pdf.pd.PDCollection;
0+import de.intarsys.pdf.pd.PDDocument;
0+import de.intarsys.pdf.pd.PDExtGState;
0+import de.intarsys.pdf.pd.PDForm;
0+import de.intarsys.pdf.pd.PDPage;
0+import de.intarsys.pdf.pd.PDPageNode;
0+import de.intarsys.pdf.pd.PDPageTree;
0+import de.intarsys.pdf.st.EnumWriteMode;
0+import de.intarsys.tools.locator.FileLocator;
0+import de.intarsys.tools.locator.ILocator;
0+
0+public class WaterMarkingJpod {
0+
0+
0+
0+        private PDFontType1 font;
0+
0+        /**
0+         * @param args
0+         * @throws IOException
0+         * @throws FontFactoryException
0+         * @throws COSLoadException
0+         */
0+        public static void main(String[] args) throws IOException,
0+                        FontFactoryException, COSLoadException {
0+                StopWatch runTestTime;
0+                int pages = 1000000;
0+                runTestTime = runTest(pages,100,1000,5000,5000);
0+                System.out.println("Total time: " +runTestTime.toSplitString()+ "   "+(pages*1.0d/runTestTime.getTime())*1000+" p/s");
0+
0+
0+        }
0+
0+        private static StopWatch runTest(int i, int j, int k, int l, int m) throws FontFactoryException, IOException, COSLoadException {
0+                StopWatch stopWatch = new StopWatch();
0+                stopWatch.start();
0+                new WaterMarkingJpod().run(i,j,k,l,m);
0+                stopWatch.split();
0+                stopWatch.stop();
0+                return stopWatch;
0+        }
0+
0+        private static final float halfSqrt2 = (float) (0.5 * Math.sqrt(2));
0+
0+        private static PDForm createForm() {
0+                PDForm form = (PDForm) PDForm.META.createNew();
0+                PDFont font = PDFontType1.createNew(PDFontType1.FONT_Helvetica);
0+                PDExtGState extGState = (PDExtGState) PDExtGState.META.createNew();
0+                extGState.setStrokingAlphaConstant(0.6f);
0+                extGState.setNonStrokingAlphaConstant(0.2f);
0+
0+                CSCreator creator = CSCreator.createNew(form);
0+                creator.textBegin();
0+                creator.setExtendedState(null, extGState);
0+                creator.setNonStrokeColorRGB(1.0f, 204f / 256f, 0); // orange
0+                creator.setStrokeColorRGB(1.0f, 204f / 256f, 0);
0+                creator.textSetFont(null, font, 100);
0+                creator.setLineWidth(2.0f);
0+                creator.textSetTransform(halfSqrt2, halfSqrt2, -halfSqrt2, halfSqrt2,
0+                                80, 0);
0+                creator.textSetRenderingMode(TextState.RENDERING_MODE_FILL_STROKE);
0+                creator.textShow("COPIA CONFORME"); //$NON-NLS-1$
0+                creator.textEnd();
0+                creator.close();
0+
0+                form.setBoundingBox(new CDSRectangle(0, 0, 600, 600));
0+
0+                return form;
0+        }
0+
0+       
0+       
0+        private void run(int iterations, int nodeEvery, int saveEvery, int sampleEvery, int resetEvery) throws FontFactoryException, IOException, COSLoadException {
0+                StopWatch stopWatch = new StopWatch();
0+                stopWatch.start();
0+
0+                PDDocument document = PDDocument.createNew();
0+                try {           
0+               
0+                        document.setAuthor(" S.p.A.");
0+                        document.setCreator("2pdf");
0+                        ILocator locator = new FileLocator("output_test.pdf");
0+                       
0+                        PDDocument fromDocument = PDDocument.createFromLocator(new FileLocator("input_test.pdf"));
0+
0+                        //PDPageNode node= fontFromDocument.getPageTree().getFirstNode();
0+                        PDPageTree pageTree = fromDocument.getPageTree();
0+                        COSArray cosKids = pageTree.cosGetField(PDPageTree.DK_Kids).asArray();
0+                       
0+                        Iterator iterrator = cosKids.iterator();
0+                       
0+
0+                       
0+                       
0+                        PDPageTree newNode = null;
0+                        PDFont font = getFont(document);
0+                       
0+                        PDForm form = createForm();
0+                       
0+                        for (int i = 0; i <= iterations; i++) {
0+
0+                                PDPageNode origPageNode = (PDPageNode) PDPageNode.META
0+                                                .createFromCos((COSObject) iterrator.next());
0+                               
0+                                document.setWriteModeHint(EnumWriteMode.INCREMENTAL);
0+                               
0+                                if(i % nodeEvery == 0) {
0+                                        newNode = (PDPageTree) PDPageTree.META.createNew();
0+                                        document.addPageNode(newNode);
0+                                       
0+                                }
0+                               
0+                                COSObject origPage = origPageNode.cosGetObject();
0+                                PDPageNode pageNode = (PDPageNode) PDPageTree.META.createFromCos(origPage);
0+//                              inlocator
0+                               
0+                                COSDictionary cosGetDict = pageNode.cosGetDict();
0+                               
0+                                cosGetDict = (COSDictionary) cosGetDict.copyDeep();
0+                               
0+                                PDPage page = (PDPage) PDPage.META.createFromCos(cosGetDict);
0+                                newNode.addNode(page);
0+
0+                                CSCreator creator = CSCreator.createNew(page);
0+                                creator.close();
0+                               
0+                                if(i % sampleEvery == 0) {
0+                                        stopWatch.split();
0+                                        stopWatch.stop();
0+                                        long partTime = stopWatch.getTime();
0+                                        String a = (sampleEvery*1.0d/partTime)*1000+" p/s ";
0+                                        System.err.println(stopWatch.toSplitString()+" "+a);
0+                                        System.err.println(i/sampleEvery);
0+                                        stopWatch.reset();
0+                                        stopWatch.start();
0+                                }
0+                                System.err.println(i);
0+                               
0+                                if(i % saveEvery == 0) {
0+                                        document.save(locator);
0+                                }
0+                                if(i % resetEvery == 0) {
0+                                        //System.out.println(document.cosGetDoc().stGetDoc().getXRefSection().getSize());
0+
0+                                        //System.gc();
0+                                        //document.close();
0+                                        //document = PDDocument.createFromLocator(new FileLocator("test.pdf"));
0+                                }
0+                               
0+                               
0+
0+                        }
0+
0+                } catch (Exception e) {
0+                        e.printStackTrace();
0+                } finally {
0+                        document.close();
0+                        document = null;
0+                }
0+        }
0+
0+        private void writeText(PDFont font, CSCreator creator,int kkk) {
0+                creator.textSetFont(null, font, 20f);
0+                creator.textLineMoveTo(1+(10*kkk), 1);
0+                creator.textShow("Hello, world.");
0+        }
0+
0+        private PDFont getFont(PDDocument document) throws FontFactoryException {
0+                //IFontFactory factory = FontOutlet.get().lookupFontFactory(document);
0+                //FontQuery fontquery = new FontQuery("Arial", "");
0+                //PDFont font = factory.getFont(fontquery);
0+                if(font==null)
0+                        font = PDFontType1.createNew(PDFontType1.FONT_Helvetica);
0+                return font;
0+        }
0+
0+}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
--- Revision None
+++ Revision 663964323165
@@ -0,0 +1,206 @@
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.lang3.time.StopWatch;
+import org.joda.time.DateTime;
+import org.joda.time.Instant;
+import org.joda.time.Interval;
+
+import de.intarsys.pdf.cds.CDSRectangle;
+import de.intarsys.pdf.content.CSContent;
+import de.intarsys.pdf.content.IContentStreamProvider;
+import de.intarsys.pdf.content.TextState;
+import de.intarsys.pdf.content.common.CSCreator;
+import de.intarsys.pdf.cos.COSArray;
+import de.intarsys.pdf.cos.COSBasedObject;
+import de.intarsys.pdf.cos.COSCatalog;
+import de.intarsys.pdf.cos.COSDictionary;
+import de.intarsys.pdf.cos.COSObject;
+import de.intarsys.pdf.cos.COSStream;
+import de.intarsys.pdf.encoding.WinAnsiEncoding;
+import de.intarsys.pdf.font.PDFont;
+import de.intarsys.pdf.font.PDFontType1;
+import de.intarsys.pdf.font.PDFontType3;
+import de.intarsys.pdf.font.outlet.FontFactoryException;
+import de.intarsys.pdf.font.outlet.FontOutlet;
+import de.intarsys.pdf.font.outlet.FontQuery;
+import de.intarsys.pdf.font.outlet.IFontFactory;
+import de.intarsys.pdf.parser.COSLoadException;
+import de.intarsys.pdf.pd.PDCollection;
+import de.intarsys.pdf.pd.PDDocument;
+import de.intarsys.pdf.pd.PDExtGState;
+import de.intarsys.pdf.pd.PDForm;
+import de.intarsys.pdf.pd.PDPage;
+import de.intarsys.pdf.pd.PDPageNode;
+import de.intarsys.pdf.pd.PDPageTree;
+import de.intarsys.pdf.st.EnumWriteMode;
+import de.intarsys.tools.locator.FileLocator;
+import de.intarsys.tools.locator.ILocator;
+
+public class WaterMarkingJpod {
+
+
+
+ private PDFontType1 font;
+
+ /**
+ * @param args
+ * @throws IOException
+ * @throws FontFactoryException
+ * @throws COSLoadException
+ */
+ public static void main(String[] args) throws IOException,
+ FontFactoryException, COSLoadException {
+ StopWatch runTestTime;
+ int pages = 1000000;
+ runTestTime = runTest(pages,100,1000,5000,5000);
+ System.out.println("Total time: " +runTestTime.toSplitString()+ " "+(pages*1.0d/runTestTime.getTime())*1000+" p/s");
+
+
+ }
+
+ private static StopWatch runTest(int i, int j, int k, int l, int m) throws FontFactoryException, IOException, COSLoadException {
+ StopWatch stopWatch = new StopWatch();
+ stopWatch.start();
+ new WaterMarkingJpod().run(i,j,k,l,m);
+ stopWatch.split();
+ stopWatch.stop();
+ return stopWatch;
+ }
+
+ private static final float halfSqrt2 = (float) (0.5 * Math.sqrt(2));
+
+ private static PDForm createForm() {
+ PDForm form = (PDForm) PDForm.META.createNew();
+ PDFont font = PDFontType1.createNew(PDFontType1.FONT_Helvetica);
+ PDExtGState extGState = (PDExtGState) PDExtGState.META.createNew();
+ extGState.setStrokingAlphaConstant(0.6f);
+ extGState.setNonStrokingAlphaConstant(0.2f);
+
+ CSCreator creator = CSCreator.createNew(form);
+ creator.textBegin();
+ creator.setExtendedState(null, extGState);
+ creator.setNonStrokeColorRGB(1.0f, 204f / 256f, 0); // orange
+ creator.setStrokeColorRGB(1.0f, 204f / 256f, 0);
+ creator.textSetFont(null, font, 100);
+ creator.setLineWidth(2.0f);
+ creator.textSetTransform(halfSqrt2, halfSqrt2, -halfSqrt2, halfSqrt2,
+ 80, 0);
+ creator.textSetRenderingMode(TextState.RENDERING_MODE_FILL_STROKE);
+ creator.textShow("COPIA CONFORME"); //$NON-NLS-1$
+ creator.textEnd();
+ creator.close();
+
+ form.setBoundingBox(new CDSRectangle(0, 0, 600, 600));
+
+ return form;
+ }
+
+
+
+ private void run(int iterations, int nodeEvery, int saveEvery, int sampleEvery, int resetEvery) throws FontFactoryException, IOException, COSLoadException {
+ StopWatch stopWatch = new StopWatch();
+ stopWatch.start();
+
+ PDDocument document = PDDocument.createNew();
+ try {
+
+ document.setAuthor(" S.p.A.");
+ document.setCreator("2pdf");
+ ILocator locator = new FileLocator("output_test.pdf");
+
+ PDDocument fromDocument = PDDocument.createFromLocator(new FileLocator("input_test.pdf"));
+
+ //PDPageNode node= fontFromDocument.getPageTree().getFirstNode();
+ PDPageTree pageTree = fromDocument.getPageTree();
+ COSArray cosKids = pageTree.cosGetField(PDPageTree.DK_Kids).asArray();
+
+ Iterator iterrator = cosKids.iterator();
+
+
+
+
+ PDPageTree newNode = null;
+ PDFont font = getFont(document);
+
+ PDForm form = createForm();
+
+ for (int i = 0; i <= iterations; i++) {
+
+ PDPageNode origPageNode = (PDPageNode) PDPageNode.META
+ .createFromCos((COSObject) iterrator.next());
+
+ document.setWriteModeHint(EnumWriteMode.INCREMENTAL);
+
+ if(i % nodeEvery == 0) {
+ newNode = (PDPageTree) PDPageTree.META.createNew();
+ document.addPageNode(newNode);
+
+ }
+
+ COSObject origPage = origPageNode.cosGetObject();
+ PDPageNode pageNode = (PDPageNode) PDPageTree.META.createFromCos(origPage);
+// inlocator
+
+ COSDictionary cosGetDict = pageNode.cosGetDict();
+
+ cosGetDict = (COSDictionary) cosGetDict.copyDeep();
+
+ PDPage page = (PDPage) PDPage.META.createFromCos(cosGetDict);
+ newNode.addNode(page);
+
+ CSCreator creator = CSCreator.createNew(page);
+ creator.close();
+
+ if(i % sampleEvery == 0) {
+ stopWatch.split();
+ stopWatch.stop();
+ long partTime = stopWatch.getTime();
+ String a = (sampleEvery*1.0d/partTime)*1000+" p/s ";
+ System.err.println(stopWatch.toSplitString()+" "+a);
+ System.err.println(i/sampleEvery);
+ stopWatch.reset();
+ stopWatch.start();
+ }
+ System.err.println(i);
+
+ if(i % saveEvery == 0) {
+ document.save(locator);
+ }
+ if(i % resetEvery == 0) {
+ //System.out.println(document.cosGetDoc().stGetDoc().getXRefSection().getSize());
+
+ //System.gc();
+ //document.close();
+ //document = PDDocument.createFromLocator(new FileLocator("test.pdf"));
+ }
+
+
+
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ document.close();
+ document = null;
+ }
+ }
+
+ private void writeText(PDFont font, CSCreator creator,int kkk) {
+ creator.textSetFont(null, font, 20f);
+ creator.textLineMoveTo(1+(10*kkk), 1);
+ creator.textShow("Hello, world.");
+ }
+
+ private PDFont getFont(PDDocument document) throws FontFactoryException {
+ //IFontFactory factory = FontOutlet.get().lookupFontFactory(document);
+ //FontQuery fontquery = new FontQuery("Arial", "");
+ //PDFont font = factory.getFont(fontquery);
+ if(font==null)
+ font = PDFontType1.createNew(PDFontType1.FONT_Helvetica);
+ return font;
+ }
+
+}