下面列出了java.awt.print.PrinterIOException#java.awt.print.PageFormat 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Not implemented.
*
* @param graphics the graphics.
* @param pageFormat the page format.
* @param pageIndex the page index.
*
* @return ?.
*
* @throws PrinterException if there is a problem.
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
/*
CairoImage image = new CairoImage(
this.getBounds().width, this.getBounds().height);
Graphics2D g2 = image.createGraphics2D();
double x = pageFormat.getImageableX();
double y = pageFormat.getImageableY();
double w = pageFormat.getImageableWidth();
double h = pageFormat.getImageableHeight();
this.chart.draw(
g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null
);
*/
return PAGE_EXISTS;
}
/**
* Creates a print job for the chart.
*/
public void createChartPrintJob() {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
PageFormat pf2 = job.pageDialog(pf);
if (pf2 != pf) {
job.setPrintable(this, pf2);
if (job.printDialog()) {
try {
job.print();
}
catch (PrinterException e) {
JOptionPane.showMessageDialog(this, e);
}
}
}
}
protected MasterReport createRelationalReport( final TableModel tableModel, final String... additionalFields ) {
final MasterReport report = new MasterReport();
report.setPageDefinition( new SimplePageDefinition( PageSize.A3, PageFormat.LANDSCAPE, new Insets( 0, 0, 0, 0 ) ) );
report.setDataFactory( new TableDataFactory( "query", tableModel ) );
report.setQuery( "query" );
final DesignTimeDataSchemaModel dataSchemaModel = new DesignTimeDataSchemaModel( report );
final RelationalReportBuilder builder = new RelationalReportBuilder( dataSchemaModel );
builder.addGroup( ROW_DIMENSION_A );
builder.addGroup( ROW_DIMENSION_B );
builder.addGroup( COLUMN_DIMENSION_A );
builder.addGroup( COLUMN_DIMENSION_B );
builder.addDetails( VALUE, null, Color.lightGray );
builder.addDetails( "relational", null, Color.yellow );
for ( int i = 0; i < additionalFields.length; i++ ) {
String additionalField = additionalFields[i];
builder.addDetails( additionalField, null, null );
}
report.setRootGroup( builder.create() );
return report;
}
public void testLayout() throws ResourceException, ContentProcessingException, ReportProcessingException {
final MasterReport basereport = new MasterReport();
basereport.setPageDefinition( new SimplePageDefinition( new PageFormat() ) );
final URL target = WeirdLayoutIT.class.getResource( "weird-layouting.xml" );
final ResourceManager rm = new ResourceManager();
rm.registerDefaults();
final Resource directly = rm.createDirectly( target, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
final Band band = report.getReportHeader();
band.setName( "ReportHeader1" );
DebugReportRunner.layoutSingleBand( report, band, false, true );
}
protected MediaPrintableArea getDefaultPrintableArea(PageFormat page,
double w, double h) {
double ix, iw, iy, ih;
if (w >= 72.0 * 6.0) {
ix = 72.0;
iw = w - 2 * 72.0;
} else {
ix = w / 6.0;
iw = w * 0.75;
}
if (h >= 72.0 * 6.0) {
iy = 72.0;
ih = h - 2 * 72.0;
} else {
iy = h / 6.0;
ih = h * 0.75;
}
return new MediaPrintableArea((float) (ix / DPI), (float) (iy / DPI),
(float) (iw / DPI), (float) (ih / DPI), MediaPrintableArea.INCH);
}
protected MediaPrintableArea getDefaultPrintableArea(PageFormat page,
double w, double h) {
double ix, iw, iy, ih;
if (w >= 72.0 * 6.0) {
ix = 72.0;
iw = w - 2 * 72.0;
} else {
ix = w / 6.0;
iw = w * 0.75;
}
if (h >= 72.0 * 6.0) {
iy = 72.0;
ih = h - 2 * 72.0;
} else {
iy = h / 6.0;
ih = h * 0.75;
}
return new MediaPrintableArea((float) (ix / DPI), (float) (iy / DPI),
(float) (iw / DPI), (float) (ih / DPI), MediaPrintableArea.INCH);
}
public void print() {
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat format = new PageFormat();
java.awt.print.Book book = new java.awt.print.Book();
book.append(this, format);
printerJob.setPageable(book);
if(printerJob.printDialog()) {
try {
printerJob.print();
} catch(PrinterException pe) {
// JOptionPane.showMessageDialog(c,
// TrackerRes.getString("TActions.Dialog.PrintError.Message"), //$NON-NLS-1$
// TrackerRes.getString("TActions.Dialog.PrintError.Title"), //$NON-NLS-1$
// JOptionPane.ERROR_MESSAGE);
}
}
}
public void testMinChunkWidth() throws Exception {
if ( DebugReportRunner.isSafeToTestComplexText() == false ) {
return;
}
final MasterReport basereport = new MasterReport();
basereport.setPageDefinition( new SimplePageDefinition( new PageFormat() ) );
basereport.setCompatibilityLevel( null );
basereport.getReportConfiguration().setConfigProperty( ClassicEngineCoreModule.COMPLEX_TEXT_CONFIG_OVERRIDE_KEY,
"true" );
final URL target = LayoutIT.class.getResource( "min-chunkwidth.xml" );
final ResourceManager rm = new ResourceManager();
rm.registerDefaults();
final Resource directly = rm.createDirectly( target, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
report.getStyle().setStyleProperty( TextStyleKeys.WORDBREAK, true );
final LogicalPageBox logicalPageBox =
DebugReportRunner.layoutSingleBand( basereport, report.getReportHeader(), true, false );
// simple test, we assert that all paragraph-poolboxes are on either 485 or 400
// and that only two lines exist for each
ModelPrinter.INSTANCE.print( logicalPageBox );
new ValidateRunner( false, true ).startValidation( logicalPageBox );
}
/**
* Creates a print job for the chart.
*/
public void createChartPrintJob() {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
PageFormat pf2 = job.pageDialog(pf);
if (pf2 != pf) {
job.setPrintable(this, pf2);
if (job.printDialog()) {
try {
job.print();
}
catch (PrinterException e) {
JOptionPane.showMessageDialog(this, e);
}
}
}
}
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 int print(Graphics graphics,
PageFormat pageFormat,
int pageIndex)
throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
this.paint(graphics);
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
g.translate((int) pf.getImageableX(), (int) pf.getImageableY());
g.setFont(new Font("Dialog", Font.PLAIN, 36));
g.drawString("\u4e00\u4e01\u4e02\u4e03\u4e04English", 20, 100);
return Printable.PAGE_EXISTS;
}
public PageFormat getPageFormat(int pgIndex) {
if (pgIndex > 0) {
throw new IndexOutOfBoundsException("pgIndex");
} else {
return pf;
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
double x0 = pageFormat.getImageableX();
double y0 = pageFormat.getImageableY();
double w0 = pageFormat.getImageableWidth();
double h0 = pageFormat.getImageableHeight();
double w1 = getWidth();
double h1 = getHeight();
double scale;
if (w0 / w1 < h0 / h1) {
scale = w0 / w1;
} else {
scale = h0 /h1;
}
g2d.translate(x0, y0);
g2d.scale(scale, scale);
Color bg = getBackground();
setBackground(Color.white);
paint(g2d);
setBackground(bg);
return(PAGE_EXISTS);
}
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
/**
* End a page.
*/
@Override
protected void endPage(PageFormat format, Printable painter,
int index) {
deviceEndPage(format, painter, index);
}
public int getPageCount() {
int res = 0;
for (int index = 0; index < painters.length; index++) {
PIDEF0painter painter = getPainter(index);
res += painter.hPageCount;
}
if (getPageFormat().getOrientation() == PageFormat.PORTRAIT)
res = (int) Math.ceil((double) res / 2.d);
return res;
}
/**
* The passed in PageFormat is cloned and altered to be usable on
* the PrinterJob's current printer.
*/
public PageFormat validatePage(PageFormat page) {
PageFormat newPage = (PageFormat)page.clone();
Paper newPaper = new Paper();
validatePaper(newPage.getPaper(), newPaper);
newPage.setPaper(newPaper);
return newPage;
}
public PageFormat getPageFormat(int pgIndex) {
if (pgIndex > 0) {
throw new IndexOutOfBoundsException("pgIndex");
} else {
return pf;
}
}
public int print(Graphics g, PageFormat pageFormat, int page) {
// Create the Graphics2D object, Translate the origin to 0,0 for the top left corner, Set the default drawing color to black
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setPaint(Color.black);
// Print the full-page PImage by converting to native BufferedImage
P.out("PageFormat size: ", (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight());
g2d.drawImage((BufferedImage) printBuffer.getNative(), 0, 0, (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight(), null);
return (PAGE_EXISTS);
}
public void draw( final Graphics2D graphics, final Rectangle2D bounds ) {
final PageFormat gpf = getPageFormat();
final Rectangle2D.Double imageableArea =
new Rectangle2D.Double( gpf.getImageableX(), gpf.getImageableY(), gpf.getImageableWidth(), gpf
.getImageableHeight() );
graphics.setPaint( new Color( 225, 225, 225 ) );
graphics.fill( imageableArea );
graphics.setPaint( Color.gray );
graphics.draw( imageableArea );
final int pcH = pageDefinition.getPageCountHorizontal();
final int pcW = pageDefinition.getPageCountVertical();
final Line2D line = new Line2D.Double();
for ( int splitH = 1; splitH < pcH; splitH += 1 ) {
final double xPos = gpf.getImageableX() + ( splitH * gpf.getImageableWidth() );
line.setLine( xPos, gpf.getImageableY(), xPos, gpf.getImageableY() + gpf.getImageableHeight() );
graphics.draw( line );
}
for ( int splitW = 1; splitW < pcW; splitW += 1 ) {
final double yPos = gpf.getImageableY() + ( splitW * gpf.getImageableHeight() );
line.setLine( gpf.getImageableX(), yPos, gpf.getImageableX() + gpf.getImageableWidth(), yPos );
graphics.draw( line );
}
}
public int print(Graphics g, PageFormat pageFormat, int index) throws PrinterException {
//out("PAPER IS: " + myPapers.size());
if (index == myPapers.size()) {
return NO_SUCH_PAGE;
}
//out(" print: " + index);
myPapers.get(index).print(g);
return PAGE_EXISTS;
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
/**
* Prints the page at the specified index into the specified
* {@link Graphics} context in the specified
* format. A {@code PrinterJob} calls the
* {@code Printable} interface to request that a page be
* rendered into the context specified by
* {@code graphics}. The format of the page to be drawn is
* specified by {@code pageFormat}. The zero based index
* of the requested page is specified by {@code pageIndex}.
* If the requested page does not exist then this method returns
* NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
* The {@code Graphics} class or subclass implements the
* {@link java.awt.PrintGraphics} interface to provide additional
* information. If the {@code Printable} object
* aborts the print job then it throws a {@link PrinterException}.
* @param graphics the context into which the page is drawn
* @param pageFormat the size and orientation of the page being drawn
* @param pageIndex the zero based index of the page to be drawn
* @return PAGE_EXISTS if the page is rendered successfully
* or NO_SUCH_PAGE if {@code pageIndex} specifies a
* non-existent page.
* @exception java.awt.print.PrinterException
* thrown when the print job is terminated.
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
int result;
/* This method will be called by the PrinterJob on a thread other
* that the application's thread. We hold on to the graphics
* until we can rendevous with the application's thread and
* hand over the graphics. The application then does all the
* drawing. When the application is done drawing we rendevous
* again with the PrinterJob thread and release the Graphics
* so that it knows we are done.
*/
/* Add the graphics to the message queue of graphics to
* be rendered. This is really a one slot queue. The
* application's thread will come along and remove the
* graphics from the queue when the app asks for a graphics.
*/
graphicsToBeDrawn.append( (Graphics2D) graphics);
/* We now wait for the app's thread to finish drawing on
* the Graphics. This thread will sleep until the application
* release the graphics by placing it in the graphics drawn
* message queue. If the application signals that it is
* finished drawing the entire document then we'll get null
* returned when we try and pop a finished graphic.
*/
if (graphicsDrawn.pop() != null) {
result = PAGE_EXISTS;
} else {
result = NO_SUCH_PAGE;
}
return result;
}
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
Printable painter, PageFormat pageFormat,
int pageIndex, boolean canRedraw) {
super(graphics, printerJob);
mPainter = painter;
mPageFormat = pageFormat;
mPageIndex = pageIndex;
mCanRedraw = canRedraw;
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
/**
* Prints the page at the specified index into the specified
* {@link Graphics} context in the specified
* format. A <code>PrinterJob</code> calls the
* <code>Printable</code> interface to request that a page be
* rendered into the context specified by
* <code>graphics</code>. The format of the page to be drawn is
* specified by <code>pageFormat</code>. The zero based index
* of the requested page is specified by <code>pageIndex</code>.
* If the requested page does not exist then this method returns
* NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
* The <code>Graphics</code> class or subclass implements the
* {@link PrinterGraphics} interface to provide additional
* information. If the <code>Printable</code> object
* aborts the print job then it throws a {@link PrinterException}.
* @param graphics the context into which the page is drawn
* @param pageFormat the size and orientation of the page being drawn
* @param pageIndex the zero based index of the page to be drawn
* @return PAGE_EXISTS if the page is rendered successfully
* or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
* non-existent page.
* @exception java.awt.print.PrinterException
* thrown when the print job is terminated.
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
int result;
/* This method will be called by the PrinterJob on a thread other
* that the application's thread. We hold on to the graphics
* until we can rendevous with the application's thread and
* hand over the graphics. The application then does all the
* drawing. When the application is done drawing we rendevous
* again with the PrinterJob thread and release the Graphics
* so that it knows we are done.
*/
/* Add the graphics to the message queue of graphics to
* be rendered. This is really a one slot queue. The
* application's thread will come along and remove the
* graphics from the queue when the app asks for a graphics.
*/
graphicsToBeDrawn.append( (Graphics2D) graphics);
/* We now wait for the app's thread to finish drawing on
* the Graphics. This thread will sleep until the application
* release the graphics by placing it in the graphics drawn
* message queue. If the application signals that it is
* finished drawing the entire document then we'll get null
* returned when we try and pop a finished graphic.
*/
if (graphicsDrawn.pop() != null) {
result = PAGE_EXISTS;
} else {
result = NO_SUCH_PAGE;
}
return result;
}
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {
System.out.println("pi = " + pi);
if (pi >= 5) {
return NO_SUCH_PAGE;
}
g.drawString("Page : " + (pi+1), 200, 200);
return PAGE_EXISTS;
}
/**
* Print the graph associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
JPanel panel = getPanel();
if (panel != null && pagenumber == 0) {
if (panel instanceof GraphView) {
return ((GraphView) panel).print(gc0, format, pagenumber);
} else {
Graphics2D gc = ((Graphics2D) gc0);
Dimension dim = panel.getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
panel.print(gc0);
return Printable.PAGE_EXISTS;
}
}
return Printable.NO_SUCH_PAGE;
}
private PageDefinition createDefaultPageDefinition() {
final PageDefinition format;
final ExtendedConfiguration config = ClassicEngineBoot.getInstance().getExtendedConfig();
if ( config.getBoolProperty( ClassicEngineCoreModule.NO_PRINTER_AVAILABLE_KEY ) ) {
format = new SimplePageDefinition( new PageFormat() );
} else {
format = new SimplePageDefinition( PrinterJob.getPrinterJob().defaultPage() );
}
return format;
}