Tips For Success

Having the best tools for document imaging is only half of the equation. Our Tech Tips are designed to provide you with valuable information to help succeed by achieving faster results with RasterMaster SDKs or FlexSnap viewers. New time-saving tips are added monthly - you can receive them through Imaging News or our Tech Tips RSS feed.

An Object-Oriented Approach to Copying and Reordering Pages in a Document

This Tech Tip first appeared in Imaging News March 2006.

If you have a requirement to reorder pages within a document, or to create a new document containing pages from multiple existing documents, here’s an easy approach.

The first step is to create a class that represents each source page. I’ve called it com.snowbound.sample.PageReference. It only needs to store the name of the source document, and the page Index of the desired page.

package com.snowbound.sample;

public class PageReference
  {
      private int pageIndex;
      private String fileName;

    public PageReference (String file, int index)
      {
         pageIndex = index;
         fileName = file;
      }

    /**
       * @return Returns the fileName.
       */
      public String getFileName()
      {
         return fileName;
      }

   /**
      * @return Returns the pageIndex.
      */
      public int getPageIndex()
      {
         return pageIndex;
      }

}

The next class to create is an extension of java.util.Vector. I’ve named it com.snowbound.sample.DocumentModel, since it models the layout of the document that we are creating. This class only has one method, writeDocument, in addition to the inherited methods of Vector. It uses a RasterMaster for Java imaging SDK Snow.Snowbnd object to handle the image processing and output.

package com.snowbound.sample; 

import java.util.Vector;
  import Snow.Snowbnd;

public class DocumentModel extends Vector
  {
      public void writeDocument(String filePath, int imageFormat)
      {
          Snowbnd snow = new Snowbnd();
         for
(int i = 0; i < size(); i++)
          {
              PageReference pageRef = (PageReference) elementAt(i);
              snow.IMG_decompress_bitmap(pageRef.getFileName(),
              pageRef.getPageIndex());
              snow.IMG_save_bitmap(filePath, imageFormat);
          }
      }
  }

Here’s a sample app that uses these two classes to create a new document made from pages of existing documents. In practice, the DocumentModel could be created using UI components and FileDialogs to define the input files, and the order of pages for the new document.

package com.snowbound.sample;

import Snow.Defines;

public class PageManipulationApp
  {

 public static void main (String args[])
  {
       DocumentModel model = new DocumentModel();
       model.addElement( new PageReference ("c:/imgs/splash.png", 0));
       model.add( new PageReference ("c:/imgs/snowbound.tif", 0));
       model.add( new PageReference ("c:/imgs/snowbound.tif", 1));
       model.add( new PageReference ("c:/imgs/photo.jpg", 0));
       model.add( new PageReference ("c:/imgs/.tif", 1));
    
model.writeDocument("c:/imgs/new.tif", Defines.TIFF_LZW);
  }
  }