Hi,
I am a software developer for SITA (http://www.sita.com/) and we are
planning on using the SVG printer feature of Batik. We want to be able to
(without using a GUI):
1) Send the print job to any connected printer
2) Be able to set the DPI of the print job.
3) Be able to find what supported sizes the printer has, and be able to
scale to that page size if possible.
So we went ahead and modified the PrintTranscoder.java file
(see attachment).
We would like to know if you can incorporate our modifications and hopefully
the other support we are looking to have. If you have any questions or
concern, please don't hesitate to reply back to this email.
Sincerely,
--
Amarnauth Sukhu
package sita.ads.apc.batik;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.io.CharArrayReader;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintJobAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterResolution;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.ext.awt.RenderingHintsKeyExt;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.TranscodingHints;
import org.apache.batik.transcoder.keys.BooleanKey;
import org.apache.batik.transcoder.keys.LengthKey;
import org.apache.batik.transcoder.keys.StringKey;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
/**
* This code is a modification version of Apache Software Foundation (ASF), BATIK.
* <br/>The code from Batik PrintTranscoder.java class located in import org.apache.batik.transcoder.print.PrintTranscoder;
* <br/>Is completely copied over so we have acces to all private variables. Mainly the reason is we need to modify the public int print(Graphics _g, PageFormat pageFormat, int pageIndex) method which had access to local variables
* <br/>Units are in 1/72inch ...(int)Ex. If the units provided is in millimeters(mm) then the answer in pixel = (the value/0.352778mm)
* <ul>
* <li>(1/72)inch = 1 point. (1/72) is used when java is rendering to the printer. </li>
* <ul>1 inch = 25.4 millimeters </li>
* <ul>1 point = (25.4/72)= 0.352777778mm</li>
* <ul>x points = (value/0.352777778mm)</li>
* </ul>
*/
public class BatikPrintTranscoder extends SVGAbstractTranscoder implements PrintJobListener, Printable{
public static final String KEY_AOI_STR = "aoi";
public static final String KEY_HEIGHT_STR = "height";
public static final String KEY_LANGUAGE_STR = "language";
public static final String KEY_MARGIN_BOTTOM_STR = "marginBottom";
public static final String KEY_MARGIN_LEFT_STR = "marginLeft";
public static final String KEY_MARGIN_RIGHT_STR = "marginRight";
public static final String KEY_MARGIN_TOP_STR = "marginTop";
public static final String KEY_PAGE_HEIGHT_STR = "pageHeight";
public static final String KEY_PAGE_ORIENTATION_STR = "pageOrientation";
public static final String KEY_PAGE_WIDTH_STR = "pageWidth";
public static final String KEY_PIXEL_TO_MM_STR = "pixelToMm";
public static final String KEY_SCALE_TO_PAGE_STR = "scaleToPage";
public static final String KEY_SHOW_PAGE_DIALOG_STR = "showPageDialog";
public static final String KEY_SHOW_PRINTER_DIALOG_STR = "showPrinterDialog";
public static final String KEY_USER_STYLESHEET_URI_STR = "userStylesheet";
public static final String KEY_WIDTH_STR = "width";
public static final String KEY_XML_PARSER_CLASSNAME_STR = "xmlParserClassName";
public static final String VALUE_MEDIA_PRINT = "print";
public static final String VALUE_PAGE_ORIENTATION_LANDSCAPE = "landscape";
public static final String VALUE_PAGE_ORIENTATION_PORTRAIT = "portrait";
public static final String VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE = "reverseLandscape";
/** Index of the page corresponding to root. */
private int curIndex = -1;
/**
* Place to cache BridgeContext so we can dispose of it when
* it is appropriate. The Base class would dispose of it too
* soon.
*/
private Vector<BridgeContext> theCtx;
/**
* Transform needed to render the current area of interest
*/
private Vector<AffineTransform> theCurTxf;
/** The X position where printing will begin in 1/72 inch. */
private int startPrintPointX = 0;
/** The Y position where printing will begin in 1/72 inch. */
private int startPrintPointY = 0;
/** (1/72)inch to a millimeter. When java converts the pixels to the device, it uses 72 as the base conversion. */
private final float inchtomm = 0.35277777777777777777777777777778f;//.0.352777778f;
/** The print service of the desired printer to use. */
private PrintService printerToUse;
/** The name of the document to print.*/
private String documentPrintName;
/** The owner of this job. */
private BatikSVGSpoolServerThread owner;
/** The logging.*/
final static Logger logger = Logger.getLogger(BatikPrintTranscoder.class);
public BatikPrintTranscoder(BatikSVGSpoolServerThread serverThread)
{
super();
theCtx = new Vector<BridgeContext>();
theCurTxf = new Vector<AffineTransform>();
hints.put(KEY_MEDIA, VALUE_MEDIA_PRINT);
setUnitInMillimeter(this.inchtomm);
setEnablePageAUTOScale(Boolean.FALSE);
this.owner = serverThread;
printerToUse = null;
//setPrinterToUser(printer);
}
/**
* Disables Batik auto scaling. Fixes Batik default print <tt>mm</tt> conversion size.
* @param printer The print service of the desired printer to use.
* @param serverThread The owner of this job.
*/
public BatikPrintTranscoder(PrintService printer, BatikSVGSpoolServerThread serverThread) {
super();
hints.put(KEY_MEDIA, VALUE_MEDIA_PRINT);
setUnitInMillimeter(this.inchtomm);
setEnablePageAUTOScale(Boolean.FALSE);
this.owner = serverThread;
setPrinterToUser(printer);
}
/*
public void setMargins()
{
try{
//hints.put(KEY_MARGIN_LEFT, convertMMtoUnitInch(20));
//hints.put(KEY_MARGIN_TOP, convertMMtoUnitInch(20));
//hints.put(KEY_MARGIN_RIGHT, convertMMtoUnitInch(20));
hints.put(KEY_MARGIN_BOTTOM, convertMMtoUnitInch(20));
}catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
logger.warn("Thread ID = (" +owner.getThreadID()+"). An error occured: \nSTART->\n" + stacktrace+"<-END");
}
}
*/
/**
* Adds the new job to the list of other jobs to be printed.
* The svg file would
* @param in The new job.
*/
public boolean addPrintJob(TranscoderInput in)
{
try {
super.transcode(in,null);
} catch (TranscoderException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
logger.warn("Thread ID = (" +owner.getThreadID()+"). An error occured: \nSTART->\n" + stacktrace+"<-END");
return false;
}
return true;
}
/**
* Transcodes the specified Document as an image in the specified output.
*
* @param document the document to transcode
* @param uri the uri of the document or null if any
* @param output the ouput where to transcode
* @exception TranscoderException if an error occured while transcoding
*/
protected void transcode(Document document, String uri, TranscoderOutput output) throws TranscoderException {
super.transcode(document, uri, output);
// We do this to hide 'ctx' from the SVGAbstractTranscoder
// otherwise it will dispose of the context before we can
// print the document.
theCtx.add(ctx);
theCurTxf.add(curTxf);
//super.root = null;
ctx = null;
}
/**
* Defines where on the paper the printing occurs.
* Units are in 1/72 inch.
*/
public void setPrintStartingPoint(int xPosition, int yPosition)
{
/*
if(xPosition < 0)
xPosition = 0;
if(yPosition < 0)
yPosition =0;
*/
this.startPrintPointX = xPosition;
this.startPrintPointY = yPosition;
logger.debug("The new pivot is (" + xPosition + "," + yPosition+")");
}
/**
* Sets what the value in mm is equal to a point. This is used for converting the svg doc to an image.
* @param mmValue The value that 1 point is equal to.
*/
public void setUnitInMillimeter(float mmValue)
{
super.addTranscodingHint(KEY_PIXEL_UNIT_TO_MILLIMETER, mmValue);
}
/**
* Converts the given length and convert it to (1/72) inches
* @param mmLength
* @return mm to 1/72 inches
*/
public float convertMMtoUnitInch(float mmLength)
{
if(mmLength==0)
return 0f;
Object obj = hints.get(KEY_PIXEL_UNIT_TO_MILLIMETER);
float solution = mmLength;
if(obj!=null)
solution = mmLength/((Float)obj).floatValue();
logger.debug(mmLength+"mm to (1/72)inch = " + solution);
return solution;
}
/**
* Setting the printer orientation
* @param isEnableAuto true = auto scale the document to print
*/
public void setEnablePageAUTOScale(Boolean isEnableAuto)
{
super.addTranscodingHint(KEY_SCALE_TO_PAGE, isEnableAuto);
}
/**
* Sets the orientation of the page
* @param orientation see the constants of this class
*/
public void setPageOrientation(String orientation)
{
super.addTranscodingHint(KEY_PAGE_ORIENTATION, orientation);
}
/**
* set the printer to use
* @param printer
*/
public void setPrinterToUser(PrintService printer)
{
this.printerToUse = printer;
}
/**
* set the printer to use
* @param printer
*/
public PrintService getPrinterToUser()
{
return this.printerToUse;
}
/**
* Initialization for printing from a file path
* @param documentName The job name
* @param filePath The file path to use
* @param pageOrientation The page orientation
* @return true = job passes the checks and is set for printing
*/
public boolean setJobFromFile(String documentName, String filePath, String pageOrientation)
{
this.documentPrintName = documentName;
setPageOrientation(pageOrientation);
boolean printIsBuilt = false;
try{
FileInputStream inputStream = new FileInputStream(filePath);
TranscoderInput tInput = new TranscoderInput(inputStream);
printIsBuilt = addPrintJob(tInput);
inputStream.close();
inputStream = null;
}
catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
logger.warn("Thread ID = (" +owner.getThreadID()+"). An error occured \nSTART->\n" + stacktrace+"<-END");
}
return printIsBuilt;
}
/**
* Initialization for printing from a SVG string
* @param documentName The job name
* @param svgString The SVG string to use
* @param pageOrientation The page orientation
* @return true = job passes the checks and is set for printing
*/
public boolean setJobFromString(String documentName, String svgString, String pageOrientation)
{
this.documentPrintName = documentName;
setPageOrientation(pageOrientation);
boolean printIsBuilt = false;
try{
Reader reader = new CharArrayReader(svgString.toCharArray());
TranscoderInput tInput = new TranscoderInput(reader);
printIsBuilt = addPrintJob(tInput);
}
catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
logger.warn("Thread ID = (" +owner.getThreadID()+"). An error occured \nSTART->\n" + stacktrace+"<-END");
}
return printIsBuilt;
}
/**
* Overrides Batik Default print method to add the additional information we require for print job.
* This includes with specifying the printer name, document name,
* @throws PrintException
* @throws Exception if there is a problem initializing the printer
*/
public void print() throws PrintException{
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
//aset.add(new Copies(1));
aset.add(new JobName(documentPrintName, Locale.getDefault()));
//aset.add(MediaSizeName.ISO_A4);
//aset.add(OrientationRequested.LANDSCAPE);
aset.add(PrintQuality.HIGH);
// changes the print resolution
//aset.add(new PrinterResolution(4000,4000,PrinterResolution.DPI));
//aset.add(javax.print.attribute.standard.ColorSupported.SUPPORTED);
//aset.add(javax.print.attribute.standard.Chromaticity.MONOCHROME);
//aset.add(new PageRanges(1,totalPages));
if(printerToUse==null)
throw new PrintException("No valid printer initialized.");
/* create a print job for the chosen service */
DocPrintJob pjob = printerToUse.createPrintJob();
PrintJobAttributeSet test = pjob.getAttributes();
for(int i=0;i<test.size(); i++)
{
System.out.println("Attribute: " + test.hashCode());
}
pjob.addPrintJobListener(this);
//PrintJobAttributeSet printerAttribures = pjob.getAttributes();
//MediaPrintableArea origional = (MediaPrintableArea) printerAttribures.get(MediaPrintableArea.class);
//System.out.println("document width = " + origional.getWidth(MediaPrintableArea.MM));
float x0 = 0; //left and right margin
float y0 = 0; //top and bottom margin.
float w0 = 7.988188976377952f; //Width 7.988188976377952, 3.244094488188976
float h0 = 3.244094488188976f; //Height
int units0 = MediaPrintableArea.INCH;
//aset.add(new MediaPrintableArea(x0, y0, 8.5f, 11f, units0));
//aset.add(MediaSize.Engineering.A);
//X must be greater than Y
//aset.add(new MediaSize(30,100, Size2DSyntax.MM));
//aset.add(new MediaPrintableArea(0, 0, 30,100, MediaPrintableArea.MM));
String pageOrientation = (String)hints.get(KEY_PAGE_ORIENTATION);
if(VALUE_PAGE_ORIENTATION_PORTRAIT.equalsIgnoreCase(pageOrientation)){
aset.add(OrientationRequested.PORTRAIT);
}
else if(VALUE_PAGE_ORIENTATION_LANDSCAPE.equalsIgnoreCase(pageOrientation)){
aset.add(OrientationRequested.LANDSCAPE);
}
else if(VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE.equalsIgnoreCase(pageOrientation)){
aset.add(OrientationRequested.REVERSE_LANDSCAPE);
}
//ATB size = 7.988188976377952, 3.244094488188976
//ATB (1/72 INCH) page W/page H: 575.1496062992126, 233.57480314960628
/*
* Create a Doc object to hold the print data.
*/
Doc doc = new SimpleDoc(this, flavor, null);
/* print the doc as specified */
//try {
pjob.print(doc, aset);
pjob = null;
//} catch (Exception e) {
// StringWriter sw = new StringWriter();
// e.printStackTrace(new PrintWriter(sw));
// String stacktrace = sw.toString();
// logger.warn("Thread ID = (" +owner.getThreadID()+"). An error occured: " + e.getMessage() +" \nSTART->\n" + stacktrace + "<-END.");
//}
}
public void cleanUpPrint()
{
if (theCtx.size() != 0)
{
while(theCtx.size()!=0)
{
theCtx.remove(0).dispose();
}
}
if(theCurTxf.size() != 0)
{
while(theCurTxf.size()!=0)
theCurTxf.remove(0);
}
if(ctx != null)
{
ctx.dispose();
ctx = null;
}
}
/**
* Printable implementation
* PageFormat - is ignored because we don't know what the page size would be
*/
public int print(Graphics _g, PageFormat pageFormat, int pageIndex){
//
// If we have already printed each page, return
//
if(pageIndex >= theCtx.size()){
curIndex = -1;
/*
if(theCtx.size() !=0)
{
while(theCtx.size()!=0)
theCtx.remove(0).dispose();
}
if(theCurTxf.size() != 0)
{
while(theCurTxf.size()!=0)
theCurTxf.remove(0);
}
*/
//userAgent.displayMessage("Printing Completed.");
return NO_SUCH_PAGE;
}
// The built print job
BridgeContext currentPrintJob = theCtx.get(pageIndex);
//
// Load a new document now if we are printing a new page
//
if(curIndex != pageIndex){
// The following call will invoke this class' transcode
// method which takes a document as an input. That method
// builds the GVT root tree.
width = (int)pageFormat.getImageableWidth();
height = (int)pageFormat.getImageableHeight();
curIndex = pageIndex;
}
// Cast to Graphics2D to access Java 2D features
Graphics2D g = (Graphics2D)_g;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING,
RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
// editing g's clipping view
//System.out.println("x = "+ pageFormat.getWidth() + " y = "+ pageFormat.getHeight());
//System.out.println("x = "+ theCtx.getDocumentSize().getWidth() + " y = "+ theCtx.getDocumentSize().getHeight());
//System.out.println("st x = "+ startPrintPointX + " st y = "+ startPrintPointY);
int documentWidth = (int)currentPrintJob.getDocumentSize().getWidth();
int documentHeight = (int)currentPrintJob.getDocumentSize().getHeight();
//System.out.println("document size w= "+ documentWidth + ", h = "+ documentWidth);
int newPageDocWidth=0;
int newPageDocHeight=0;
if(startPrintPointX>=0)
newPageDocWidth = documentWidth+startPrintPointX;
else
newPageDocWidth = documentWidth;
if(startPrintPointY>=0)
newPageDocHeight = documentHeight+startPrintPointY;
else
newPageDocHeight = documentHeight;
//System.out.println("new document size w= "+ newPageDocWidth + ", h = "+ newPageDocHeight);
//g.setClip(0,0,newPageDocWidth,newPageDocHeight);
g.setClip(startPrintPointX,startPrintPointY,documentWidth,documentHeight);
g.translate(startPrintPointX,startPrintPointY);
//pageFormat.getPaper().setImageableArea(0, 0, documentWidth, documentHeight);
/*
int modifyX=0, modifyY=0;
Float leftMargin = (Float)hints.get(KEY_MARGIN_LEFT);
Float topMargin = (Float)hints.get(KEY_MARGIN_TOP);
Float rightMargin = (Float)hints.get(KEY_MARGIN_RIGHT);
Float bottomMargin = (Float)hints.get(KEY_MARGIN_BOTTOM);
if(leftMargin != null){
modifyX += (int)leftMargin.floatValue();
documentWidth -= leftMargin.floatValue();
}
if(topMargin != null){
modifyY += (int)topMargin.floatValue();
documentHeight -= (int)topMargin.floatValue();
}
if(rightMargin != null){
documentWidth -= (int)rightMargin.floatValue();
}
if(bottomMargin != null){
documentHeight -= (int)bottomMargin.floatValue();
}
g.setClip(startPrintPointX,startPrintPointY,documentWidth,documentHeight);
g.translate(modifyX,modifyY);
*/
/*
float modifyX=0, modifyY=0;
float newWidth = (float)pageFormat.getWidth();
float newHeight=(float)pageFormat.getHeight();
Float leftMargin = (Float)hints.get(KEY_MARGIN_LEFT);
Float topMargin = (Float)hints.get(KEY_MARGIN_TOP);
Float rightMargin = (Float)hints.get(KEY_MARGIN_RIGHT);
Float bottomMargin = (Float)hints.get(KEY_MARGIN_BOTTOM);
if(leftMargin != null){
modifyX = leftMargin.floatValue();
newWidth -= leftMargin.floatValue();
}
if(topMargin != null){
modifyX = topMargin.floatValue();
newHeight -= topMargin.floatValue();
}
if(rightMargin != null){
newWidth -= rightMargin.floatValue();
}
if(bottomMargin != null){
newHeight -= bottomMargin.floatValue();
}
g.setClip((int)modifyX,(int)modifyY,(int)newWidth,(int)newHeight);
*/
//
// Compute transform so that the SVG document fits on one page
//
AffineTransform t = g.getTransform();
Shape clip = g.getClip();
//System.err.println("Returns the x/y coordinate of the upper left point of the imageable area of the Paper object associated with this PageFormat. X/Y: " + pageFormat.getImageableX() + ", " + pageFormat.getImageableY());
//System.err.println("Returns the width/height of the imageable area of the page. ((1/72 INCH) W/H: " + width + ", " + height);
//System.err.println("Returns the width/height of the imageable area of the page. " + (width/72) + ", " + (height/72));
//System.err.println("Returns the width/height of the page. (INCH) page W/page H: " + (pageFormat.getWidth()/72) + ", " + (pageFormat.getHeight()/72));
//System.err.println("Returns the width/height of the page. (1/72 INCH) page W/page H: " + (pageFormat.getWidth()) + ", " + (pageFormat.getHeight()));
//System.err.println("Clip: " + clip.getBounds2D());
// Offset 0,0 to the start of the imageable Area.
//g.translate(pageFormat.getImageableX(),pageFormat.getImageableY()); // amar - removed
//
// Append transform to selected area
//
g.transform(theCurTxf.get(pageIndex));
//
// Delegate rendering to painter
//
try{
root.paint(g);
}catch(Exception e){
//System.err.println("Clip is set. because of an exception");
g.setTransform(t);
g.setClip(clip);
drawError(_g, e);
}
//
// Restore transform and clip
//
g.setTransform(t);
g.setClip(clip);
//System.err.println("Clip was restored.");
// g.setPaint(Color.black);
// g.drawString(uris[pageIndex], 30, 30);
//
// Return status indicated that we did paint a page
//
return PAGE_EXISTS;
}
/**
* Sets document size according to the hints.
* Global variables width and height are modified.
*
* @param docWidth Width of the document.
* @param docHeight Height of the document.
*/
protected void setImageSize(float docWidth, float docHeight) {
// Check hint to know if scaling is really needed
Boolean scaleToPage = (Boolean)hints.get(KEY_SCALE_TO_PAGE);
if(scaleToPage != null && !scaleToPage.booleanValue()) {
float w = docWidth;
float h = docHeight;
if (hints.containsKey(KEY_AOI)) {
Rectangle2D aoi = (Rectangle2D)hints.get(KEY_AOI);
w = (float)aoi.getWidth();
h = (float)aoi.getHeight();
}
super.setImageSize(w, h);
}
}
/**
* Prints an error on the output page
*/
private void drawError(Graphics g, Exception e){
userAgent.displayError(e);
// Should also probably draw exception on page.
}
// --------------------------------------------------------------------
// Keys definition
// --------------------------------------------------------------------
/**
* The showPageDialog key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_SHOW_PAGE_DIALOG</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Boolean</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">false</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">Specifies whether or not the transcoder
* should pop up a dialog box for selecting
* the page format.</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_SHOW_PAGE_DIALOG
= new BooleanKey();
/**
* The showPrinterDialog key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_SHOW_PAGE_DIALOG</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Boolean</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">false</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">Specifies whether or not the transcoder
* should pop up a dialog box for selecting
* the printer. If the dialog box is not
* shown, the transcoder will use the default
* printer.</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_SHOW_PRINTER_DIALOG
= new BooleanKey();
/**
* The pageWidth key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_PAGE_WIDTH</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The width of the print page</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_PAGE_WIDTH
= new LengthKey();
/**
* The pageHeight key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_PAGE_HEIGHT</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The height of the print page</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_PAGE_HEIGHT
= new LengthKey();
/**
* The marginTop key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_MARGIN_TOP</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The print page top margin</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_MARGIN_TOP
= new LengthKey();
/**
* The marginRight key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_MARGIN_RIGHT</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The print page right margin</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_MARGIN_RIGHT
= new LengthKey();
/**
* The marginBottom key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_MARGIN_BOTTOM</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The print page bottom margin</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_MARGIN_BOTTOM
= new LengthKey();
/**
* The marginLeft key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_MARGIN_LEFT</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Length</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">None</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The print page left margin</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_MARGIN_LEFT
= new LengthKey();
/**
* The pageOrientation key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_PAGE_ORIENTATION</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">String</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">VALUE_PAGE_ORIENTATION_PORTRAIT</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">The print page's orientation</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_PAGE_ORIENTATION
= new StringKey();
/**
* The scaleToPage key.
* <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
* <TD VALIGN="TOP">KEY_SCALE_TO_PAGE</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
* <TD VALIGN="TOP">Boolean</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
* <TD VALIGN="TOP">true</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
* <TD VALIGN="TOP">No</TD></TR>
* <TR>
* <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
* <TD VALIGN="TOP">Specifies whether or not the SVG images are scaled to
* fit into the printed page</TD></TR>
* </TABLE> */
public static final TranscodingHints.Key KEY_SCALE_TO_PAGE
= new BooleanKey();
public static final String USAGE = "java org.apache.batik.transcoder.print.PrintTranscoder <svgFileToPrint>";
public static void setTranscoderFloatHint(Transcoder transcoder,
String property, TranscodingHints.Key key) {
String str = System.getProperty(property);
if (str != null) {
try {
Float value = new Float(Float.parseFloat(str));
transcoder.addTranscodingHint(key, value);
} catch (NumberFormatException e) {
handleValueError(property, str);
}
}
}
public static void setTranscoderRectangleHint(Transcoder transcoder,
String property, TranscodingHints.Key key) {
String str = System.getProperty(property);
if (str != null) {
StringTokenizer st = new StringTokenizer(str, " ,");
if (st.countTokens() != 4) {
handleValueError(property, str);
}
try {
String x = st.nextToken();
String y = st.nextToken();
String width = st.nextToken();
String height = st.nextToken();
Rectangle2D r = new Rectangle2D.Float(Float.parseFloat(x),
Float.parseFloat(y), Float.parseFloat(width), Float
.parseFloat(height));
transcoder.addTranscodingHint(key, r);
} catch (NumberFormatException e) {
handleValueError(property, str);
}
}
}
public static void setTranscoderBooleanHint(Transcoder transcoder,
String property, TranscodingHints.Key key) {
String str = System.getProperty(property);
if (str != null) {
Boolean value = "true".equalsIgnoreCase(str) ? Boolean.TRUE
: Boolean.FALSE;
transcoder.addTranscodingHint(key, value);
}
}
public static void setTranscoderStringHint(Transcoder transcoder,
String property, TranscodingHints.Key key) {
String str = System.getProperty(property);
if (str != null) {
transcoder.addTranscodingHint(key, str);
}
}
public static void handleValueError(String property, String value) {
System.err.println("Invalid " + property + " value : " + value);
System.exit(1);
}
/**
* Called to notify the client that data has been successfully transferred to the print service, and the client may free local resources allocated for that data.
*/
public void printDataTransferCompleted(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printDataTransferCompleted() -- print data Transfer completed.");
this.owner.setPrintJobIsCompeted(true);
logger.info("Thread ID = (" +owner.getThreadID()+"). ownerjobstate = (" + this.owner.getPrintJobIsCompeted()+").");
}
/**
* Called to notify the client that the job was canceled by user or program.
*/
public void printJobCanceled(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printJobCanceled() -- print job was canceled.");
this.owner.setPrintJobIsCompeted(false);
logger.debug("Thread ID = (" +owner.getThreadID()+"). PrintJobIsCompleted = (" + this.owner.getPrintJobIsCompeted()+").");
}
/**
* led to notify the client that the job completed successfully.
*/
public void printJobCompleted(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printJobCompleted() -- print Job Transfer completed.");
this.owner.setPrintJobIsCompeted(true);
logger.debug("Thread ID = (" +owner.getThreadID()+"). PrintJobIsCompleted = (" + this.owner.getPrintJobIsCompeted()+").");
}
/**
* Called to notify the client that the job failed to complete successfully and will have to be resubmitted.
*/
public void printJobFailed(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printJobFailed -- print job failed.");
this.owner.setPrintJobIsCompeted(false);
logger.debug("Thread ID = (" +owner.getThreadID()+"). PrintJobIsCompleted = (" + this.owner.getPrintJobIsCompeted()+").");
}
/**
* Called to notify the client that no more events will be delivered. One cause of this event being generated is if the job has successfully completed,
* but the printing system is limited in capability and cannot verify this.
* This event is required to be delivered if none of the other terminal events (completed/failed/canceled) are delivered.
*/
public void printJobNoMoreEvents(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printJobNoMoreEvents -- print job, no more events will be delivered.");
}
/**
* Called to notify the client that some possibly user rectifiable problem occurs (eg printer out of paper).
*/
public void printJobRequiresAttention(PrintJobEvent pje) {
logger.info("Thread ID = (" +owner.getThreadID()+"). BatikPrintTranscoder::printJobRequiresAttention -- print job some possibly user rectifiable problem occured.");
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]