Revision 353766393836 () - Diff

Link to this snippet: https://friendpaste.com/3YzlS3bvTcXEPK3FZkl7nb
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
import java.io.IOException;

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.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.PDPageTree;
import de.intarsys.tools.locator.FileLocator;

public class SpeedTestJpod {

/**
* @param args
* @throws IOException
* @throws FontFactoryException
* @throws COSLoadException
*/
public static void main(String[] args) throws IOException,
FontFactoryException, COSLoadException {

PDDocument pdDocument = PDDocument.createFromLocator(new FileLocator("input_test.pdf"));
FileLocator locator = new FileLocator("output_test.pdf");

PDPageTree pageTree = pdDocument.getPageTree();


PDForm form = createForm();

markPages(form, pageTree, pdDocument, locator);

pdDocument.save(locator, null);

}

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 static void markPages(PDForm form, PDPageTree pageTree,
PDDocument pdDocument, FileLocator locatorOut)
throws IOException {
for (int i = 0;; i++) {

float formWidth = form.getBoundingBox().getWidth();
// --> don't use getPageAt - its a tree
CDSRectangle rect = pageTree.getPageAt(i).getCropBox();
float scale = 1f;
float offsetY = (rect.getHeight() - (formWidth * scale)) / 2;
float offsetX = (rect.getWidth() - (formWidth * scale)) / 2;

CSContent content = CSContent.createNew();

// --> why look up again?
// --> why parse the content
CSCreator creator = CSCreator.createFromContent(content,
pageTree.getPageAt(i));

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

// --> why look up again?
// --> why change the input document?
pageTree.getPageAt(i).cosAddContents(content.createStream());

PDPage page = (PDPage) PDPage.META.createNew();
page.setContentStream(content);
// pdDocument.addPageNode(pageAt);
// --> why creating an inefficent output tree?
pdDocument.getPageTree().addNode(i, page);
if (i % 10000 == 0) {
System.out.println(i);
pdDocument.save(locatorOut, null);
}
}

}

}