Revision 363336313339 () - Diff

Link to this snippet: https://friendpaste.com/15GXISzN0XdOcwrApscxZK
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
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.StopWatch;

import de.intarsys.pdf.cds.CDSRectangle;
import de.intarsys.pdf.content.CSContent;
import de.intarsys.pdf.content.TextState;
import de.intarsys.pdf.content.common.CSCreator;
import de.intarsys.pdf.cos.COSArray;
import de.intarsys.pdf.cos.COSObject;
import de.intarsys.pdf.font.PDFont;
import de.intarsys.pdf.font.PDFontType1;
import de.intarsys.pdf.font.outlet.FontFactoryException;
import de.intarsys.pdf.parser.COSLoadException;
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.pd.PDXObject;
import de.intarsys.pdf.st.EnumWriteMode;
import de.intarsys.tools.locator.FileLocator;

public class SelfWaterMarkingJpod {


/**
* @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, -1);
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, int n) throws FontFactoryException, IOException, COSLoadException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
new SelfWaterMarkingJpod().run(i,j,k,l,m,n);
stopWatch.split();
stopWatch.stop();
return stopWatch;
}

private static final float halfSqrt2 = (float) (0.5 * Math.sqrt(2));
private int i;

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, 80);
creator.setLineWidth(2.0f);
creator.textSetTransform(halfSqrt2, halfSqrt2, -halfSqrt2, halfSqrt2,
80, 0);
creator.textSetRenderingMode(TextState.RENDERING_MODE_FILL_STROKE);
creator.textShow("COPIA CONFORME");
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, int stopAt) throws FontFactoryException, IOException, COSLoadException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String path = "output.pdf";
File file = new File(path);
if(file.exists()) {
FileUtils.forceDelete(file);
}
FileUtils.copyFile(new File("94951244_13336301894205_.pdf"),file);
FileLocator locator = new FileLocator(path);
PDDocument fromDocument = PDDocument.createFromLocator(locator);
try {
PDPageTree pageTree = fromDocument.getPageTree();
fromDocument.setWriteModeHint(EnumWriteMode.INCREMENTAL);

PDForm form = createForm();
i = 0;

navigateCosTree(pageTree, form, fromDocument);

} catch (Exception e) {
e.printStackTrace();
FileUtils.forceDelete(file);
} finally {
fromDocument.close();
fromDocument = null;
}
}
private PDPageNode navigateCosTree(PDPageNode node, PDForm form,PDDocument fromDocument) {
COSArray cosKids = getKidsIterator(node);
if(cosKids==null) {
return null;
}
Iterator iterrator = cosKids.iterator();
PDPageNode origPageNode = (PDPageNode) PDPageNode.META
.createFromCos((COSObject) iterrator.next());

PDPage page = origPageNode.getFirstPage();
navigateCosTree(origPageNode,form,fromDocument);
try {
if(true){
waterMarkThePage(page, form);
fromDocument.save();
}
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
return node;
}
private COSArray getKidsIterator(PDPageNode pageNode) {
return pageNode.cosGetField(PDPageTree.DK_Kids).asArray();
}

private void waterMarkThePage(PDPageNode origPageNode, PDForm form) throws IOException {
// TODO Auto-generated method stub
PDPage page = origPageNode.getFirstPage();
CSContent content = CSContent.createNew();
CSCreator creator = CSCreator.createFromContent(content, page);

CDSRectangle rect = page.getCropBox();

float formWidth = form.getBoundingBox().getWidth();
float scale = 1f;
float offsetY = (rect.getHeight() - (formWidth * scale)) / 2;
float offsetX = (rect.getWidth() - (formWidth * scale)) / 2;

creator.saveState();
creator.transform(scale, 0, 0, scale, offsetX, offsetY);
creator.doXObject(null, form);
creator.restoreState();
creator.close();

page.cosAddContents(content.createStream());


}

}