001: package it.stefanochizzolini.clown.samples;
002:
003: import it.stefanochizzolini.clown.documents.Document;
004: import it.stefanochizzolini.clown.documents.Page;
005: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
006: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
007: import it.stefanochizzolini.clown.documents.contents.composition.BlockFilter;
008: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
009: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
010: import it.stefanochizzolini.clown.documents.contents.entities.Image;
011: import it.stefanochizzolini.clown.files.File;
012:
013: import java.awt.Dimension;
014: import java.awt.geom.Dimension2D;
015: import java.awt.geom.Point2D;
016: import java.awt.geom.Rectangle2D;
017:
018: /**
019: This sample demonstrates how to embed an image object within a content stream.
020: <h3>Remarks</h3>
021: <p>Inline objects should be used sparingly, as they easily clutter content streams.</p>
022: <p>The alternative (and preferred) way to insert an image object is via external object
023: (XObject); its main advantage is to allow content reuse.</p>
024: */
025: public class InlineObjectSample implements ISample {
026: // <static>
027: // <fields>
028: private static final float Margin = 36;
029:
030: // </fields>
031: // </static>
032:
033: // <dynamic>
034: // <interface>
035: // <public>
036: // <ISample>
037: public void run(PDFClownSampleLoader loader) {
038: // 1. PDF file instantiation.
039: File file = new File();
040:
041: // 2. Content creation.
042: Document document = file.getDocument();
043: populate(document, loader);
044:
045: // (boilerplate metadata insertion -- ignore it)
046: loader.buildAccessories(document, this .getClass(),
047: "Inline image",
048: "embedding an image within a content stream");
049:
050: // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
051: loader.serialize(file, this .getClass().getSimpleName(), false);
052: }
053:
054: // </ISample>
055: // </public>
056:
057: // <private>
058: /**
059: Populates a PDF file with contents.
060: */
061: private void populate(Document document, PDFClownSampleLoader loader) {
062: Page page = new Page(document);
063: document.getPages().add(page);
064: Dimension2D pageSize = page.getSize();
065:
066: PrimitiveFilter builder = new PrimitiveFilter(page);
067: {
068: BlockFilter blockFilter = new BlockFilter(builder);
069: blockFilter.setHyphenation(true);
070: blockFilter.begin(new Rectangle2D.Double(Margin, Margin,
071: (float) pageSize.getWidth() - Margin * 2,
072: (float) pageSize.getHeight() - Margin * 2),
073: AlignmentXEnum.Justify, AlignmentYEnum.Top);
074: StandardType1Font bodyFont = new StandardType1Font(
075: document, StandardType1Font.FamilyNameEnum.Courier,
076: true, false);
077: builder.setFont(bodyFont, 32);
078: blockFilter.showText("Inline image sample");
079: blockFilter.showBreak();
080: builder.setFont(bodyFont, 16);
081: blockFilter
082: .showText("Showing the GNU logo as an inline image within the page content stream.");
083: blockFilter.end();
084: }
085: // Showing the 'GNU' image...
086: {
087: // Instantiate a jpeg image object!
088: Image image = Image.get(loader.getInputPath()
089: + java.io.File.separator + "images"
090: + java.io.File.separator + "gnu.jpg"); // Abstract image (entity).
091: builder.applyMatrix(200, 0, 0, 200,
092: (pageSize.getWidth() - 200) / 2, (pageSize
093: .getHeight() - 200) / 2);
094: //Show the image!
095: builder.add(image.toInlineObject());
096: }
097: builder.flush();
098: }
099: // </private>
100: // </interface>
101: // </dynamic>
102: }
|