Revision 613066356634 () - Diff

Link to this snippet: https://friendpaste.com/69voaS1et3ImpN2P2ymdYK
Embed:
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
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
COSStream cosGetStream = pageNode.cosGetStream();
PDPage page = (PDPage) PDPage.META.createFromCos(cosGetStream);
newNode.addNode(page);

CSCreator creator = CSCreator.createNew(page);
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;
}

}