package JustusPiater.xml; import org.apache.batik.transcoder.print.PrintTranscoder; import org.apache.batik.transcoder.TranscoderInput; import java.awt.print.*; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.standard.*; import javax.print.*; import java.io.File; class SVG2PS extends PrintTranscoder { String dstfile; /* override this method to print directly into a file: */ public void print() throws PrinterException{ // // Now, request the transcoder to actually perform the // printing job. // PrinterJob printerJob = PrinterJob.getPrinterJob(); PageFormat pageFormat = printerJob.defaultPage(); // // Set the page parameters from the hints // Paper paper = pageFormat.getPaper(); Float pageWidth = (Float)hints.get(KEY_PAGE_WIDTH); Float pageHeight = (Float)hints.get(KEY_PAGE_HEIGHT); if(pageWidth != null){ paper.setSize(pageWidth.floatValue(), paper.getHeight()); } if(pageHeight != null){ paper.setSize(paper.getWidth(), pageHeight.floatValue()); } float x=0, y=0; float width=(float)paper.getWidth(), height=(float)paper.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){ x = leftMargin.floatValue(); width -= leftMargin.floatValue(); } if(topMargin != null){ y = topMargin.floatValue(); height -= topMargin.floatValue(); } if(rightMargin != null){ width -= rightMargin.floatValue(); } if(bottomMargin != null){ height -= bottomMargin.floatValue(); } paper.setImageableArea(x, y, width, height); String pageOrientation = (String)hints.get(KEY_PAGE_ORIENTATION); if(VALUE_PAGE_ORIENTATION_PORTRAIT.equalsIgnoreCase(pageOrientation)){ pageFormat.setOrientation(pageFormat.PORTRAIT); } else if(VALUE_PAGE_ORIENTATION_LANDSCAPE.equalsIgnoreCase(pageOrientation)){ pageFormat.setOrientation(pageFormat.LANDSCAPE); } else if(VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE.equalsIgnoreCase(pageOrientation)){ pageFormat.setOrientation(pageFormat.REVERSE_LANDSCAPE); } pageFormat.setPaper(paper); pageFormat = printerJob.validatePage(pageFormat); // // If required, pop up a dialog to adjust the page format // Boolean showPageFormat = (Boolean)hints.get(KEY_SHOW_PAGE_DIALOG); if(showPageFormat != null && showPageFormat.booleanValue()){ PageFormat tmpPageFormat = printerJob.pageDialog(pageFormat); if(tmpPageFormat == pageFormat){ // Dialog was cancelled, meaning that the print process should // be stopped. return; } pageFormat = tmpPageFormat; } // // If required, pop up a dialog to select the printer // Boolean showPrinterDialog = (Boolean)hints.get(KEY_SHOW_PRINTER_DIALOG); if(showPrinterDialog != null && showPrinterDialog.booleanValue()){ if(!printerJob.printDialog()){ // Dialog was cancelled, meaning that the print process // should be stopped. return; } } // Print now printerJob.setPrintable(this, pageFormat); /* Justus: print to file name corresponding to first SVG file */ // from the PrinterJob API doc: try { StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices("application/postscript"); StreamPrintService psPrinter = factories[0].getPrintService(new java.io.FileOutputStream(dstfile)); printerJob.setPrintService(psPrinter); } catch (Exception e) { System.err.println("Cannot print to file"); return; } printerJob.print(); } protected SVG2PS(String dstfile) throws java.net.URISyntaxException { super(); this.dstfile = dstfile; } public static void main(String args[]) throws Exception{ if(args.length < 1){ System.err.println(USAGE); System.exit(0); } int dot = args[0].lastIndexOf("."); String dstfilename = ((dot > 0) ? args[0].substring(0, dot) : args[0]) + ".ps"; SVG2PS transcoder = new SVG2PS(dstfilename); // All the rest is copied verbatim from PrintTranscoder.java:main(): // // Set the hints, from the command line arguments // // Language setTranscoderFloatHint(transcoder, KEY_LANGUAGE_STR, KEY_LANGUAGE); // User stylesheet setTranscoderFloatHint(transcoder, KEY_USER_STYLESHEET_URI_STR, KEY_USER_STYLESHEET_URI); // XML parser setTranscoderStringHint(transcoder, KEY_XML_PARSER_CLASSNAME_STR, KEY_XML_PARSER_CLASSNAME); // Scale to page setTranscoderBooleanHint(transcoder, KEY_SCALE_TO_PAGE_STR, KEY_SCALE_TO_PAGE); // AOI setTranscoderRectangleHint(transcoder, KEY_AOI_STR, KEY_AOI); // Image size setTranscoderFloatHint(transcoder, KEY_WIDTH_STR, KEY_WIDTH); setTranscoderFloatHint(transcoder, KEY_HEIGHT_STR, KEY_HEIGHT); // Pixel to millimeter setTranscoderFloatHint(transcoder, KEY_PIXEL_TO_MM_STR, KEY_PIXEL_UNIT_TO_MILLIMETER); // Page orientation setTranscoderStringHint(transcoder, KEY_PAGE_ORIENTATION_STR, KEY_PAGE_ORIENTATION); // Page size setTranscoderFloatHint(transcoder, KEY_PAGE_WIDTH_STR, KEY_PAGE_WIDTH); setTranscoderFloatHint(transcoder, KEY_PAGE_HEIGHT_STR, KEY_PAGE_HEIGHT); // Margins setTranscoderFloatHint(transcoder, KEY_MARGIN_TOP_STR, KEY_MARGIN_TOP); setTranscoderFloatHint(transcoder, KEY_MARGIN_RIGHT_STR, KEY_MARGIN_RIGHT); setTranscoderFloatHint(transcoder, KEY_MARGIN_BOTTOM_STR, KEY_MARGIN_BOTTOM); setTranscoderFloatHint(transcoder, KEY_MARGIN_LEFT_STR, KEY_MARGIN_LEFT); // Dialog options setTranscoderBooleanHint(transcoder, KEY_SHOW_PAGE_DIALOG_STR, KEY_SHOW_PAGE_DIALOG); setTranscoderBooleanHint(transcoder, KEY_SHOW_PRINTER_DIALOG_STR, KEY_SHOW_PRINTER_DIALOG); // // First, request the transcoder to transcode // each of the input files // for(int i=0; i