package examples.rtf2fo;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import de.vc.jfo.Document;
import de.vc.jfo.attributes.Values;
import de.vc.jfo.env.JFOConfig;
import de.vc.jfo.flow.Block;
import de.vc.jfo.flow.BlockContainer;
import de.vc.jfo.flow.ExternalGraphic;
import de.vc.jfo.flow.StaticContent;
import de.vc.jfo.pagination.PageSequence;
import de.vc.jfo.renderer.OutputFormat;
import de.vc.jfo.rtf.RTFImporter;
import de.vc.util.Measure;

/**
 * This example shows you how to add static (footer or header) images
 * at a specific position.
 * <b>Note, images can be displaced due to evaluation message of JFO.</b>
 */
public class AddStaticImages {

  private final static String PATH      = "r:/working/support/kelan/";
  private final static String RTF_FILE  = PATH+"sample.rtf";
  private final static String IMAGE_FILE= PATH+"sample.gif";

  private final static String PDF_FILE  = PATH+"sample.pdf";
  private final static String FO_FILE   = PATH+"sample.fo";


  public static void main(String[] args) throws IOException{
    RTFImporter i = new RTFImporter();
    Document d = i.importDocument(RTF_FILE);

    // add an image relative to top left margin of header
    addImage(d, Values.REGION_BEFORE, IMAGE_FILE,
             new Measure("0cm"), // pos X
             new Measure("0cm"), // pos Y
             null,   // width of image
             null);  // height of image

    // add another image relative to top left margin of header
    addImage(d, Values.REGION_AFTER, IMAGE_FILE,
             new Measure("5cm"),    // pos X
             new Measure("-2cm"),    // pos Y
             new Measure("3cm"),     // width of image
             new Measure("0.5cm"));  // height of image

    // Enable XML output formatting
    JFOConfig.instance().setFormatXMLOutput(true);
    // Write XSL-FO file (of course you can skip this step)
    d.writeTo(FO_FILE);

    // Render the document into PDF
    d.render(OutputFormat.PDF, new File(PDF_FILE));
  }


  /**
   * Adds an image to a header or footer of a document.
   * @param d Document
   * @param region Region (Values.REGION_BEFORE or Values.REGION_AFTER).
   * @param imageFile String
   * @param posX Measure
   * @param posY Measure
   * @param width Measure
   * @param height Measure
   * @throws IOException
   */
  private static void addImage(
      Document d, Values.Region region,
      String imageFile,
      Measure posX, Measure posY,
      Measure width, Measure height) throws IOException{

    if (region != Values.REGION_BEFORE &&
        region != Values.REGION_AFTER)
      throw new IllegalArgumentException("Invalid Region");

    // Iterator over all page sequence
    Iterator it = d.getPageSequences().iterator();
    while (it.hasNext()){
      PageSequence ps = (PageSequence) it.next();

      // get an existing or create a new static content for the region
      StaticContent staticContent = ps.getStaticContent(region);
      if (staticContent==null){
        staticContent = new StaticContent();
        ps.addStaticContent(region,staticContent);
      }

      // create a new absolute positioned block container and add it to
      // the static content
      BlockContainer blockContainer = new BlockContainer();
      staticContent.add(blockContainer);
      blockContainer.setAbsolutePosition(posX, posY);

      // for FOP compliance
      blockContainer.setAttribute("position","absolute");

      // set the size of the block container; if we don't know the size
      // of the image then reserve some space
      blockContainer.setSize(
          width==null ? new Measure("20cm") : width.add(new Measure("0.1cm")),
          height==null ? new Measure("20cm") : height.add(new Measure("0.1cm")) );

      // Now we just have to create the external graphic and add it to
      // the block container
      ExternalGraphic g = new ExternalGraphic(
          "url("+new File(imageFile).toURL().toExternalForm()+")");
      g.setContentSize(width,height);
      blockContainer.add(new Block(g));

    }
  }

}
