TransformationSample.java :  » PDF » PDFClown-0.0.5 » it » stefanochizzolini » clown » samples » Java Open Source

Java Open Source » PDF » PDFClown 0.0.5 
PDFClown 0.0.5 » it » stefanochizzolini » clown » samples » TransformationSample.java
package it.stefanochizzolini.clown.samples;

import it.stefanochizzolini.clown.documents.Document;
import it.stefanochizzolini.clown.documents.Page;
import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
import it.stefanochizzolini.clown.documents.contents.composition.BlockFilter;
import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
import it.stefanochizzolini.clown.documents.contents.entities.Image;
import it.stefanochizzolini.clown.files.File;

import java.awt.Dimension;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
  This sample demonstrates how to spacially manipulate an image object.
*/
public class TransformationSample
  implements ISample
{
  // <static>
  // <fields>
  private static final float Margin = 36;
  // </fields>
  // </static>

  // <dynamic>
  // <interface>
  // <public>
  // <ISample>
  public void run(
    PDFClownSampleLoader loader
    )
  {
    // 1. PDF file instantiation.
    File file = new File();

    // 2. Content creation.
    Document document = file.getDocument();
    populate(
      document,
      loader
      );

    // (boilerplate metadata insertion -- ignore it)
    loader.buildAccessories(document,this.getClass(),"Transformation","graphics object transformation");

    // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
    loader.serialize(file,this.getClass().getSimpleName(),false);
  }
  // </ISample>
  // </public>

  // <private>
  /**
    Populates a PDF file with contents.
  */
  private void populate(
    Document document,
    PDFClownSampleLoader loader
    )
  {
    Page page = new Page(document);
    document.getPages().add(page);
    Dimension2D pageSize = page.getSize();

    PrimitiveFilter builder = new PrimitiveFilter(page);
    {
      BlockFilter blockFilter = new BlockFilter(builder);
      blockFilter.setHyphenation(true);
      blockFilter.begin(
        new Rectangle2D.Double(
          Margin,
          Margin,
          (float)pageSize.getWidth() - Margin * 2,
          (float)pageSize.getHeight() - Margin * 2
          ),
        AlignmentXEnum.Justify,
        AlignmentYEnum.Top
        );
      StandardType1Font bodyFont = new StandardType1Font(
        document,
        StandardType1Font.FamilyNameEnum.Courier,
        true,
        false
        );
      builder.setFont(bodyFont,32);
      blockFilter.showText("Transformation sample"); blockFilter.showBreak();
      builder.setFont(bodyFont,16);
      blockFilter.showText("Showing the GNU logo placed on the page center, rotated by 25 degrees clockwise.");
      blockFilter.end();
    }
    // Showing the 'GNU' image...
    {
      // Instantiate a jpeg image object!
      Image image = Image.get(
        loader.getInputPath() + java.io.File.separator + "images" + java.io.File.separator + "gnu.jpg"
        ); // Abstract image (entity).
      // Show the image!
      builder.showXObject(
        image.toXObject(document),
        new Point2D.Double(
          (float)pageSize.getWidth() / 2,
          (float)pageSize.getHeight() / 2
          ),
        new Dimension(0,0),
        AlignmentXEnum.Center,
        AlignmentYEnum.Middle,
        -25
        );
    }
    builder.flush();
  }
  // </private>
  // </interface>
  // </dynamic>
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.