📄 printutil.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.print;
import java.awt.print.*;
import java.util.*;
import java.util.logging.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
import org.compiere.util.*;
/**
* Print Utilities
*
* @author Jorg Janke
* @version $Id: PrintUtil.java,v 1.27 2005/11/13 23:40:21 jjanke Exp $
*/
public class PrintUtil
{
/** Logger */
private static CLogger log = CLogger.getCLogger(PrintUtil.class);
/** Default Print Request Attribute Set */
private static PrintRequestAttributeSet s_prats = new HashPrintRequestAttributeSet();
/**
* Return Default Print Request Attributes
* @return PrintRequestAttributeSet
*/
public static PrintRequestAttributeSet getDefaultPrintRequestAttributes()
{
return s_prats;
} // getDefaultPrintRequestAttributes
/**
* Get Default Application Flavor
* @return Pageable
*/
public static DocFlavor getDefaultFlavor()
{
return DocFlavor.SERVICE_FORMATTED.PAGEABLE;
} // getDefaultFlavor
/**
* Get Print Services for standard flavor and pratt
* @return print services
*/
public static PrintService[] getPrintServices ()
{
return PrintServiceLookup.lookupPrintServices (getDefaultFlavor(), getDefaultPrintRequestAttributes());
} // getPrintServices
/**
* Get Default Print Service
* @return PrintService
*/
public static PrintService getDefaultPrintService()
{
return PrintServiceLookup.lookupDefaultPrintService();
} // getPrintServices
/*************************************************************************/
/**
* Print (async)
* @param printerName optional printer name
* @param jobName optional printer job name
* @param pageable pageable
* @param copies number of copies
* @param withDialog if true, shows printer dialog
*/
static public void print (Pageable pageable, String printerName, String jobName,
int copies, boolean withDialog)
{
if (pageable == null)
return;
String name = "Compiere_";
if (jobName != null)
name += jobName;
//
PrinterJob job = CPrinter.getPrinterJob(printerName);
job.setJobName (name);
job.setPageable (pageable);
// Attributes
HashPrintRequestAttributeSet prats = new HashPrintRequestAttributeSet();
prats.add(new Copies(copies));
// Set Orientation
if (pageable.getPageFormat(0).getOrientation() == PageFormat.PORTRAIT)
prats.add(OrientationRequested.PORTRAIT);
else
prats.add(OrientationRequested.LANDSCAPE);
prats.add(new JobName(name, Language.getLoginLanguage().getLocale()));
prats.add(getJobPriority(pageable.getNumberOfPages(), copies, withDialog));
//
print (job, prats, withDialog, false);
} // print
/**
* Print Async
* @param pageable pageable
* @param prats print attribure set
*/
static public void print (Pageable pageable, PrintRequestAttributeSet prats)
{
PrinterJob job = CPrinter.getPrinterJob();
job.setPageable(pageable);
print (job, prats, true, false);
} // print
/**
* Print
* @param job printer job
* @param prats print attribure set
* @param withDialog if true shows Dialog
* @param waitForIt if false print async
*/
static public void print (final PrinterJob job,
final PrintRequestAttributeSet prats,
boolean withDialog, boolean waitForIt)
{
if (job == null)
return;
boolean printed = true;
if (withDialog)
printed = job.printDialog(prats);
if (printed)
{
if (withDialog)
{
Attribute[] atts = prats.toArray();
for (int i = 0; i < atts.length; i++)
log.fine(atts[i].getName() + "=" + atts[i]);
}
//
if (waitForIt)
{
log.fine("(wait) " + job.getPrintService());
try
{
job.print(prats);
}
catch (Exception ex)
{
log.log(Level.SEVERE, "(wait)", ex);
}
}
else // Async
{
// Create Thread
Thread printThread = new Thread()
{
public void run()
{
log.fine("print: " + job.getPrintService());
try
{
job.print(prats);
}
catch (Exception ex)
{
log.log(Level.SEVERE, "print", ex);
}
}
};
printThread.start();
} // Async
} // printed
} // printAsync
/**
* Get Job Priority based on pages printed.
* The more pages, the lower the priority
* @param pages number of pages
* @param copies number of copies
* @param withDialog dialog gets lower priority than direct print
* @return Job Priority
*/
static public JobPriority getJobPriority (int pages, int copies, boolean withDialog)
{
// Set priority (the more pages, the lower the priority)
int priority = copies * pages;
if (withDialog) // prefer direct print
priority *= 2;
priority = 100 - priority; // convert to 1-100 supported range
if (priority < 10)
priority = 10;
else if (priority > 100)
priority = 100;
return new JobPriority(priority);
} // getJobPriority
/*************************************************************************/
/**
* Dump Printer Job info
* @param job printer job
*/
public static void dump (PrinterJob job)
{
StringBuffer sb = new StringBuffer(job.getJobName());
sb.append("/").append(job.getUserName())
.append(" Service=").append(job.getPrintService().getName())
.append(" Copies=").append(job.getCopies());
PageFormat pf = job.defaultPage();
sb.append(" DefaultPage ")
.append("x=").append(pf.getImageableX())
.append(",y=").append(pf.getImageableY())
.append(" w=").append(pf.getImageableWidth())
.append(",h=").append(pf.getImageableHeight());
System.out.println(sb.toString());
} // dump
/**
* Dump Print Service Attribute Set to System.out
* @param psas PS Attribute Set
*/
public static void dump (PrintServiceAttributeSet psas)
{
System.out.println("PrintServiceAttributeSet - length=" + psas.size());
Attribute[] ats = psas.toArray();
for (int i = 0; i < ats.length; i++)
System.out.println(ats[i].getName() + " = " + ats[i] + " (" + ats[i].getCategory() + ")");
} // dump
/**
* Dump Print Request Service Attribute Set to System.out
* @param prats Print Request Attribute Set
*/
public static void dump (PrintRequestAttributeSet prats)
{
System.out.println("PrintRequestAttributeSet - length=" + prats.size());
Attribute[] ats = prats.toArray();
for (int i = 0; i < ats.length; i++)
System.out.println(ats[i].getName() + " = " + ats[i] + " (" + ats[i].getCategory() + ")");
} // dump
/**
* Dump Stream Print Services
* @param docFlavor flavor
* @param outputMimeType mime
*/
public static void dump (DocFlavor docFlavor, String outputMimeType)
{
System.out.println();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -