01: package it.stefanochizzolini.clown.samples;
02:
03: import it.stefanochizzolini.clown.documents.Document;
04: import it.stefanochizzolini.clown.documents.Page;
05: import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor;
06: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
07: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
08: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
09: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
10: import it.stefanochizzolini.clown.files.File;
11: import it.stefanochizzolini.clown.tokens.FileFormatException;
12: import it.stefanochizzolini.clown.tools.PageStamper;
13:
14: import java.awt.geom.Dimension2D;
15: import java.awt.geom.Point2D;
16:
17: /**
18: This sample demonstrates how to stamp the page number on alternated corners of an existing
19: document's pages.
20: <h3>Remarks</h3>
21: <p>Stamping is just one of the several ways PDF contents can be manipulated using PDF Clown:
22: contents can be inserted as (raw) data chunks, mid-level content objects, external forms,
23: etc.</p>
24: */
25: public class PageNumberingSample implements ISample {
26: public void run(PDFClownSampleLoader loader) {
27: // (boilerplate user choice -- ignore it)
28: String filePath = loader
29: .getPdfFileChoice("Please select a PDF file");
30:
31: // 1. Open the PDF file!
32: File file;
33: try {
34: file = new File(filePath);
35: } catch (FileFormatException e) {
36: throw new RuntimeException(filePath
37: + " file has a bad file format.", e);
38: } catch (Exception e) {
39: throw new RuntimeException(
40: filePath + " file access error.", e);
41: }
42:
43: // Get the PDF document!
44: Document document = file.getDocument();
45:
46: // 2. Stamp the document!
47: stamp(document);
48:
49: // (boilerplate metadata insertion -- ignore it)
50: loader.buildAccessories(document, this .getClass(),
51: "Page numbering", "numbering a document's pages");
52:
53: // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
54: loader.serialize(file, this .getClass().getSimpleName());
55: }
56:
57: private void stamp(Document document) {
58: // 1. Instantiate the stamper!
59: /* NOTE: The PageStamper is optimized for dealing with pages. */
60: PageStamper stamper = new PageStamper();
61:
62: // 2. Numbering each page...
63: StandardType1Font font = new StandardType1Font(document,
64: StandardType1Font.FamilyNameEnum.Courier, true, false);
65: DeviceRGBColor redColor = new DeviceRGBColor(1, 0, 0);
66: int margin = 32;
67: for (Page page : document.getPages()) {
68: // 2.1. Associate the page to the stamper!
69: stamper.setPage(page);
70:
71: // 2.2. Stamping the page number on the foreground...
72: {
73: PrimitiveFilter foreground = stamper.getForeground();
74:
75: foreground.setFont(font, 16);
76: foreground.setFillColor(redColor);
77:
78: Dimension2D pageSize = page.getSize();
79: int pageNumber = page.getIndex() + 1;
80: boolean pageIsEven = (pageNumber % 2 == 0);
81: foreground.showText(Integer.toString(pageNumber),
82: new Point2D.Double((pageIsEven ? margin
83: : pageSize.getWidth() - margin),
84: pageSize.getHeight() - margin),
85: (pageIsEven ? AlignmentXEnum.Left
86: : AlignmentXEnum.Right),
87: AlignmentYEnum.Bottom, 0);
88: }
89:
90: // 2.3. End the stamping!
91: stamper.flush();
92: }
93: }
94: }
|