下面列出了怎么用java.awt.print.Book的API类实例代码及写法,或者点击链接到github查看源代码。
private static void doTest() {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(Chromaticity.MONOCHROME);
MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
Paper paper = new Paper();
paper.setSize(size[0] * 72.0, size[1] * 72.0);
paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
Book pageable = new Book();
pageable.append(new WrongPaperForBookPrintingTest(), pf);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(pageable);
if (job.printDialog()) {
try {
job.print(aset);
} catch (PrinterException pe) {
throw new RuntimeException(pe);
}
}
}
private static void doTest() {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(Chromaticity.MONOCHROME);
MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
Paper paper = new Paper();
paper.setSize(size[0] * 72.0, size[1] * 72.0);
paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
Book pageable = new Book();
pageable.append(new WrongPaperForBookPrintingTest(), pf);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(pageable);
if (job.printDialog()) {
try {
job.print(aset);
} catch (PrinterException pe) {
throw new RuntimeException(pe);
}
}
}
private static void doTest() {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(Chromaticity.MONOCHROME);
MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
Paper paper = new Paper();
paper.setSize(size[0] * 72.0, size[1] * 72.0);
paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
Book pageable = new Book();
pageable.append(new WrongPaperForBookPrintingTest(), pf);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(pageable);
if (job.printDialog()) {
try {
job.print(aset);
} catch (PrinterException pe) {
throw new RuntimeException(pe);
}
}
}
private static void doTest() {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(Chromaticity.MONOCHROME);
MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
Paper paper = new Paper();
paper.setSize(size[0] * 72.0, size[1] * 72.0);
paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
Book pageable = new Book();
pageable.append(new WrongPaperForBookPrintingTest(), pf);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(pageable);
if (job.printDialog()) {
try {
job.print(aset);
} catch (PrinterException pe) {
throw new RuntimeException(pe);
}
}
}
public void sendToDefaultPrinter() {
// configure the printing job & simple single-page document
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new ImagePage(), printJob.defaultPage());
PageFormat documentPageFormat = new PageFormat();
/*
// new settings to play with
Paper pape = new Paper();
pape.setSize(10000, 6000);
pape.setImageableArea(0, 0, 10000, 6000);
documentPageFormat.setPaper(pape);
*/
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
printJob.setPageable(book);
// use the print dialog UI or just print it!
if(useDialog) {
if (printJob.printDialog()) printPage(printJob);
} else {
printPage(printJob);
}
}
public AudioFormat colorized(Book b1, Book b2, int random) throws Exception, IllegalArgumentException {
LinkedList<Point2D> points = new LinkedList<Point2D>();
points.add(new Point(10, 20));
points.add(new Point(20, 20));
points.add(new Point(30, 20));
if(random > 100) {
Bar barInstance = new Bar();
barInstance.returnField(new JLabel("I am a JLabel object"));
if(random < 150) {
for(int i = 0 ; i < 10 ; ++i) {
JButton button = new JButton("I am a JButton object");
}
barInstance = new Bar(new JTextArea());
return new AudioFormat(Encoding.ALAW, (float)1.0, 8, 2, 1, (float)1.0, true, new HashMap<String, Object>());
}
else {
throw new Exception("a test exception");
}
}
else {
throw new IllegalArgumentException("a test illegal format exception");
}
}
/**
* Constructor: Example3
* <p>
*
*/
public void doPrintDemo() {
//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob();
//--- Create a new book to add pages to
Book book = new Book();
//--- Add the cover page using the default page format for this print job
book.append(new ImagePage(), printJob.defaultPage());
// book.append(new IntroPage(), printJob.defaultPage());
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.PORTRAIT);
// book.append(new Document(), documentPageFormat);
//--- Add a third page using the same painter
// book.append(new Document(), documentPageFormat);
//--- Tell the printJob to use the book as the pageable object
printJob.setPageable(book);
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
// remove the print dialog and just print it!
// if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
// }
}
public static void main(String args[]) {
new Book().getNumberOfPages();
}
/**
*
*/
public boolean printPages(
int firstPageIndex,
int lastPageIndex,
boolean withPrintDialog
) throws JRException
{
boolean isOK = true;
if (
firstPageIndex < 0 ||
firstPageIndex > lastPageIndex ||
lastPageIndex >= jasperPrint.getPages().size()
)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,
new Object[]{firstPageIndex, lastPageIndex, jasperPrint.getPages().size()}
);
}
pageOffset = firstPageIndex;
PrinterJob printJob = PrinterJob.getPrinterJob();
// fix for bug ID 6255588 from Sun bug database
initPrinterJobFields(printJob);
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("JasperReports - " + jasperPrint.getName());
switch (jasperPrint.getOrientationValue())
{
case LANDSCAPE :
{
pageFormat.setOrientation(PageFormat.LANDSCAPE);
paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
paper.setImageableArea(
0,
0,
jasperPrint.getPageHeight(),
jasperPrint.getPageWidth()
);
break;
}
case
PORTRAIT :
default :
{
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
paper.setImageableArea(
0,
0,
jasperPrint.getPageWidth(),
jasperPrint.getPageHeight()
);
}
}
pageFormat.setPaper(paper);
Book book = new Book();
book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
printJob.setPageable(book);
try
{
if (withPrintDialog)
{
if (printJob.printDialog())
{
printJob.print();
}
else
{
isOK = false;
}
}
else
{
printJob.print();
}
}
catch (Exception ex)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
null,
ex);
}
return isOK;
}
/**
*
*/
public boolean printPages(
int firstPageIndex,
int lastPageIndex,
PrintService service
) throws JRException
{
boolean isOK = true;
if (
firstPageIndex < 0 ||
firstPageIndex > lastPageIndex ||
lastPageIndex >= jasperPrint.getPages().size()
)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,
new Object[]{firstPageIndex, lastPageIndex, jasperPrint.getPages().size()}
);
}
pageOffset = firstPageIndex;
PrinterJob printJob = PrinterJob.getPrinterJob();
// fix for bug ID 6255588 from Sun bug database
initPrinterJobFields(printJob);
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("JasperReports - " + jasperPrint.getName());
switch (jasperPrint.getOrientationValue())
{
case LANDSCAPE :
{
pageFormat.setOrientation(PageFormat.LANDSCAPE);
paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
paper.setImageableArea(
0,
0,
jasperPrint.getPageHeight(),
jasperPrint.getPageWidth()
);
break;
}
case
PORTRAIT :
default :
{
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
paper.setImageableArea(
0,
0,
jasperPrint.getPageWidth(),
jasperPrint.getPageHeight()
);
}
}
pageFormat.setPaper(paper);
Book book = new Book();
book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
printJob.setPageable(book);
try
{
if (service == null) {
if (printJob.printDialog()) {
printJob.print();
} else {
isOK = false;
}
} else {
printJob.setPrintService(service);
printJob.print();
}
}
catch (Exception ex)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
null,
ex);
}
return isOK;
}
/**
* Constructor: Example3
* <p>
*
*/
public void doPrintDemo() {
//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob();
//--- Create a new book to add pages to
Book book = new Book();
//--- Add the cover page using the default page format for this print job
book.append(new ImagePage(), printJob.defaultPage());
// book.append(new IntroPage(), printJob.defaultPage());
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
//documentPageFormat.add(new Copies(1));
book.append(new Document(), documentPageFormat);
//--- Add a third page using the same painter
// book.append(new Document(), documentPageFormat);
//--- Tell the printJob to use the book as the pageable object
printJob.setPageable(book);
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
// remove the print dialog and just print it!
// if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
// }
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
/**
* Return the zero based index of the first page to
* be printed in this job.
*/
protected int getFirstPage() {
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
}
public void sendToDefaultPrinter() {
// configure the printing job & simple single-page document
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
float inchWidth = 16;
float inchHeight = 20;
float marginWidth = 100;
float marginHeight = 100;
int pWidth = (int) ((inchWidth * 25.4f) * 2.835f);// quick: convert inch to mm, mm to pt
int pHeight = (int) ((inchHeight * 25.4f) * 2.835f);
paper.setSize(pWidth, pHeight);
paper.setImageableArea(marginWidth, marginHeight, pWidth, pHeight);
pageFormat.setPaper(paper);
//not sure this exactly works, we should probably just scale the square to fit and we send it the correct rotation orientation
//pageFormat.setOrientation(PageFormat.LANDSCAPE);
// we might also be able to set attributes here
Printable page = new ImagePage();
Book book = new Book();
book.append(page, pageFormat);
/*
Early testing at "LikeMinded Productions":
PageFormat documentPageFormat = new PageFormat();
// new settings to play with
Paper pape = new Paper();
pape.setSize(10000, 6000);
pape.setImageableArea(0, 0, 10000, 6000);
documentPageFormat.setPaper(pape);
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
*/
printJob.setPageable(book);
// use the print dialog UI or just print it!
if(useDialog) {
if (printJob.printDialog()) printPage(printJob);
} else {
printPage(printJob);
}
}